使用方法:
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCLabelTTF *pLabel=CCLabelTTF::create("CSV 文件数据读取", "Thonburi", 28);
CCSize size=CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width*0.5, size.height-20));
this->addChild(pLabel,1);
HCSVFile *csvFile=new HCSVFile();
csvFile->openFile("test.csv");
for (int i=0; i<csvFile->getCols(); i++) {
string strLine="";
for (int j=0; j<csvFile->getRows(); j++) {
strLine+=csvFile->getData(i,j);
strLine+=",";
}
CCLabelTTF *pLabel=CCLabelTTF::create(strLine.c_str(), "Thonburi", 18);
CCSize size=CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width*0.5, size.height-90-i*30));
this->addChild(pLabel,1);
}
return true;
}
//
// HCSVFile.h
// CsvParseTest
//
// Created by Himi on 12-10-15.
//
//
#ifndef _CSV_FILE_
#define _CSV_FILE_
#include "cocos2d.h"
using namespace std;
class HCSVFile {
public:
HCSVFile(istream& fin = cin, string sep = ","):
fieldsep(sep),
cols(0){}
~HCSVFile();
//用以存储数据
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
//
// HCSVFile.cpp
// CsvParseTest
//
// Created by Himi on 12-10-15.
//
//
#include "HCSVFile.h"
using namespace cocos2d;
//获取指定行列的数据
const char* HCSVFile::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 HCSVFile::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 HCSVFile::openFile(const char* fileName)
{
string pathKey = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(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 HCSVFile::StringSplit( const 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 HCSVFile::split(vector<string>& field,string line)
{
string fld;
int i, j;
if (line.length() == 0)
return ;
i = 0;
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 HCSVFile::advquoted(const string& s, string& fld, int i)
{
int j;
fld = "";
for (j = i; j < s.length(); j++)
{
if (s[j] == '"' && s[++j] != '"')
{
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;
}
int HCSVFile::advplain(const string& s, string& fld, int i)
{
int j;
j = s.find_first_of(fieldsep, i);
if (j > s.length())
j = s.length();
fld = string(s, i, j-i);
return j;
}
//析构函数,释放内存
HCSVFile::~HCSVFile()
{
for (int i=0; i<data.size(); i++) {
data[i].clear();
}
data.clear();
}