📄 csvfile.cpp
字号:
// CSVFile.cpp: implementation of the CCSVFile class.
//
//////////////////////////////////////////////////////////////////////
#include "CSVFile.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCSVFile::CCSVFile(char *pStrFileName /* = NULL */)
{
if (pStrFileName == NULL || strlen(pStrFileName) > 511)
{
return;
}
strcpy(this->m_StrFileName,pStrFileName);
LoadCSVFile(pStrFileName);
}
int CCSVFile::GetRow()
{
return m_pRowDatabase.GetSize();
}
int CCSVFile::GetCellInRow(int iRow)
{
char* pActiveRow = (char*)m_pRowDatabase.GetAt(iRow - 1);
UINT uActiveRowLength = strlen(pActiveRow);
UINT uNumberOfQuote = 0;
int uNumberOfCell = 1;
bool check = false;
for (UINT i = 0; i < uActiveRowLength - 1; i++ )
{
if (pActiveRow[i] == '"')
{
uNumberOfQuote++;
}
if ((pActiveRow[i] == ',') && (uNumberOfQuote % 2 == 0))
{
uNumberOfCell++;
}
}
return uNumberOfCell;
}
bool CCSVFile::LoadCSVFile(char *pStrFileName)
{
if (pStrFileName == NULL ||strlen(pStrFileName ) > 511)
{
return false;
}
CFile CSVFile;
BOOL bCheckOpenFile = false;
bCheckOpenFile = CSVFile.Open(pStrFileName,CFile::modeRead);
if (bCheckOpenFile != 1)
{
return false;
}
UINT uSize = CSVFile.GetLength();
char * pBuffer = NULL;
pBuffer = new char[sizeof(char) * uSize];
CSVFile.Read(pBuffer,uSize);
if(pBuffer == NULL)
{
CSVFile.Close();
return false;
}
UINT uNumberOfQuote = 0;
UINT uStartRowIndex = 0;
for (UINT i = 0; i < uSize; i++ )
{
if (pBuffer[i] == '"')
{
uNumberOfQuote ++ ;
}
if (pBuffer[i] == '\n' && uNumberOfQuote % 2 == 0)
{
UINT uRowLength = i - uStartRowIndex;
char * pRowData = new char[uRowLength + 1];
memcpy(pRowData,&pBuffer[uStartRowIndex],uRowLength);
pRowData[uRowLength] = 0;
m_pRowDatabase.Add(pRowData);
uStartRowIndex = i + 1;
}
}
delete[] pBuffer;
pBuffer = NULL;
CSVFile.Close();
return true;
}
int CCSVFile::GetActiveCellLength(int uIndeRow, int uIndexColumn)
{
if (uIndeRow > 65536 || uIndexColumn > 256 || uIndeRow < 1 || uIndexColumn < 1)
{
return -1;
}
int iNumberRows = GetRow();
int iNumberCellInRows = GetCellInRow(uIndeRow);
if (uIndeRow > iNumberRows || uIndexColumn > iNumberCellInRows )
{
m_CurrentCellInfo.cIndex = 0;
m_CurrentCellInfo.Length = 0;
m_CurrentCellInfo.iColumnIndex = uIndexColumn;
m_CurrentCellInfo.iRowIndex = uIndeRow;
return 0;
}
char* pActiveRow = (char*)m_pRowDatabase.GetAt(uIndeRow - 1);
UINT uActiveRowLength = strlen(pActiveRow);
UINT uNumberOfQuote = 0;
UINT uStartCellIndex = 0;
UINT uCellLength = 0;
int uNumberOfCell = 1;
bool check = false;
for (UINT i = 0; i < uActiveRowLength - 1; i++ )
{
if (pActiveRow[i] == '"')
{
uNumberOfQuote++;
}
if ((pActiveRow[i] == ',') && (uNumberOfQuote % 2 == 0))
{
uNumberOfCell++;
}
if ((uIndexColumn == uNumberOfCell) && !check)
{
uStartCellIndex = i;
check = true;
uCellLength = 0;
}
if (uNumberOfCell == (uIndexColumn + 1))
{
break;
}
uCellLength++;
}
if (uStartCellIndex != 0 || (uStartCellIndex == 0 && uIndexColumn == 2) )
{
m_CurrentCellInfo.cIndex = uStartCellIndex + 1;
uCellLength--;
}
else
m_CurrentCellInfo.cIndex = uStartCellIndex;
m_CurrentCellInfo.Length = uCellLength;
m_CurrentCellInfo.iColumnIndex = uIndexColumn;
m_CurrentCellInfo.iRowIndex = uIndeRow;
return uCellLength;
}
bool CCSVFile::SetCellValue(int uIndeRow, int uIndexColumn, char *pValue)
{
if (uIndeRow > 65536 || uIndexColumn > 256 || uIndeRow < 1 || uIndexColumn < 1)
{
return false;
}
int iValueLength = strlen(pValue);
int iNumCell = GetCellInRow(uIndeRow);
char *pValueData = (char*)m_pRowDatabase.GetAt(uIndeRow - 1);
UINT uRowSize = strlen(pValueData);
if (uIndexColumn > iNumCell)
{
UINT uNewRowSize = uRowSize + iValueLength + uIndexColumn - iNumCell;
char *pNewRow = new char[uNewRowSize + 1];
memcpy(pNewRow,pValueData,uRowSize);
for (UINT count = uRowSize; count < uRowSize + uIndexColumn - iNumCell; count++)
{
pNewRow[count] = ',';
}
memcpy(&pNewRow[uRowSize + uIndexColumn - iNumCell], pValue, iValueLength);
pNewRow[uNewRowSize] = 0;
for (int i = 0 ; i < strlen(pNewRow); i++)
{
printf("%c ",pNewRow[i]);
}
m_pRowDatabase.SetAt(uIndeRow - 1,pNewRow);
}
// puts((char*)m_pRowDatabase.GetAt(uIndeRow - 1));
return true;
}
int CCSVFile:: GetActiveCellValue(char *pOutBuffer , int BuffSize)
{
UINT uCellLength = m_CurrentCellInfo.Length;
UINT uStartCellIndex = m_CurrentCellInfo.cIndex;
if (uCellLength > BuffSize)
{
return -1;
}
int iRow = m_CurrentCellInfo.iRowIndex;
char* pRowData = (char*)m_pRowDatabase.GetAt(iRow - 1);
if (pRowData == NULL)
{
return -1;
}
memcpy(pOutBuffer,&pRowData[uStartCellIndex],uCellLength);
return uCellLength;
}
CCSVFile::~CCSVFile()
{
printf("here");
for (int i = 0; i < m_pRowDatabase.GetSize(); i++)
{
if (m_pRowDatabase[i]!=NULL)
delete [] m_pRowDatabase[i];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -