cocos2d-x 读取CSV文件,读取本地Excel配置表的方法

cocos2d-x 读取CSV文件,读取本地Excel配置表的方法,第1张

概述//CSVReader.h #define MAP_LINE std::map<std::string, std::string> //key为首行字符串, value为此列字符串#define MAP_CONTENT std::map<int, MAP_LINE> //key为code, value为一行map#define VEC_MAP std::vector<std::pa

//CSVReader.h


#define MAP_liNE std::map<std::string,std::string>			//key为首行字符串,value为此列字符串#define MAP_CONTENT std::map<int,MAP_liNE>				//key为code,value为一行map#define VEC_MAP  std::vector<std::pair<std::string,int>>//csv文件读取器class CSVReader{public:	CSVReader();	static CSVReader *getInst();	//获取实例	//解析csv. filename:csv文件名,voID parse(const char *filename);	//获取内容map. filename:文件名	const MAP_CONTENT &getContentMap(std::string filename);	//获取一行map.filename:文件名, code一行code	const MAP_liNE &getlineMap(std::string filename,int code);	//获取某行某列的值	const std::string  &getByCode(std::string filename,int code,const std::string &key);private:	//读取csv的一行.line:一行的内容	voID readCSVline(const char *line,int index);	 VEC_MAP m_firstVector;											//第一行的vector	 MAP_CONTENT m_contentMap;									//内容map	 std::map<std::string,MAP_CONTENT> m_fileMap;		//文件map	 static CSVReader *m_inst;		//实例};


//CSVReader.cpp

CSVReader *CSVReader::m_inst = NulL;//构造函数CSVReader::CSVReader(){	m_firstVector.clear();	m_contentMap.clear();	m_fileMap.clear();}//获取实例CSVReader *CSVReader::getInst()	{	if(!m_inst)	{m_inst = new CSVReader();}	return m_inst;}//获取内容map. filename:文件名const MAP_CONTENT &CSVReader::getContentMap(std::string filename){	return m_fileMap.find(filename)->second;}//获取一行map.filename:文件名, code一行codeconst MAP_liNE &CSVReader::getlineMap(std::string filename,int code){	return getContentMap(filename).find(code)->second;}//获取某行某列的值const std::string  &CSVReader::getByCode(std::string filename,const std::string &key){	return getlineMap(filename,code).find(key)->second;}//解析csv. filename:csv文件名,voID CSVReader::parse(const char *filename){	m_contentMap.clear();		//首先进行清理	std::string path = filename;	unsigned long size;	const char *data = (const char*)(cocos2d::CCfileUtils::sharedfileUtils()->getfileData(path.c_str(),"r",&size));	CCAssert(data != NulL,"file is not exist.");	if (data == NulL)		return;			char line[32768];	//一行最多字节数	const char *src = data;	if (size == 0)		size = strlen(src);	char *pl = line;		//指向line数组的指针	int index = 0;	bool skip = false;	//若一行为空,skip则标记为true	while (data - src < size)	{		//读取到一行的末尾		if (*data == '\n' && !skip)		{			*pl = '//用法如下';			readCSVline(line,index);			++index;			pl = line;		}		else if (*data == '\r')		{}		else		{			//任何一个字段能留空			if (*data == '"')				skip = !skip;			*pl = *data;			++pl;		}		++data;	}	*pl = '
bool HelloWorldScene::init(){    //////////////////////////////	CSVReader::getInst()->parse("haha.csv");	MAP_liNE map_line = CSVReader::getInst()->getlineMap("haha.csv",1000000);	MAP_liNE::iterator it = map_line.begin();	while (it != map_line.end())	{		cclog("key:%s,value:%s",it->first.c_str(),it->second.c_str());		it++;	}}
'; //添加到map m_fileMap.insert(std::map<std::string,MAP_CONTENT>::value_type(filename,m_contentMap));}//读取csv的一行.line:一行的内容voID CSVReader::readCSVline(const char *line,int index){ char value[32768]; //一行最多字节数 if (*line == '//csv表格数据如下图') return; char *pv[32]; char *tv = value; bool skip = false; int count = 0; *tv = ''; pv[count++] = tv; while (*line != '') { if (*line == ',' && !skip) { *tv = ''; ++tv; pv[count++] = tv; } else if (*line == '"') { skip = !skip; } else { *tv = *line; ++tv; } ++line; } *tv = ''; //临时数组 std::vector<std::pair<std::string,int> > tVector; for (int i=0; i<count; ++i) {tVector.push_back(std::map<std::string,int>::value_type(pv[i],i));} //第一行作为key if(index == 0) {m_firstVector = tVector;} //第2行为注释 else if(index > 1) { //一行的map std::map<string,string> tmp; for (int i = 0; i < m_firstVector.size(); i++) {tmp.insert(std::map<string,string>::value_type(m_firstVector[i].first,tVector[i].first));} m_contentMap.insert(std::map<int,std::map<string,string>>::value_type(atoi(tVector[0].first.c_str()),tmp)); }}





总结

以上是内存溢出为你收集整理的cocos2d-x 读取CSV文件,读取本地Excel配置的方法全部内容,希望文章能够帮你解决cocos2d-x 读取CSV文件,读取本地Excel配置表的方法所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存