coco2d-x 读取csv数据表

看到网上有很多关于读取csv文章,但是基本上都是转载一个人的,而且都有问题,明显的不能输出截取图片那样的结果,经过自己的研究,修改了一下就可以输出了。

直接上代码

h部分

#ifndef __MGGAnalysisCSV_h__
#define __MGGAnalysisCSV_h__

#include "cocos2d.h"

USING_NS_CC;
using namespace std;

class MGGAnalysisCSV
{
public:
	MGGAnalysisCSV(istream& fin = cin,string sep = ",") : fieldsep(sep),cols(0) {}
	~MGGAnalysisCSV(void);
	static MGGAnalysisCSV& getInstance()
	{
		static MGGAnalysisCSV INSTACNE;
		return INSTACNE;
	}

	//用以存储数据
	vector<vector<std::string>> data;

private:
	string fieldsep;
	int cols;

	void StringSplit(const string& str,vector<string>& tokens,const char& delimiters);
	void split(vector<string>& field,string line);
	int advplain(const string& line,string& fld,int);
	int advquoted(const string& line,int);

public:
	bool openFile(const char* fileName);
	const char* getData(unsigned int rows,unsigned int cols);
	int findColsData(int cols,const char* value);

	inline int getCols() {return cols;}
	inline int getRows() {return data.size();}
};
#endif
c部分
#include "MGGAnalysisCSV.h"

MGGAnalysisCSV::~MGGAnalysisCSV(void)
{
	for (int i=0; i<data.size(); i++)
		data[i].clear();

	data.clear();
}

//解析 CVS 文件
bool MGGAnalysisCSV::openFile(const char* fileName)
{
	string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
	unsigned char* pBuffer = nullptr;
	unsigned long bufferSize = 0;
	pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(),"r",&bufferSize);

	string s = (char*)pBuffer;
	string str = s.substr(0,bufferSize);

	vector<string> line;
	StringSplit(str,line,'\n');

	for(unsigned int i=0; i<line.size(); ++i)
	{
		vector<string> field;
		split(field,line[i]);
		data.push_back(field);
	}
	cols = max(cols,(int)data.size());

	return true;
}

void MGGAnalysisCSV::StringSplit(const string& str,const char& delimiters)
{
	string::size_type lastPos = str.find_first_not_of(delimiters,0);
	string::size_type pos = str.find_first_of(delimiters,lastPos);
	while (string::npos != pos || string::npos != lastPos)
	{
		tokens.push_back(str.substr(lastPos,pos-lastPos));
		lastPos = str.find_first_not_of(delimiters,pos);
		pos = str.find_first_of(delimiters,lastPos);
	}
}

void MGGAnalysisCSV::split(vector<string>& field,string line)
{
	string fld;
	unsigned int i,j=0;

	if(line.length() ==0)
		return;
	i = 0;

	do
	{
		if(j < line.length() && line[i] == '"')
			j = advquoted(line,fld,++i);
		else
			j = advplain(line,i);

		field.push_back(fld);
		i = j + 1;
	}
	while (j < line.length());
}

int MGGAnalysisCSV::advplain(const string& s,int i)
{
	unsigned int j;
	j = s.find_first_of(fieldsep,i);
	if(j > s.length())
		j = s.length();
	fld = string(s,i,j - i);
	return j;
}

int MGGAnalysisCSV::advquoted(const string& s,int i)
{
	unsigned int j;
	fld = "";
	for (j=i; j<s.length(); ++j)
	{
		if(s[j] == '"' && s[++j] != '"')
		{
			unsigned int k = s.find_first_of(fieldsep,j);
			if(k > s.length())
				k = s.length();
			for(k-=j; k-->0; )
				fld += s[j++];
			break;
		}
		fld += s[j];
	}
	return j;
}

//获取指定行列的数据
const char* MGGAnalysisCSV::getData(unsigned int rows,unsigned int cols)
{
	if (rows<0 || rows>=data.size() || cols<0 || cols>=data[rows].size())
	{
		return "";
	}
	return data[rows][cols].c_str();
}

//获取指定数据的列下标
int MGGAnalysisCSV::findColsData(int cols,const char* value)
{
	for (unsigned int i=0; i<data.size(); i++)
	{
		if(strcmp(getData(i,cols),value) == 0)
			return i;
	}
	return -1;
}

这部分是在需要调用的地方写,首先要包含头文件
#include "MGGAnalysisCSV.h"

MGGAnalysisCSV::getInstance().openFile("config/quest.csv");

for (int i=0; i<MGGAnalysisCSV::getInstance().getCols(); i++)
{
	string strLine = "";
	for (int j=0; j<MGGAnalysisCSV::getInstance().getRows(); j++)
	{
		strLine = MGGAnalysisCSV::getInstance().getData(i,j);
		cclabelTTF* pLabel = cclabelTTF::create(strLine.c_str(),"Thonburi",18);
		pLabel->setPosition(ccp(visibleSize.width * 0.5,visibleSize.height- 90 - i * 30));
		pLabel->setColor(ccc3(255,0));
		this->addChild(pLabel,100);
	}
}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...