cocos2dx 3.x 读取csv

前端之家收集整理的这篇文章主要介绍了cocos2dx 3.x 读取csv前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#ifndef _PARSE_CSV_H_
#define _PARSE_CSV_H_

#include <fstream>
#include <sstream>
#include <map>
#include <list>
#include <vector>
using namespace std;

struct CsvData
{
	string FileName;
	map< string,map< string,string > > TabData;
	CsvData()
	{
		FileName = "";
		TabData.clear();
	}

	string GetData( string key,string head )
	{//获取数据
		return TabData[key][head];
	}

	void PushData( string key,string > data )
	{//压入数据
		TabData[key] = data;//保存整行的数据
	}

	void SetFileName( string fileName )
	{//设置文件名
		FileName = fileName;
	}
};

class ParseCsv
{
private:
	static ParseCsv* m_pThis;
public:
	/**
	*	@brief 单例
	*/
	static ParseCsv* Instance()
	{
		if (!m_pThis)
		{
			m_pThis = new ParseCsv();
		}
		return m_pThis;
	}
	/**
	*	@brief 构造函数
	*/
	ParseCsv(void);
	/**
	*	@brief 析构函数
	*/
	~ParseCsv(void);
	/**
	*	@brief 加载CSV文件
	*/
	bool LoadCsv( const char* fileName,const char* filePath )
	{
		return _loadCsv( fileName,filePath );
	}
	/**
	*	@brief 获取数据
	*/
	string GetData( string fileName,string key,string head );
	string GetData( string fileName,int key,string head );
	/**
		@brief 分割字符串
	*/
	vector<string> SplitString( const string &strContext,string strSpilt );
private:
	bool _loadCsv( const char* fileName,const char* filePath );
private:
	map< string,CsvData > m_AllDatas;
};

#endif
<pre name="code" class="cpp">#include "ParseCsv.h"
#include "extensions\cocos-ext.h"
#include "cocos2d.h"
USING_NS_CC;

ParseCsv *ParseCsv::m_pThis = NULL;
ParseCsv::ParseCsv(void)
{
	m_AllDatas.clear();
}


ParseCsv::~ParseCsv(void)
{
}

bool ParseCsv::_loadCsv( const char* fileName,const char* filePath )
{//加载CSV文件
	//调用cocos2dx工具类读出数据
	string path = FileUtils::getInstance()->fullPathForFilename( filePath );
	ssize_t bufferSize = 0;
	char* buf = (char*)FileUtils::getInstance()->getFileData(path.c_str(),"r",&bufferSize);
	ssize_t size = bufferSize;

	//新建Csv结构体
	CsvData data;
	data.SetFileName(fileName);

	//把整个表按行分割
	string strbuff;
	ssize_t count = 0;
	do
	{
		if( count >= size ) break;
		strbuff += buf[ count++ ];
	}while( true );

	vector<string> vecTab = this->SplitString( strbuff,"\r\n" );

	//读取表头
	vector<string>::iterator iterTab = vecTab.begin();
	if ( iterTab == vecTab.end() ) return false;
	vector<string> vecHeadLine = this->SplitString( *iterTab,"\t" );
	list<string> head;//表头
	for (vector<string>::iterator iterLine = vecHeadLine.begin(); iterLine != vecHeadLine.end(); iterLine++)
	{
		head.push_back( *iterLine );
	}
	
	//每行按"\t"分割 读出所有数据
	iterTab++;
	for ( ; iterTab != vecTab.end(); iterTab++  )
	{//读取表里数据
		map<string,string> tabLine;
		vector<string> vecLine = this->SplitString( *iterTab,"\t" );
		list<string>::iterator iterHead = head.begin();
		vector<string>::iterator iterLine = vecLine.begin();
		for ( ; iterLine != vecLine.end(); iterLine++ )
		{
			tabLine[*iterHead] = *iterLine;
			iterHead++;
			if ( iterHead == head.end() ) break;
		}
		data.PushData( tabLine[*head.begin()],tabLine );
	}
	m_AllDatas[fileName] = data;
	return true;
}

string ParseCsv::GetData( string fileName,string head )
{//获取数据
 	return m_AllDatas[fileName].GetData( key,head );
}

string ParseCsv::GetData( string fileName,string head )
{//获取数据
	char sz[32] = {0};
	sprintf( sz,"%d",key );
	return this->GetData( fileName,sz,head );
}

vector<string> ParseCsv::SplitString( const string &strContext,string strSpilt )
{//分割字符串
	vector<string> vecStr;
	int charLen = strSpilt.size();//分隔符的长度
	int lastPos = 0;
	int index = -1;
	while ( -1 != ( index = strContext.find( strSpilt,lastPos ) ) )
	{
		vecStr.push_back( strContext.substr( lastPos,index - lastPos ) );
		lastPos = index + charLen;
	}
	string lastString = strContext.substr( lastPos );//截取最后一个分隔符后的内容
	if ( !lastString.empty() )
	{//不为空也压进去
		vecStr.push_back( lastString );
	}
	return vecStr;
}


 

原文链接:https://www.f2er.com/cocos2dx/343619.html

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