Cocos2d-x to read CSV files, read local Excel configuration table method

Source: Internet
Author: User
Tags csv file reader

Cocos2d-x to read CSV files, read local Excel configuration table method

// CSVReader. h

 

 

# Define MAP_LINE std: map
 
  
// Key is the first line string. value is the string of this column # define MAP_CONTENT std: map
  
   
// The key is code, and the value is a map # define VEC_MAP std: vector
   
    
> // Csv file reader class CSVReader {public: CSVReader (); static CSVReader * getInst (); // obtain the instance // parse csv. fileName: csv file name, void parse (const char * fileName); // get the content map. filename: file name const MAP_CONTENT & getContentMap (std: string filename); // obtain a map row. filename: file name, code a row of codeconst MAP_LINE & getLineMap (std: string filename, int code); // gets the value of a column in a row of const std: string & getByCode (std:: string filename, int code, const std: string & key); private: // read a csv row. line: void readCSVLine (const char * line, int index) of a row; VEC_MAP m_firstVector; // vector MAP_CONTENT m_contentMap of the first row; // content map std: map
    
     
M_fileMap; // file map static CSVReader * m_inst; // instance };
    
   
  
 


 

// CSVReader. cpp

 

CSVReader * CSVReader: m_inst = NULL; // constructor CSVReader: CSVReader () {m_firstVector.clear (); m_contentMap.clear (); m_fileMap.clear ();} // obtain the instance CSVReader * CSVReader: getInst () {if (! M_inst) {m_inst = new CSVReader ();} return m_inst;} // get the map content. filename: file name const MAP_CONTENT & CSVReader: getContentMap (std: string filename) {return m_fileMap.find (filename)-> second;} // obtain a map row. filename: file name, code a line codeconst MAP_LINE & CSVReader: getLineMap (std: string filename, int code) {return getContentMap (filename ). find (code)-> second;} // obtain the value of a column in a row. const std: string & CSVReader: getByCode (std: string filen Ame, int code, const std: string & key) {return getLineMap (filename, code ). find (key)-> second;} // parse csv. fileName: csv file name, void CSVReader: parse (const char * fileName) {m_contentMap.clear (); // first 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]; // maximum number of bytes in a row const char * src = data; if (size = 0) size = strlen (src); char * pl = line; // the pointer to the line array int index = 0; bool skip = false; // If a behavior is empty, skip is marked as truewhile (data-src <size) {// read to the end of a row if (* data = '\ n '&&! Skip) {* pl = '\ 0'; readCSVLine (line, index); ++ index; pl = line;} else if (* data =' \ R ') {} else {// any field can be left blank if (* data = '"') skip =! Skip; * pl = * data; ++ pl;} + data;} * pl = '\ 0'; // Add it to mapm_fileMap.insert (std: map
 
  
: Value_type (fileName, m_contentMap);} // read a csv row. line: void CSVReader: readCSVLine (const char * line, int index) {char value [32768]; // maximum number of bytes in a row 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'; // temporary array std: vector
  
   
> TVector; for (int I = 0; I
   
    
: Value_type (pv [I], I);} // The first line serves as the keyif (index = 0) {m_firstVector = tVector ;} // 2nd behavior annotation else if (index> 1) {// mapstd: map of a row
    
     
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 ));}}
      
     
    
   
  
 


 

// Usage:

 

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 table data, as shown in

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.