Esempio n. 1
0
            /// <summary>
            ///
            /// </summary>
            /// <param name="resbuf"></param>
            /// <returns></returns>
            public override List <int> ReadSplitLine(StringBuilder resbuf = null)
            {
                bool esc   = false;
                bool seqst = true;
                char prev  = '\0';
                var  pos   = new Pointer();

                this.oneLineBuffer.Clear();
                var res = new List <int>();

                res.Add(0);

                while (true)
                {
                    if (this.charIndex <= 0)
                    {
                        this.charIndex = this.innerStream.ReadBlock(this.charBuffer, 0, this.charBuffer.Length);
                        if (this.charIndex <= 0)
                        {
                            break;
                        }
                    }

                    for (int i = 0; i < this.charIndex; ++i)
                    {
                        char c = this.charBuffer[i];

                        switch (c)
                        {
                        case ',':
                            resbuf?.Append(c);
                            if (!esc)
                            {
                                pos.AjustLine(this.oneLineBuffer);
                                res.Add(this.oneLineBuffer.Count);
                                pos.srcStart = this.oneLineBuffer.Count;
                                pos.dstStart = pos.srcStart;
                                seqst        = true;
                            }
                            else
                            {
                                seqst = false;
                            }
                            break;

                        case '"':
                            resbuf?.Append(c);
                            seqst = false;
                            if (esc)
                            {
                                esc = false;
                            }
                            else
                            {
                                if (pos.srcStart == pos.srcEnd)
                                {
                                    esc = true;
                                }
                                else if (prev == '"')
                                {
                                    this.oneLineBuffer.Add('"');
                                    esc = true;
                                }
                            }
                            pos.dstEnd = this.oneLineBuffer.Count;
                            break;

                        case '\n':
                            seqst = false;
                            if (prev == '\r')
                            {
                                this.oneLineBuffer.RemoveAt(this.oneLineBuffer.Count - 1);
                                pos.srcEnd--;
                                if (resbuf != null)
                                {
                                    resbuf.Remove(resbuf.Length - 1, 1);
                                }
                            }
                            if (esc)
                            {
                                if (prev == '\r')
                                {
                                    this.oneLineBuffer.Add(prev);
                                    resbuf?.Append(prev);
                                }
                                this.oneLineBuffer.Add(c);
                                resbuf?.Append(c);
                            }
                            else
                            {
                                var clen = this.charIndex - (i + 1);
                                Array.Copy(this.charBuffer, i + 1, this.charBuffer, 0, clen);
                                this.charIndex = clen;
                                pos.AjustLine(this.oneLineBuffer);
                                res.Add(this.oneLineBuffer.Count);
                                return(res);
                            }
                            pos.dstEnd = this.oneLineBuffer.Count;
                            break;

                        default:
                            resbuf?.Append(c);
                            this.oneLineBuffer.Add(c);
                            if (char.IsWhiteSpace(c))
                            {
                                if (seqst)
                                {
                                    pos.dstStart++;
                                }
                            }
                            else
                            {
                                seqst      = false;
                                pos.dstEnd = this.oneLineBuffer.Count;
                            }
                            pos.srcEnd = this.oneLineBuffer.Count;
                            break;
                        }

                        prev = c;
                    }

                    this.charIndex = 0;

                    // 改行エスケープがあれば次を取り込む
                    if (this.innerStream.Peek() == -1)
                    {
                        break;
                    }
                }
                return(res);
            }