cocos2dx中读取数据文件可能有很多种,像读取xml,lua,csv,json等,这些都可以作为配置数据的格式。
最近用到了读取csv数据文件,所以在网上找了一下关于这方面的技术博客。果然,网上各路大神都是不吝啬的,
不说废话了,直接上代码。代码如下(测试通过,可读取数据):
.h头文件
//
// QLCSVFile.h
//
// Created by quasi_lee on 15-7-7.
//
//
#ifndef __QLCSVFile__
#define __QLCSVFile__
#include <stdio.h>
#include "cocos2d.h"
USING_NS_CC;
using namespace std;
class QLCSVFile {
public:
QLCSVFile();
~QLCSVFile();
//用以存储数据
vector<vector<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,string& fld,int);
public:
//打开CSV文件
bool openFile(const char*fileName);
//根据行列获取数据
const char* getData(int rows,int cols);
//获取指定数据的列下标
int findColsData(int cols,const char* value);
//得到总列数
inline int getCols(){return cols;}
//得到总行数
inline int getRows(){return data.size();}
};
#endif /* defined(__QLCSVFile__) */
.cpp文件
//
// QLCSVFile.cpp
//
// Created by quasi_lee on 15-7-7.
//
//
#include "QLCSVFile.h"
QLCSVFile::QLCSVFile()
: fieldsep(",")
, cols(0)
{
}
//获取指定行列的数据
const char* QLCSVFile::getData(int rows, int cols)
{
if(rows < 0
|| rows >= data.size()
|| cols < 0
|| cols >= data[rows].size())
{
return "";
}
return data[rows][cols].c_str();
}
//获取指定数据的列下标
int QLCSVFile::findColsData(int cols, const char *value)
{
for(int i = 0; i < data.size(); i++)
{
if(strcmp(getData(i, cols), value) == 0)
{
return i;
}
}
return -1;
}
//解析CSV文件
bool QLCSVFile::openFile(const char *fileName)
{
string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
unsigned char* pBuffer = NULL;
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(int i = 0; i < line.size(); i++)
{
vector<string> field;
split(field, line[i]);
data.push_back(field);
cols = max(cols, (int)field.size());
}
return true;
}
void QLCSVFile::StringSplit(const std::string &str, vector<string> &tokens, 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 QLCSVFile::split(vector<string>& field,string line)
{
string fld;
int i = 0;
int j = 0;
if(line.length() == 0)
{
return;
}
do {
if(i < line.length()
&& line[i] == '"')
{
j = advquoted(line, fld, ++i);
}
else
{
j = advplain(line, fld, i);
}
field.push_back(fld);
i = j + 1;
} while (j < line.length());
}
int QLCSVFile::advquoted(const string &line, string &fld, int i)
{
int j = 0;
fld = "";
for(j = i; j < line.length(); j++)
{
if(line[j] == '"'
&& line[++j] != '"')
{
int k = line.find_first_of(fieldsep, j);
if(k > line.length())
{
k = line.length();
}
for(k -= j; k-- > 0; )
{
fld += line[j++];
}
break;
}
fld += line[j];
}
return j;
}
int QLCSVFile::advplain(const string &line, string &fld, int i)
{
int j = 0;
j = line.find_first_of(fieldsep, i);
if(j > line.length())
{
j = line.length();
}
fld = string(line, i, j - i);
return j;
}
//析构函数,释放内存
QLCSVFile::~QLCSVFile()
{
for(int i = 0; i < data.size(); i++)
{
data[i].clear();
}
data.clear();
}