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()); } }
//============================================================ // <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); }
public bool Open(string filename) { if (!File.Exists(filename)) { throw new FFatalException("File is not exists (filename={0})", filename); } _lineFormat = false; _lineValidator = false; _filename = filename; StreamReader reader = null; if (_encoding != null) { reader = new StreamReader(new FileStream(filename, FileMode.Open), _encoding); } else { reader = new StreamReader(new FileStream(filename, FileMode.Open)); } _reader = new FCharReader(reader, 16); if (_format != null) { if (_format.Header) { ReadHeads(); } } _stopped = false; _readedCount = 0; if (_heads != null) { _lineFormat = _heads.HasValidator(ECsvWorkType.Read); _lineValidator = _heads.HasValidator(ECsvWorkType.Read); } return(_reader.HasNext()); }
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()); } }