Exemplo n.º 1
0
        public static void ParseLineWithoutClose(FCsvLine line, FCharReader reader, FCsvFormat format)
        {
            char    delimiter = format.Delimiter;
            FString value     = new FString();

            while (reader.HasNext())
            {
                // Read
                char ch = reader.Read();
                // Check
                if (ch == '\n')
                {
                    line.Append(value.Flush());
                    break;
                }
                else if (ch == delimiter)
                {
                    line.Append(value.Flush());
                    continue;
                }
                value.Append(ch);
            }
            if (!value.IsEmpty)
            {
                line.Append(value.Flush());
            }
        }
Exemplo n.º 2
0
        //============================================================
        // <T>解析CSV文件行。</T>
        //
        // @param reader 读取器
        // @param format 格式器
        // @return 行对象
        //============================================================
        public static FCsvLine ParseLine(FCharReader reader, FCsvFormat format)
        {
            FCsvLine line = null;

            if (reader.HasNext())
            {
                line = new FCsvLine();
                if (format.DataClose)
                {
                    ParseLineWithClose(line, reader, format);
                }
                else
                {
                    ParseLineWithoutClose(line, reader, format);
                }
            }
            return(line);
        }
Exemplo n.º 3
0
        public static void ParseLineWithClose(FCsvLine line, FCharReader reader, FCsvFormat format)
        {
            char    delimiter = format.Delimiter;
            bool    inComment = false;
            FString value     = new FString();

            while (reader.HasNext())
            {
                // Read
                char ch = reader.Read();
                // Check
                if (inComment)
                {
                    if (ch == '"')
                    {
                        char nch = reader.Get(1);
                        if (nch == '"')
                        {
                            value.Append(ch);
                            reader.Skip(1);
                            continue;
                        }
                        if (nch == '\n')
                        {
                            line.Append(value.Flush());
                            reader.Skip(1);
                            break;
                        }
                        if (nch == delimiter)
                        {
                            reader.Skip(1);
                        }
                        inComment = false;
                        line.Append(value.Flush());
                        continue;
                    }
                }
                else
                {
                    if (ch == '\n')
                    {
                        line.Append(value.Flush());
                        break;
                    }
                    else if (ch == '"')
                    {
                        inComment = true;
                        continue;
                    }
                    else if (ch == delimiter)
                    {
                        line.Append(value.Flush());
                        continue;
                    }
                }
                value.Append(ch);
            }
            if (!value.IsEmpty)
            {
                line.Append(value.Flush());
            }
        }