cocos2d-x 讀取CSV檔案,讀取本地Excel配置表的方法,cocos2d-xcsv
//CSVReader.h
#define MAP_LINE std::map<std::string, std::string>//key為首行字串, value為此列字串#define MAP_CONTENT std::map<int, MAP_LINE>//key為code, value為一行map#define VEC_MAP std::vector<std::pair<std::string, 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一行codeconst 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<std::string, MAP_CONTENT> m_fileMap;//檔案map static CSVReader *m_inst;//執行個體};
//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一行codeconst 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則標記為truewhile (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';//添加到mapm_fileMap.insert(std::map<std::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<std::pair<std::string, int> > tVector; for (int i=0; i<count; ++i){tVector.push_back(std::map<std::string, int>::value_type(pv[i], i));}//第一行作為keyif(index == 0){m_firstVector = tVector;}//第2行為注釋else if(index > 1){//一行的mapstd::map<string, string> tmp;for (int i = 0; i < m_firstVector.size(); i++){tmp.insert(std::map<string, string>::value_type(m_firstVector[i].first, tVector[i].first));}m_contentMap.insert(std::map<int, std::map<string, string>>::value_type(atoi(tVector[0].first.c_str()), tmp));}}
//用法如下
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表格資料如