3. * 夝愅姰揑悢悩曻摓data?槩擇?悢?拞丆捠?getData(int rows,int cols)
4. * ?庢悢悩
5. **/
6.
7.#ifndef _CSVPARSE_
8.#define _CSVPARSE_
9.
10.#include "cocos2d.h"
11.#include 12.#include 13.#include 14. 15.class CSVParse { 16. 17.public: 18. CSVParse(istream& fin = cin, string sep = g fld; 18. int i, j; 19. 20. if (line.length() == 0) 21. return ; 22. i = 0; 23. 24. do { 25. if (i < line.length() && line[i] == '"') 26. j = advquoted(line, fld, ++i); // skip quote 27. else 28. j = advplain(line, fld, i); 29. 30. field.push_back(fld); 31. i = j + 1; 32. } while (j < line.length()); 33. 34.} 35. 36.// advquoted: quoted field; return index of next separator 37.int CSVParse::advquoted(const string& s, string& fld, int i) 38.{ 39. int j; 40. 41. fld = ""; 42. for (j = i; j < s.length(); j++) 43. { 44. if (s[j] == '"' && s[++j] != '"') 45. { 46. int k = s.find_first_of(fieldsep, j); 47. if (k > s.length()) // no separator found 48. k = s.length(); 49. for (k -= j; k-- > 0; ) 50. fld += s[j++]; 51. break; 52. } 53. fld += s[j]; 54. } 55. return j; 56.} 57. 58.// advplain: unquoted field; return index of next separator 59.int CSVParse::advplain(const string& s, string& fld, int i) 60.{ 61. int j; 62. 63. j = s.find_first_of(fieldsep, i); // look for separator . if (j > s.length()) // none found 65. j = s.length(); 66. fld = string(s, i, j-i); 67. return j; 68.} 69. 70. 71.// getfield: return n-th field 72.const char* CSVParse::getData(int rows,int cols) 73.{ 74. if (rows<0||rows>=data.size()||cols<0||cols>=data[rows].size()) { 75. return ""; 76. } 77. 78. return data[rows][cols].c_str(); 79.} 80. 81.int CSVParse::findColsData(int cols,const char* value) 82.{ 83. for (int i=0; i 85. { 86. return i; 87. } 88. } . return -1; 90.} 91.//梡槹寭梕windows壓揑?峴晞\ \ 92.//尨場惀嵼windows壓?峴晞?\ \ 丆帶嵼unix壓\ 93.//?峴晻憰,斲庢揑?峴?庢揑嵟岪堦槩悢悩嘁?夛桳槩?奜揑\ 94.bool getline(std::ifstream &is,std::string &str){ 95. bool b = std::getline(is,str); 96. std::string::size_type p = str.find_last_of('\ '); 97. if(p != std::string::npos) str.erase(p); 98. return b; 99.} 100. 101.//?庢曽幃: 拃峴?庢, 彨峴?擖帤晞孁, 峴擵?梡夞峴嬫暘 102.//If you want to avoid reading into character arrays, 103.//you can use the C++ string getline() function to read lines into strings 104.bool CSVParse::openFile(const char* fileName) 105.{ 106. string pathKey = CCFileUtils::fullPathFromRelativePath(fileName); 107. ifstream fin(pathKey.c_str()); 108. string s; 109. 110. while(::getline(fin,s)) 111. { 112. CCLOG("%s下载本文