展会信息港展会大全

cocos2d-x 读取CSV文件,读取本地Excel配置表的方法
来源:互联网   发布日期:2015-09-28 11:08:38   浏览:4779次  

导读: //CSVReader.h #define MAP_LINE std::map<:string :string="\">//key为首行字符串, value为此列字符串 #defi...

//CSVReader.h

#define MAP_LINE std::map<:string :string=""> //key为首行字符串, value为此列字符串

#define MAP_CONTENT std::map //key为code, value为一行map

#define VEC_MAP std::vector<:pair int="">>

//csv文件读取器

class CSVReader

{

public:

CSVReader();

static CSVReader *getInst(); //获取实例

//解析csv. fileName:csv文件名,

void parse(const char *fileName);

//获取内容map. filename:文件名

const MAP_CONTENT &getContentMap(std::string filename);

//获取一行map.filename:文件名, code一行code

const MAP_LINE &getLineMap(std::string filename, int code);

//获取某行某列的值

const std::string &getByCode(std::string filename, int code, const std::string &key);

private:

//读取csv的一行.line:一行的内容

void readCSVLine(const char *line, int index);

VEC_MAP m_firstVector; //第一行的vector

MAP_CONTENT m_contentMap; //内容map

std::map<:string map_content=""> m_fileMap; //文件map

static CSVReader *m_inst; //实例

};,><:string>,>,>

//CSVReader.cpp

CSVReader *CSVReader::m_inst = NULL;

//构造函数

CSVReader::CSVReader()

{

m_firstVector.clear();

m_contentMap.clear();

m_fileMap.clear();

}

//获取实例

CSVReader *CSVReader::getInst()

{

if(!m_inst)

{m_inst = new CSVReader();}

return m_inst;

}

//获取内容map. filename:文件名

const MAP_CONTENT &CSVReader::getContentMap(std::string filename)

{

return m_fileMap.find(filename)->second;

}

//获取一行map.filename:文件名, code一行code

const MAP_LINE &CSVReader::getLineMap(std::string filename, int code)

{

return getContentMap(filename).find(code)->second;

}

//获取某行某列的值

const std::string &CSVReader::getByCode(std::string filename, int code, const std::string &key)

{

return getLineMap(filename, code).find(key)->second;

}

//解析csv. fileName:csv文件名,

void CSVReader::parse(const char *fileName)

{

m_contentMap.clear(); //首先进行清理

std::string path = fileName;

unsigned long size;

const char *data = (const char*)(cocos2d::CCFileUtils::sharedFileUtils()->getFileData(path.c_str(),"r" , &size));

CCAssert(data != NULL, "File is not exist.");

if (data == NULL)

return;

char line[32768]; //一行最多字节数

const char *src = data;

if (size == 0)

size = strlen(src);

char *pl = line; //指向line数组的指针

int index = 0;

bool skip = false; //若一行为空,skip则标记为true

while (data - src < size)

{

//读取到一行的末尾

if (*data == '\n' && !skip)

{

*pl = '\0';

readCSVLine(line, index);

++index;

pl = line;

}

else if (*data == '\r')

{}

else

{

//任何一个字段能留空

if (*data == '"')

skip = !skip;

*pl = *data;

++pl;

}

++data;

}

*pl = '\0';

//添加到map

m_fileMap.insert(std::map<:string map_content="">::value_type(fileName, m_contentMap));

}

//读取csv的一行.line:一行的内容

void CSVReader::readCSVLine(const char *line, int index)

{

char value[32768]; //一行最多字节数

if (*line == '\0')

return;

char *pv[32];

char *tv = value;

bool skip = false;

int count = 0;

*tv = '\0';

pv[count++] = tv;

while (*line != '\0')

{

if (*line == ',' && !skip)

{

*tv = '\0';

++tv;

pv[count++] = tv;

}

else if (*line == '"')

{

skip = !skip;

}

else

{

*tv = *line;

++tv;

}

++line;

}

*tv = '\0';

//临时数组

std::vector<:pair int=""> > tVector;

for (int i=0; i::value_type(pv[i], i));}

//第一行作为key

if(index == 0)

{m_firstVector = tVector;}

//第2行为注释

else if(index > 1)

{

//一行的map

std::map tmp;

for (int i = 0; i < m_firstVector.size(); i++)

{tmp.insert(std::map::value_type(m_firstVector[i].first, tVector[i].first));}

m_contentMap.insert(std::map>::value_type(atoi(tVector[0].first.c_str()), tmp));

}

}

,>,>,>;><:string>,>

//用法如下

bool HelloWorldScene::init()

{

//////////////////////////////

CSVReader::getInst()->parse("haha.csv");

MAP_LINE map_line = CSVReader::getInst()->getLineMap("haha.csv", 1000000);

MAP_LINE::iterator it = map_line.begin();

while (it != map_line.end())

{

CCLog("key:%s, value:%s", it->first.c_str(), it->second.c_str());

it++;

}

}

//csv表格数据如下图

\

赞助本站

人工智能实验室
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港