Exemple #1
0
//============================================================
// <T>解析读入的头部字符串。</T>
//
// @param pValue 读入的头部字符串。
//============================================================
void FCsvHeads::Parse(TCharC* pValue){
   TStringRefer head = pValue;
   TInt headNamesBegin = head.IndexOf('\n') + 1;
   TInt headNamesEnd = head.IndexOf('\n', headNamesBegin);
   TBool hasLabel = EFalse;
   TInt labelsBegin = ENotFound;
   TInt labelsEnd = ENotFound;
   // 存在label
   TInt headLength = head.Length();
   if(headNamesEnd != headLength -1){
      hasLabel = ETrue;
      labelsBegin =  head.IndexOf('\n', headNamesEnd + 1) + 1;
      labelsEnd = head.IndexOf('\n', labelsBegin);
   }
   TInt nameBegin = headNamesBegin;
   TInt nameEnd = head.IndexOf(',', nameBegin);
   TInt labelBegin = ENotFound;
   TInt labelEnd = ENotFound;
   if(hasLabel){
      labelBegin = labelsBegin;
      labelEnd = head.IndexOf(',', labelBegin);
   }
   TInt count = 0;
   while(nameBegin < headNamesEnd){
      if(ENotFound == nameEnd){
         nameEnd = headNamesEnd;
         if(hasLabel){
            MO_ASSERT(ENotFound == labelEnd);
            labelEnd = labelsEnd;
         }

      }
      FCsvHead* pHead = MO_CREATE(FCsvHead);
      pHead->SetIndex(count);
      TFsName name = head.SubStrC(nameBegin, nameEnd);
      pHead->SetName(name);
      _pHeads->Set(pHead->Name(), pHead);
      nameBegin = nameEnd + 1;
      if(nameBegin < headNamesEnd){
         nameEnd = head.IndexOf(',', nameBegin);
      }
      if(hasLabel){
         TFsLabel label = head.SubStrC(labelBegin, labelEnd);
         pHead->SetLabel(label);
         labelBegin = labelEnd + 1;
         if( labelBegin < labelsEnd){
            labelEnd =  head.IndexOf(',', labelBegin);
         }
      }
      count++;
   }  
}
Exemple #2
0
//============================================================
// <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());
}