#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;
}
}
}
}