CSVEditor

#ifndef __CSVEDITOR_H__
#define __CSVEDITOR_H__

#pragma warning(disable : 4786)
#include <string>
#include <vector>

using namespace std;

class CSVEditor
{
public:
 
 /*
  使用的時候必須設置列數
 */
 explicit CSVEditor(int iRow = 10);

 /*
  添加一行數據
 */
 int AddOneLine( string & strInfo, const string & strSplict);

 /*
  修改指定行列的一個字符(行,列都是從 0 開始)
 */
 int ModifySpecfyData(int iLine, int iRow, const string & strdata);

 /*
  數據保存到文件
 */
 int SaveToFile(const char * pszFilePath);

 /*
  返回列數
 */
 int GetRows(void)
 {
  return m_iRows;
 }

 /*
  返回行數
 */
 int GetLines(void)
 {
  return m_iLines;
 }

private:

 void ReplaseSpecifyString(string & strSource, const string & strFind, const string & strReplase);
 
 void ModifySpecifyString(string & strSource, const string & strFind, const string & strSplict);

 int    m_iLines;   
 int    m_iRows;
 vector<string> m_vec;
};

#endif

 

 

//////////////////////////////////////////////////////

#include "CSVEditor.h"

const char gszSplict[] = {"#*#"};  // 私有字符串分隔符
const char gsChangeLine[] = "/n";

CSVEditor::CSVEditor(int iRow /* = 10 */)
{
 m_iRows = iRow;
}

int CSVEditor::AddOneLine(string & strInfo, const string & strSplict)
{
 string::size_type Pos = 0;
 string::size_type BackPos = Pos;

 ModifySpecifyString(strInfo,",", strSplict); //處理原始 , 號

 int iOffset = strSplict.length();
 int iAddLen = strlen(gszSplict);
 int iCount = 1;
 bool bNeedDel = true;


 while (iCount < m_iRows)
 {
  Pos = strInfo.find(strSplict, BackPos);
  iCount++;
  if ( string::npos != Pos )
  {
   strInfo.erase(Pos, iOffset);
   strInfo.insert(Pos, gszSplict);
   BackPos = Pos;
   BackPos+= iAddLen;
  }
  else
  {
   //數據不夠,剩餘的填空
   strInfo.insert(strInfo.length(),gszSplict);
   bNeedDel = false;
  }
 }

 if ((string::npos != BackPos) && bNeedDel)
 {
  Pos = strInfo.find(strSplict, BackPos);

  if (string::npos != Pos)
  {
   //刪除多餘數據
   int iEraseLen = strInfo.length() - Pos;
   strInfo.erase(Pos, iEraseLen);
  }
 }

 m_vec.push_back(strInfo);
 return 0;

}

int CSVEditor::ModifySpecfyData(int iLine, int iRow, const string & strdata)
{
 if ((iLine < m_iLines) && (iRow < m_iRows))
 {
  vector<string>::iterator iter = m_vec.begin();
  vector<string>::iterator iternext = m_vec.end();
  iter += iLine;

  string strOrg = *iter;
  string::size_type Pos = 0;
  string::size_type BackPos = Pos;
  
  int iCount = 0;  //將第0個元素當做第1個對象
  int iOffset = strlen(gszSplict);

  while ((string::npos != Pos) && (iCount < iRow))
  {
   Pos = strOrg.find(gszSplict, BackPos);
   BackPos = Pos;
   BackPos += iOffset;
   iCount++;
  }

  int iLen = 0;

  string::size_type EndPos = strOrg.find(gszSplict, BackPos);
  if( string::npos != EndPos )
  {
   iLen = EndPos - BackPos;
  }
  else
  {
   iLen = strOrg.length() - BackPos;
  }
  
  strOrg.replace(BackPos, iLen, strdata);

  return 0;
 }
 return -1;
}

int CSVEditor::SaveToFile(const char * pszFilePath)
{
 FILE *fe  = fopen(pszFilePath, "wt");
 if (NULL == fe)
 {
  return -1;
 }

 string strTmp;
 vector<string>::iterator iter = m_vec.begin();
 while(iter != m_vec.end())
 {
  strTmp = *iter;
  ReplaseSpecifyString(strTmp,gszSplict,",");
  strTmp += gsChangeLine; //換行
  fwrite(strTmp.c_str(), strTmp.length(),1, fe);
  iter++;

 }

 fclose(fe);
 return 0;
}

void CSVEditor::ReplaseSpecifyString(string & strSource, const string & strFind, const string & strReplase)
{
 string::size_type npos = 0;
 int iReplaseLen = strFind.length();
 int iOffset = strReplase.length();

 while (1)
 {
  npos = strSource.find(strFind, npos);

  if (string::npos != npos)
  {
   strSource.replace(npos,iReplaseLen, strReplase);
   npos += iOffset;
  }
  else
  {
   break;
  } 
 }

}

void CSVEditor::ModifySpecifyString(string & strSource, const string & strFind, const string & strSplict)
{
 if (strSplict.empty())
 {
  //單個字符
  string::size_type pos = strSource.find(strFind);
  if (string::npos != pos)
  {
   strSource.insert(0,"/"");
   strSource.insert(strSource.length(), "/"");
  }

 }
 else
 {
  string::size_type start = 0;
  string::size_type lastEnd = start;
  string::size_type Pos = start;   // ,的位置
  int iOffset = strSplict.length();
  bool bLastCompare = false;

  while(1)
  {
   start = strSource.find(strSplict, lastEnd);

   if (string::npos == start)
   {
    start = strSource.length();
    bLastCompare = true;
   // break;
   }
   

   //檢測是否有,存在
   Pos = strSource.find(strFind, lastEnd);
   if (string::npos == Pos)
   {
    //之後的字符串中沒有,需要檢測的字符串,直接結束對比
    break;
   }
   else
   {
    
    //檢測位置是否在當前的查找範圍內,如果在該範圍則處理,否則進行下一輪對比
    if (Pos <= start)
    {
     strSource.insert(start, "/""); //先加尾巴後加前
     strSource.insert(lastEnd, "/"");
    }
    lastEnd = start;
    lastEnd += iOffset;
    lastEnd += 2;

   }

   if (bLastCompare)
   {
    //完成了所有的查找對比,退出
    break;
   }

 

  }
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章