coco2d-x 读取csv数据表

前端之家收集整理的这篇文章主要介绍了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);
	}
}

猜你在找的Cocos2d-x相关文章