1 .CReadPFfromCSV.h
#pragma once
#include <assert.h>
#include <map>
#include <vector>
#include <string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include <string>
#include <cstring>
#include <sstream>
using namespace std;
typedef unsigned long u32;
class CReadPFfromCSV
{
private:
map<u32, map<u32, string>> m_stringMap;
string m_CSVName;
public:
CReadPFfromCSV(void);
CReadPFfromCSV(const char *path)
{
assert(LoadCSV(path));
}
bool LoadCSV(string path);
bool Loadthrmldata(const char *path);
bool SaveCSV(const char *path = NULL);
bool GetIntValue(u32 uiRow, u32 uiCol, int &riValue);
bool GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue);
string* GetStringValue(u32 uiRow, u32 uiCol);
int GetParamFromString(string str, vector<string> &stringVec, char delim = ',');
int GetParamFromStringb(string str, vector<string> &stringVec, char delim = ' ');
map<u32, map<u32, string>>& GetCSVMap()
{
return m_stringMap;
}
~CReadPFfromCSV(void);
};
2. CReadPFfromCSV.cpp
#include "stdafx.h"
#include "ReadPFfromCSV.h"
bool CReadPFfromCSV::LoadCSV(string strpath)
{
const char *path = strpath.c_str();
FILE *pFile;
int err = fopen_s(&pFile,path, "r");
if (pFile)
{
ifstream in(path, ios::in);
istreambuf_iterator<char> beg(in), end;
string strdata(beg, end);
in.close();
const char*cfirst = strdata.c_str();
char*fileBuffer = new char[strlen(cfirst) + 1];
strcpy( fileBuffer, cfirst);
map<u32, string> stringMap;
u32 uiIndex = 1;
char *pBegin = fileBuffer;
char *pEnd = strchr(pBegin, '\n');
pBegin = pEnd + 1;
pEnd = strchr(pBegin, '\n');
pBegin = pEnd + 1;
pEnd = strchr(pBegin, '\n');
while (pEnd)
{
string strTemp;
strTemp.insert(0, pBegin, pEnd-pBegin);
assert(!strTemp.empty());
stringMap[uiIndex++] = strTemp;
pBegin = pEnd + 1;
pEnd = strchr(pBegin, '\n');
}
delete []fileBuffer;
fileBuffer = NULL;
pBegin = NULL;
pEnd = NULL;
map<u32, string>::iterator iter = stringMap.begin();
for (; iter != stringMap.end(); ++iter)
{
vector<string> stringVec;
map<u32, string> stringMapTemp;
assert(GetParamFromString(iter->second, stringVec) > 0);
vector<string>::size_type idx = 0;
for (; idx != stringVec.size(); ++idx)
{
stringMapTemp[idx + 1] = stringVec[idx];
}
m_stringMap[iter->first] = stringMapTemp;
}
fclose(pFile);
m_CSVName = path;
return true;
}
else
{
return false;
}
}
bool CReadPFfromCSV::Loadthrmldata(const char *path)
{
FILE *pFile = fopen(path, "r");
if (pFile == NULL)
{
cout<<"violmt.dat is not found."<<endl;
}
if (pFile)
{
ifstream in(path, ios::in);
istreambuf_iterator<char> beg(in), end;
string strdata(beg, end);
in.close();
const char*cfirst = strdata.c_str();
char*fileBuffer = new char[strlen(cfirst) + 1];
strcpy( fileBuffer, cfirst);
map<u32, string> stringMap;
u32 uiIndex = 1;
char *pBegin = fileBuffer;
char *pEnd = strchr(pBegin, '\n');
while (pEnd)
{
string strTemp;
strTemp.insert(0, pBegin, pEnd-pBegin);
pBegin = pEnd + 1;
pEnd = strchr(pBegin, '\n');
if (strTemp.size()<8)
{
break;
}
}
while (pEnd)
{
string strTemp;
strTemp.insert(0, pBegin, pEnd-pBegin);
assert(!strTemp.empty());
stringMap[uiIndex++] = strTemp;
pBegin = pEnd + 1;
pEnd = strchr(pBegin, '\n');
}
delete []fileBuffer;
fileBuffer = NULL;
pBegin = NULL;
pEnd = NULL;
map<u32, string>::iterator iter = stringMap.begin();
for (; iter != stringMap.end(); ++iter)
{
vector<string> stringVec;
map<u32, string> stringMapTemp;
GetParamFromStringb(iter->second, stringVec,' ');
vector<string>::size_type idx = 0;
for (; idx != stringVec.size(); ++idx)
{
stringMapTemp[idx + 1] = stringVec[idx];
}
m_stringMap[iter->first] = stringMapTemp;
}
fclose(pFile);
m_CSVName = path;
return true;
}
else
{
return false;
}
}
bool CReadPFfromCSV::SaveCSV(const char *path )
{
if (path != NULL)
{
m_CSVName = path;
}
FILE *pFile = fopen(m_CSVName.c_str(), "w");
if (pFile)
{
map<u32, map<u32, string>>::iterator iter = m_stringMap.begin();
for (; iter != m_stringMap.end(); ++iter)
{
map<u32, string> &rStringMap = iter->second;
map<u32, string>::iterator it = rStringMap.begin();
for (; it != rStringMap.end(); ++it)
{
string strTemp = it->second;
strTemp += ',';
fwrite(strTemp.c_str(), 1, 1, pFile);
}
char delim = '\n';
fwrite(&delim, 1, 1, pFile);
}
fclose(pFile);
return true;
}
else
{
return false;
}
}
bool CReadPFfromCSV::GetIntValue(u32 uiRow, u32 uiCol, int &riValue)
{
string *pStr = GetStringValue(uiRow, uiCol);
if (pStr)
{
riValue = atoi(pStr->c_str());
return true;
}
else
{
return false;
}
}
bool CReadPFfromCSV::GetFloatValue(u32 uiRow, u32 uiCol, float &rfValue)
{
string *pStr = GetStringValue(uiRow, uiCol);
if (pStr)
{
rfValue = atof(pStr->c_str());
return true;
}
else
{
return false;
}
}
string* CReadPFfromCSV::GetStringValue(u32 uiRow, u32 uiCol)
{
map<u32, map<u32, string>>::iterator iter = m_stringMap.find(uiRow);
if (iter != m_stringMap.end())
{
map<u32, string> &rStrMap = iter->second;
map<u32, string>::iterator it = rStrMap.find(uiCol);
if (it != rStrMap.end())
{
return &(it->second);
}
else
{
return NULL;
}
}
else
{
return NULL;
}
}
int CReadPFfromCSV::GetParamFromString(string str, vector<string> &stringVec, char delim)
{
char *token = strtok(const_cast<char *>(str.c_str()), &delim);
while (token)
{
string strTemp = token;
stringVec.push_back(strTemp);
token = strtok(NULL, &delim);
}
return stringVec.size();
}
int CReadPFfromCSV::GetParamFromStringb(string str, vector<string> &stringVec, char delim)
{
char *token = strtok(const_cast<char *>(str.c_str()), &delim);
while (token)
{
string strTemp = token;
stringVec.push_back(strTemp);
token = strtok(NULL, &delim);
}
return stringVec.size();
}
CReadPFfromCSV::CReadPFfromCSV(void)
{
}
CReadPFfromCSV::~CReadPFfromCSV(void)
{
}