//============================================================ // <T>解析读入的一行。</T> // // @param pLine 读入的一行。 //============================================================ void FCsvLine::Parse(TCharC* pLine){ TStringRefer line = pLine; TInt lineLength = line.Length(); TInt cellStart, cellEnd, pos, dummy; cellStart = cellEnd = pos = dummy = 0; while(pos < lineLength){ TChar cur = pLine[pos]; if(cur == '"'){ dummy++; }else if(cur == ','){ TInt dummyTemp = dummy % 2; if(0 == dummyTemp){ dummy = 0; cellEnd = pos; if(cellEnd == cellStart){ _pCells->Push(""); }else{ TString cellText = line.SubStrC(cellStart, cellEnd); TInt cellTextLen = cellText.Length(); TString cell; if(WrapedByRefer(cellText)){ TString tempCell = cellText.SubStrC(1, cellTextLen - 1); SinglePairRefer(tempCell, cell); }else{ SinglePairRefer(cellText, cell); } _pCells->Push(cell); } cellStart = cellEnd + 1; } }else if(cur == '\n'){ cellEnd = pos; dummy %= 2; MO_ASSERT(0 == dummy); if(cellEnd == cellStart){ _pCells->Push(""); }else{ TString cellText = line.SubStrC(cellStart, cellEnd); TInt cellTextLen = cellText.Length(); TString cell; if(WrapedByRefer(cellText)){ TString tempCell = cellText.SubStrC(1, cellTextLen - 1); SinglePairRefer(tempCell, cell); }else{ SinglePairRefer(cellText, cell); } _pCells->Push(cell); } } pos++; } }
//============================================================ // <T>解析表段尾部项。</T> // // @param pValue 尾部项字符串。 //============================================================ void FCsvFooters::Parse(TCharC* pValue){ TStringRefer foot = pValue; TInt namesBegin = foot.IndexOf('\n') + 1; TInt namesEnd = foot.IndexOf('\n', namesBegin); TBool hasLabel = EFalse; TInt labelsBegin = ENotFound; TInt labelsEnd = ENotFound; // 存在label TInt headLength = foot.Length(); if(namesEnd != headLength -1){ hasLabel = ETrue; labelsBegin = foot.IndexOf('\n', namesEnd + 1) + 1; labelsEnd = foot.IndexOf('\n', labelsBegin); } TInt nameBegin = namesBegin; TInt nameEnd = foot.IndexOf(',', nameBegin); TInt labelBegin = ENotFound; TInt labelEnd = ENotFound; if(hasLabel){ labelBegin = labelsBegin; labelEnd = foot.IndexOf(',', labelBegin); } TInt count = 0; while(nameBegin < namesEnd){ if(ENotFound == nameEnd){ nameEnd = namesEnd; if(hasLabel){ MO_ASSERT(ENotFound == labelEnd); labelEnd = labelsEnd; } } FCsvFooter* pFooter = MO_CREATE(FCsvFooter); pFooter->SetIndex(count); TFsName name = foot.SubStrC(nameBegin, nameEnd); pFooter->SetName(name); _pFooters->Set(pFooter->Name(), pFooter); nameBegin = nameEnd + 1; if(nameBegin < namesEnd){ nameEnd = foot.IndexOf(',', nameBegin); } if(hasLabel){ TFsLabel label = foot.SubStrC(labelBegin, labelEnd); pFooter->SetLabel(label); labelBegin = labelEnd + 1; if(labelBegin < labelsEnd){ labelEnd = foot.IndexOf(',', labelBegin); } } count++; } }
//============================================================ // <T>将一行保存至文件。</T> // // @param out 文件输出流。 //============================================================ void FCsvLine::Store(TDataOutput& out){ TString csvCell; TString csvLine; TInt count = _pCells->Count(); for(TInt n = 0; n < count; n++){ csvCell.Clear(); TString tempCell; TStringRefer cell = _pCells->Get(n); TInt cellLength = cell.Length(); // 不允许空的格 MO_ASSERT( 0 != cellLength); // 处理双引号 TInt start = 0; TInt index = cell.IndexOf('"'); if(ENotFound != index){ while(ENotFound != index){ index++; tempCell.Append(cell.SubStrC(start, index)); // 不以引号开头或结尾 if(index != 1 && index != cellLength){ tempCell.Append('"'); }else{ tempCell.Append('"'); tempCell.Append('"'); } start = index; if(start < cellLength){ index = cell.IndexOf('"', start); }else{ break; } } // 拷贝余下内容 if(start < cellLength - 1){ tempCell.Append(cell.SubStrC(start, cell.Length())); } }else{ tempCell = cell; } // 处理逗号 TInt tempCellLen = tempCell.Length(); if(tempCell.Contains(',')){ // 不以“开头 if(tempCell[0] != '"'){ csvCell.Append('"'); } csvCell.Append(tempCell); // 不以“结尾 if(tempCell[tempCellLen -1] != '"'){ csvCell.Append('"'); } }else{ csvCell = tempCell; } // 处理换行符 TInt csvCellLen = csvCell.Length(); TInt lfStart = 0; TInt lfIndex = csvCell.IndexOf('\n'); if(ENotFound != lfIndex){ while(ENotFound != lfIndex){ // 拷贝换行符前面的内容 if(lfStart < lfIndex){ TString sub = csvCell.SubStrC(lfStart, lfIndex); csvLine.Append(sub); } csvLine.Append('\\'); csvLine.Append('n'); // 跳过换行符 lfIndex++; lfStart = lfIndex; if(lfStart < csvCellLen){ lfIndex = csvCell.IndexOf('\n', lfStart); }else{ break; } } if(lfStart < csvCellLen - 1){ TString sub = csvCell.SubStrC(lfStart, csvCell.Length()); csvLine.Append(sub); } }else{ csvLine.Append(csvCell); } // 一格格式化结束 if((count - 1) == n){ csvLine.Append('\n'); }else{ csvLine.Append(','); } } out.Write((TCharC*)csvLine, csvLine.Length()); }