之前做一个项目,把中文数据保存在csv类型的表里面,用的时候根据某行某列读取出来,在此把所使用到的代码贴出来,做个笔记。
1.首先要创建一个文件,把后缀名改为.csv,保存。
2.用Excel打开并写入数据,注意第一个单元格是0行0列
3.把.csv文件放入资源目录下
4.创建CsvHelper文件,把以下代码写入
5.直接对照最下方代码,拿来使用
CsvHelper.h:
#ifndef __Cell__GCCsvHelper__
#define __Cell__GCCsvHelper__
#include <iostream>
#include "cocos2d.h"
#include <vector>
USING_NS_CC;
class GCCsvHelper
{
public:
GCCsvHelper();
~GCCsvHelper();
bool openAndResolveFile(const char *fileName);
const char *getData(unsigned int rowIndex, unsigned int colIndex);
inline int getColLength() { return m_colLength; }
inline int getRowLength() { return data.size(); }
private:
const std::string m_seperator;
std::vector<std::vector<std::string> > data;
//cols length
int m_colLength;
void rowSplit(std::vector<std::string> &rows, const std::string &content, const char &rowSeperator);
void fieldSplit(std::vector<std::string> &fields, std::string line);
//获取带引号的字段
int getFieldWithQuoted(const std::string &line, std::string& field, int index);
//获取无引号的字段
int getFieldNoQuoted(const std::string &line, std::string &field, int index);
};
#endif /* defined(__Cell__GCCsvHelper__) */
CsvHelper.cpp:
#include "CsvHelper.h"
GCCsvHelper::GCCsvHelper()
:m_seperator(",")
,m_colLength(0)
{
}
GCCsvHelper::~GCCsvHelper()
{
}
#pragma region reselove the content begin...
bool GCCsvHelper::openAndResolveFile(const char *fileName)
{
data.clear();
std::string pathKey = CCFileUtils::getInstance()->fullPathForFilename(fileName);
unsigned char* pBuffer = NULL;
ssize_t bufferSize = 0;
pBuffer = CCFileUtils::getInstance()->getFileData(pathKey.c_str(), "r", &bufferSize);
std::string tmpStr = (char*)pBuffer;
std::string fileContent = tmpStr.substr(0, bufferSize);
delete pBuffer;
std::vector<std::string> line;
rowSplit(line, fileContent, '\n');
for (unsigned int i = 0; i < line.size(); ++i) {
std::vector<std::string> fieldVector;
fieldSplit(fieldVector, line[i]);
data.push_back(fieldVector);
m_colLength = std::max(m_colLength, (int)fieldVector.size());
}
return true;
}
void GCCsvHelper::rowSplit(std::vector<std::string> &rows, const std::string &content, const char &rowSeperator)
{
std::string::size_type lastIndex = content.find_first_not_of(rowSeperator, 0);
std::string::size_type currentIndex = content.find_first_of(rowSeperator,lastIndex);
while (std::string::npos != currentIndex || std::string::npos != lastIndex) {
rows.push_back(content.substr(lastIndex, currentIndex - lastIndex));
lastIndex = content.find_first_not_of(rowSeperator, currentIndex);
currentIndex = content.find_first_of(rowSeperator, lastIndex);
}
}
void GCCsvHelper::fieldSplit(std::vector<std::string> &fields, std::string line)
{
if (line[line.length() - 1] == '\r') {
line = line.substr(0, line.length() - 1);
}
std::string field;
unsigned int i = 0, j = 0;
while (j < line.length()) {
if (line[i] == '"') {
//有引号
j = getFieldWithQuoted(line, field, i);
} else {
j = getFieldNoQuoted(line, field, i);
}
fields.push_back(field);
i = j + 1; //解析下一个field, +1为了跳过当前的分隔符
}
}
int GCCsvHelper::getFieldWithQuoted(const std::string &line, std::string &field, int i)
{
unsigned int j = 0;
field = std::string();
if (line[i] != '"') {
//不是引号起始,有问题
CCLOGERROR("start char is not quote when call %s", __FUNCTION__);
return -1;
}
for (j = i + 1; j < line.length() - 1; ++j) {
if (line[j] != '"') {
//当前char不为引号,则是field内容(包括逗号)
field += line[j];
} else {
//遇到field结束时的引号,可以返回
return j;
break;
}
}
if (j == line.length()) {
//没有找到成对的结束引号
CCLOGERROR("resoleve the line error: no pair quote, line:%s, field:%s, start index:%d", line.c_str(), field.c_str(), i);
}
return j;
}
int GCCsvHelper::getFieldNoQuoted(const std::string &line, std::string &field, int index)
{
unsigned int j = 0;
//找到下一个分隔符位置
j = line.find_first_of(m_seperator, index);
if (j > line.length()) {
j = line.length();
}
field = std::string(line, index, j - index);
return j;
}
#pragma region end.
///search data
const char *GCCsvHelper::getData(unsigned int rowIndex, unsigned int colIndex)
{
if (rowIndex >= (unsigned int)getRowLength() || colIndex >= (unsigned int)getColLength()) {
return "";
}
if (colIndex >= data[rowIndex].size()) {
return "";
}
return data[rowIndex][colIndex].c_str();
}
使用CsvHelper代码如下,
ShopWord.h:
#ifndef __ShopWord__
#define __ShopWord__
#include "cocos2d.h"
#include "Public\CsvHelper.h"
class ShopWord
{
public:
static ShopWord* sharedShopWord();
int getWordCount();
const char* GetWordByTag(int row, int col);
private:
ShopWord();
~ShopWord();
bool init();
GCCsvHelper csvHelper;
};
#define g_ShopWord ShopWord::sharedShopWord()
#endif
ShopWord.cpp:
#include "ShopWord.h"
USING_NS_CC;
using namespace std;
#define STATIC_DATA_PATH "WordData/ShopWord.csv"
static ShopWord* _sharedStaticData = NULL;
ShopWord* ShopWord::sharedShopWord()
{
if(_sharedStaticData == NULL){
_sharedStaticData = new ShopWord();
_sharedStaticData->init();
}
return _sharedStaticData;
}
ShopWord::ShopWord()
{
}
ShopWord::~ShopWord()
{
}
bool ShopWord::init()
{
csvHelper.openAndResolveFile(STATIC_DATA_PATH);
return true;
}
int ShopWord::getWordCount()
{
return csvHelper.getRowLength()-1;
}
const char* ShopWord::GetWordByTag(int row, int col)
{
if (row>getWordCount())
{
return "";
}
return csvHelper.getData(row, col);
}