//============================================================ // <T>将尾部项存入文件。</T> // // @param out 输出文件流。 //============================================================ void FCsvFooters::Store(TDataOutput& out){ FDictionary<FCsvFooter*>::TIteratorC it = _pFooters->IteratorC(); TString foot = TC("@footer.name\n"); TBool showLabel = EFalse; TBool showDescription = EFalse; TInt count = 0; TInt lastFooterIndex = _pFooters->Count() - 1; while(it.Next()){ FCsvFooter* pFoot = it.Value(); foot.Append(pFoot->Name()); if(count != lastFooterIndex){ foot.Append(','); }else{ foot.Append('\n'); } TInt labelLength = RString::Length(pFoot->Label()); if(labelLength){ showLabel = ETrue; } TInt desLength = RString::Length(pFoot->Description()); if(desLength){ showDescription= ETrue; } ++count; } if(showLabel){ foot.Append(TC("@footer.label\n")); count = 0; it.Reset(); while(it.Next()){ FCsvFooter* pFoot = it.Value(); foot.Append(pFoot->Label()); if(count != lastFooterIndex){ foot.Append(','); }else{ foot.Append('\n'); } } } if(showDescription){ foot.Append(TC("@head.description\n")); count = 0; it.Reset(); while(it.Next()){ FCsvFooter* pFoot = it.Value(); foot.Append(pFoot->Description()); if(count != lastFooterIndex){ foot.Append(','); }else{ foot.Append('\n'); } } } out.Write((TCharC*)foot, foot.Length()); }
//============================================================ // <T>将头部存入文件。</T> // // @param out 文件输出流。 //============================================================ void FCsvHeads::SaveToFile(TDataOutput& out){ FDictionary<FCsvHead*>::TIteratorC it = _pHeads->IteratorC(); TString head = TC("@head.name\n"); TBool showLabel = EFalse; TBool showDescription = EFalse; TInt count = 0; TInt headCount = _pHeads->Count() - 1; while(it.Next()){ FCsvHead* pHead = it.Value(); head.Append(pHead->Name()); if(count != headCount){ head.Append(','); }else{ head.Append('\n'); } TInt labelLength = RString::Length(pHead->Label()); if(labelLength){ showLabel = ETrue; } TInt desLength = RString::Length(pHead->Description()); if(desLength){ showDescription= ETrue; } ++count; } if(showLabel){ head.Append(TC("@head.label\n")); count = 0; it.Reset(); while(it.Next()){ FCsvHead* pHead = it.Value(); head.Append(pHead->Label()); if(count != headCount){ head.Append(','); }else{ head.Append('\n'); } } } if(showDescription){ head.Append(TC("@head.description\n")); count = 0; it.Reset(); while(it.Next()){ FCsvHead* pHead = it.Value(); head.Append(pHead->Description()); if(count != headCount){ head.Append(','); }else{ head.Append('\n'); } } } out.Write((TCharC*)head, head.Length()); }
//============================================================ // <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()); }