// // CCSVParse.cpp // CPPAlgorithm // // Created by xujw on 16/2/26. // Copyright © 2016年 xujw. All rights reserved. // #include "CCSVParse.h" #include CCSVParse::CCSVParse():_useSimpleModel(false){} CCSVParse::~CCSVParse(){} std::vector CCSVParse::splitString(const std::string &str, const std::string &separator) { std::vector resVec; if ("" == str) { return resVec; } //方便截取最后一段数据 std::string dataStr = str + separator; size_t pos = dataStr.find(separator); size_t size = dataStr.size(); while (pos != std::string::npos) { std::string x = dataStr.substr(0,pos); resVec.push_back(x); dataStr = dataStr.substr(pos+1,size); pos = dataStr.find(separator); } return resVec; // //Method 2 // size_t nStartPosFound = str.find(separator, 0); // size_t nFieldStart = 0; // for (; nStartPosFound != -1; nStartPosFound = str.find(separator, nStartPosFound)) // { // std::string strSub = str.substr(nFieldStart, nStartPosFound - nFieldStart); // nStartPosFound = nStartPosFound + separator.size(); // nFieldStart = nStartPosFound; // // // resVec.push_back(strSub); // } // // // 加入最后一个字段 // if (nFieldStart < str.size()) // { // std::string strSub = str.substr(nFieldStart, str.size() - nFieldStart); // resVec.push_back(strSub); // } // return resVec; } std::string CCSVParse::loadCsvFile(const std::string &fileName) { FILE *pFile = fopen(fileName.c_str(), "rb"); if (0 == pFile) { return ""; } fseek(pFile, 0, SEEK_END); //指针移动到文件结尾 long len = ftell(pFile); //获取文件大小 char *pBuffer = new char[len+1]; fseek(pFile, 0, SEEK_SET); //指针移动到文件开头 fread(pBuffer, 1, len, pFile); //读取文件 fclose(pFile); //等价于std::string s;s.assign(pBuffer,len); pBuffer[len] = 0; std::string strRead(pBuffer,len); delete [] pBuffer; return strRead; } std::vector> CCSVParse::parseCsvFile(const std::string &fileName,const std::string &separator) { _gridData.clear(); std::string strAllData = loadCsvFile(fileName); if (strAllData.size() == 0) { return _gridData; } //分隔符只能是一个字符 assert(separator.size() == 1); //简易模式,字段里面不能包含分隔符 if (_useSimpleModel) { //分出行和字段 std::vector ret = splitString(strAllData, "\n"); for (size_t i=0; i rowData = splitString(ret.at(i), separator); _gridData.push_back(rowData); } return _gridData; } //标准模式,字段里面可以包含分隔符 //定义状态 typedef enum stateType { kNewFieldStart = 0, //新字段开始 kNonQuotesField, //非引号字段 kQuotesField, //引号字段 kFieldSeparator, //字段分隔 kQuoteInQuotesField, //引号字段中的引号 kBackSlash, //转义符号 kBackSlashInQuotesField, //引号字段中的转义符号 kRowSeparator, //行分隔符(回车) kError //语法错误 }StateType; //分出行 std::vector vecRows = splitString(strAllData, "\n"); for (int i=0; i vecFields; std::string strField; StateType state = kNewFieldStart; for (int j=0; j rowData = _gridData.at(row); for (int col = 0; col > CCSVParse::parseCsvFileToMap(const std::string &fileName,const std::string &separator) { //先获取所有的行列数据 std::vector> allData = parseCsvFile(fileName,separator); //转为字典形式 std::map > mapAllData; for (size_t i=1; i rowData = allData.at(i); //数据第一行为数据类型key std::vector keyData = allData.at(0); std::map mapRow; for (int i=0; i>res; return res; } float conToFloat(std::string &source) { std::stringstream ss; float res; ss<>res; return res; } double conToDouble(std::string &source) { std::stringstream ss; double res; ss<>res; return res; } std::string conToString(int s) { std::stringstream ss; std::string res; ss<>res; return res; } std::string conToString(float s) { std::stringstream ss; std::string res; ss<>res; return res; } std::string conToString(double s) { std::stringstream ss; std::string res; ss<>res; return res; }