自己封装的关于csv文件读取的类,可以读取csv文件,并按照自己的需求转换数据结果。提供字符串转float,int等功能。
头文件
#ifndef __CSV_FILE_H__
#define __CSV_FILE_H__
#include "cocos2d.h"
class CSVFile:public cocos2d::Ref
{
public:
CSVFile();
~CSVFile();
static CSVFile* create(std::string filename);
bool initWithFile(std::string filename);
std::string getData(unsigned int rowIndex, unsigned int colIndex);
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);
inline int getColLength() { return m_nLength; }
inline int getRowLength() { return data.size(); }
float getDataOfFloat(unsigned int rowIndex, unsigned int colIndex);
int getDataOfInt(unsigned int rowIndex, unsigned int colIndex);
private:
const std::string m_seperator;
int m_nLength;
std::string _text;
std::vector<std::vector<std::string>> data;
};
#endif
cpp文件
#include "CSVFileUtil.h"
USING_NS_CC;
CSVFile::CSVFile() :m_seperator(",")
{
}
CSVFile* CSVFile::create(std::string filename)
{
CSVFile* csv = new CSVFile();
if (csv->initWithFile(filename))
{
csv->autorelease();
return csv;
}
delete(csv);
return NULL;
}
bool CSVFile::initWithFile(std::string filename)
{
_text = FileUtils::getInstance()->getStringFromFile(filename);
std::vector<std::string> line;
rowSplit(line, _text, '\n');
for (unsigned int i = 0; i < line.size(); ++i) {
std::vector<std::string> fieldVector;
fieldSplit(fieldVector, line[i]);
data.push_back(fieldVector);
m_nLength = std::max(m_nLength, (int)fieldVector.size());
}
return true;
}
void CSVFile::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 CSVFile::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 CSVFile::getFieldWithQuoted(const std::string &line, std::string &field, int i)
{
unsigned int j = 0;
field = std::string();
if (line[i] != '"') {
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()) {
}
return j;
}
int CSVFile::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;
}
///search data
std::string CSVFile::getData(unsigned int rowIndex, unsigned int colIndex)
{
rowIndex--;
colIndex--;
if (rowIndex >= getRowLength() || colIndex >= getColLength()) {
return "";
}
if (colIndex >= data[rowIndex].size()) {
return "";
}
return data[rowIndex][colIndex];
}
float CSVFile::getDataOfFloat(unsigned int rowIndex, unsigned int colIndex)
{
return atof(getData(rowIndex, colIndex).c_str());
}
int CSVFile::getDataOfInt(unsigned int rowIndex, unsigned int colIndex)
{
return atoi(getData(rowIndex, colIndex).c_str());
}
CSVFile::~CSVFile()
{
_text.clear();
}