TP:E84BF8F4 基于cocos2d-x,CSV文件读取类。

TP:E84BF8F4 基于cocos2d-x,CSV文件读取类。,第1张

概述自己封装的关于csv文件读取的类,可以读取csv文件,并按照自己的需求转换数据结果。提供字符串转float,int等功能。 头文件 #ifndef __CSV_FILE_H__ #define __CSV_FILE_H__ #include "cocos2d.h" class CSVFile:public cocos2d::Ref { public: CSVFile(); ~CSVFile();

自己封装的关于csv文件读取的类,可以读取csv文件,并按照自己的需求转换数据结果。提供字符串转float,int等功能。


头文件


#ifndef __CSV_file_H__
#define __CSV_file_H__

#include "cocos2d.h"


class CSVfile:public cocos2d::Ref
{
public:
CSVfile();
~CSVfile();
static CSVfile* create(std::string filename);
bool initWithfile(std::string filename);
std::string getData(unsigned int rowIndex,unsigned int colindex);
voID rowSplit(std::vector<std::string> &rows,const std::string &content,const char &rowSeperator);
voID fIEldSplit(std::vector<std::string> &fIElds,std::string line);
//获取带引号的字段
int getFIElDWithQuoted(const std::string &line,std::string& fIEld,int index);


//获取无引号的字段
int getFIEldNoQuoted(const std::string &line,std::string &fIEld,int index);
inline int getColLength() { return m_nLength; }
inline int getRowLength() { return data.size(); }


float getDataOffloat(unsigned int rowIndex,unsigned int colindex);
int getDataOfInt(unsigned int rowIndex,unsigned int colindex);
private:
const std::string m_seperator;
int m_nLength;
std::string _text;
std::vector<std::vector<std::string>> data;
};
#endif


cpp文件

#include "CSVfileUtil.h" USING_NS_CC; CSVfile::CSVfile() :m_seperator(",") { } CSVfile* CSVfile::create(std::string filename) { CSVfile* csv = new CSVfile(); if (csv->initWithfile(filename)) { csv->autorelease(); return csv; } delete(csv); return NulL; } bool CSVfile::initWithfile(std::string filename) { _text = fileUtils::getInstance()->getStringFromfile(filename); std::vector<std::string> line; rowSplit(line,_text,'\n'); for (unsigned int i = 0; i < line.size(); ++i) { std::vector<std::string> fIEldVector; fIEldSplit(fIEldVector,line[i]); data.push_back(fIEldVector); m_nLength = std::max(m_nLength,(int)fIEldVector.size()); } return true; } voID CSVfile::rowSplit(std::vector<std::string> &rows,const char &rowSeperator) { std::string::size_type lastIndex = content.find_first_not_of(rowSeperator,0); std::string::size_type currentIndex = content.find_first_of(rowSeperator,lastIndex); while (std::string::npos != currentIndex || std::string::npos != lastIndex) { rows.push_back(content.substr(lastIndex,currentIndex - lastIndex)); lastIndex = content.find_first_not_of(rowSeperator,currentIndex); currentIndex = content.find_first_of(rowSeperator,lastIndex); } } voID CSVfile::fIEldSplit(std::vector<std::string> &fIElds,std::string line) { if (line[line.length() - 1] == '\r') { line = line.substr(0,line.length() - 1); } std::string fIEld; unsigned int i = 0,j = 0; while (j < line.length()) { if (line[i] == '"') { //有引号 j = getFIElDWithQuoted(line,fIEld,i); } else { j = getFIEldNoQuoted(line,i); } fIElds.push_back(fIEld); i = j + 1; //解析下一个fIEld, +1为了跳过当前的分隔符 } } int CSVfile::getFIElDWithQuoted(const std::string &line,int i) { unsigned int j = 0; fIEld = std::string(); if (line[i] != '"') { return -1; } for (j = i + 1; j < line.length() - 1; ++j) { if (line[j] != '"') { //当前char不为引号,则是fIEld内容(包括逗号) fIEld += line[j]; } else { //遇到fIEld结束时的引号,可以返回 return j; break; } } if (j == line.length()) { } return j; } int CSVfile::getFIEldNoQuoted(const std::string &line,int index) { unsigned int j = 0; //找到下一个分隔符位置 j = line.find_first_of(m_seperator,index); if (j > line.length()) { j = line.length(); } fIEld = std::string(line,index,j - index); return j; } ///////search data std::string CSVfile::getData(unsigned int rowIndex,unsigned int colindex) { rowIndex--; colindex--; if (rowIndex >= getRowLength() || colindex >= getColLength()) { return ""; } if (colindex >= data[rowIndex].size()) { return ""; } return data[rowIndex][colindex]; } float CSVfile::getDataOffloat(unsigned int rowIndex,unsigned int colindex) { return atof(getData(rowIndex,colindex).c_str()); } intCSVfile::getDataOfInt(unsigned int rowIndex,unsigned int colindex) { return atoi(getData(rowIndex,colindex).c_str()); } CSVfile::~CSVfile() { _text.clear(); }

总结

以上是内存溢出为你收集整理的TP:E84BF8F4 基于cocos2d-x,CSV文件读取类。全部内容,希望文章能够帮你解决TP:E84BF8F4 基于cocos2d-x,CSV文件读取类。所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1060972.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存