COCOS
有部分机型在读取XML和CSV时,在WIN32端能正常运行,但在安卓端上就会出现闪退显现,原因就是在安卓端时找不到CSV的路径,现由帅气的班长提供解决方案如下:
一.XML
1. 原读取方式:
tinyxml2::XMLDocument doc;
doc.LoadFile(file.c_str());//读取地址
auto ele = doc->FirstChildElement();
int index = 0;
while (ele)
{
ValueMap map;
map["ID"]= ele->Attribute("ID");
map["Name"]= ele->Attribute("Name");
map["Level"]= ele->Attribute("Level");
map["Exp"]= ele->Attribute("Exp");
map["RingCount"]= ele->Attribute("RingCount");
map["GoldCount"]= ele->Attribute("GoldCount");
map["WoodCount"]= ele->Attribute("WoodCount");
map["GoldCapacity"]= ele->Attribute("GoldCapacity");
map["WoodCapacity"]= ele->Attribute("WoodCapacity");
data.push_back((Value)map);
ele = ele->NextSiblingElement();
}
delete doc;
使用这种方式就会出现闪退;
2.现改为如下方式:
将 tinyxml2::XMLDocument doc;
doc.LoadFile(file.c_str());
这两句改为:
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument();
ssize_t bufferSize;
auto pBuffer = FileUtils::getInstance()->getFileData(file.c_str(), "rb", &bufferSize);
int error = doc->Parse((constchar*)pBuffer);
pBuffer[(int)bufferSize - 1] = '\0';
二CSV
将改版解析CSV文件奉上:
#ifndef __QLCSVFile__
#define__QLCSVFile__
#include"head.h"
//h文件
usingnamespace std;
classQLCSVFile {
public:
QLCSVFile();
~QLCSVFile();
//用以存储数据
vector<vector<string> > data;
private:
string fieldsep;
int cols;
void StringSplit(conststring& str, vector<string>& tokens, constchar& delimiters);
void split(vector<string>& field, string line);
int advplain(conststring& line, string& fld, int);
int advquoted(conststring& line, string& fld, int);
public:
//打开CSV文件
bool openFile(constchar*fileName);
//根据行列获取数据
constchar* getData(int rows, int cols);
//获取指定数据的列下标
int findColsData(int cols, constchar* value);
//得到总列数
inlineint getCols() { return cols; }
//得到总行数
inlineint getRows() { return data.size(); }
};
#endif/* defined(__QLCSVFile__) */
//cpp文件
#include"head.h"
QLCSVFile::QLCSVFile()
: fieldsep(",")
, cols(0)
{
}
//获取指定行列的数据
constchar* QLCSVFile::getData(introws, intcols)
{
if (rows < 0
|| rows >= data.size()
|| cols < 0
|| cols >= data[rows].size())
{
return"";
}
return data[rows][cols].c_str();
}
//获取指定数据的列下标
intQLCSVFile::findColsData(intcols, constchar *value)
{
for (int i = 0; i < data.size(); i++)
{
if (strcmp(getData(i, cols), value) == 0)
{
return i;
}
}
return -1;
}
//解析CSV文件
boolQLCSVFile::openFile(constchar *fileName)
{
string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
ssize_t bufferSize = 0;
auto pBuffer = FileUtils::getInstance()->getFileData(fileName, "rb", &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());
}
returntrue;
}
voidQLCSVFile::StringSplit(const std::string &str, vector<string> &tokens, constchar &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);
}
}
voidQLCSVFile::split(vector<string>& field, stringline)
{
string fld;
int i = 0;
int j = 0;
if (line.length() == 0)
{
return;
}
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());
}
intQLCSVFile::advquoted(conststring &line, string &fld, inti)
{
int j = 0;
fld="";
for (j = i; j < line.length(); j++)
{
if (line[j] == '"'
&& line[++j] != '"')
{
int k = line.find_first_of(fieldsep, j);
if (k > line.length())
{
k = line.length();
}
for (k -= j; k-- > 0; )
{
fld+=line[j++];
}
break;
}
fld+=line[j];
}
return j;
}
intQLCSVFile::advplain(conststring &line, string &fld, inti)
{
int j = 0;
j = line.find_first_of(fieldsep, i);
if (j > line.length())
{
j = line.length();
}
fld=string(line, i, j - i);
return j;
}
//析构函数,释放内存
QLCSVFile::~QLCSVFile()
{
for (int i = 0; i < data.size(); i++)
{
data[i].clear();
}
data.clear();
}
//解析方式:
void loadCsvData(std::stringfile, ValueVector& data)
{
QLCSVFile csv;
csv.openFile(file.c_str());
for (int i = 0; i <csv.getRows(); i++)
{
ValueMap map;
for (int j = 0; j < csv.getCols(); j++)
{
map[csv.getData(0,j)]= csv.getData(i, j);
}
data.push_back((Value)map);
}
}
//这种方式不会出现闪退,但在读取最后一列时会出现多出/r的回车符号,需自己分割。