TP:E84BF8F4 基于cocos2d-x,CSV文件读取类。
来源:互联网 发布:java 加密字符串 编辑:程序博客网 时间:2024/05/10 09:56
自己封装的关于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::stringm_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();
}
- TP:E84BF8F4 基于cocos2d-x,CSV文件读取类。
- Cocos2d-x读取Csv文件工具类
- cocos2d-x(quick-lua)读取csv文件
- cocos2d-x CSV文件读取 (Excel生成csv文件)
- cocos2d-x CSV文件读取 (Excel生成csv文件)
- cocos2d-x学习笔记——Csv文件读取工具
- Cocos2d-js读取csv文件工具类
- Cocos2d-x——读取Csv配置文件
- 【cocos2d-x 3.5】Lua读取CSV
- cocos2d-x 读取CSV文件,读取本地Excel配置表的方法
- cocos2d-x 读取 文件
- cocos2d-x 读取文件
- Cocos2d-x Lua 读取Csv文件,更方便的使用数据
- Cocos2d-x 3.x中读取csv数据文件
- CSV文件读取类
- cocos2d-x 读取plist文件
- cocos2d-x plist文件读取
- cocos2d-x 读取.plist文件
- 云_阿里云_科普01
- IOS UIView方法详细解析
- CocoaPods安装和使用
- 周鸿祎谈产品设计 要从这3点出发
- Linux命令行报bash:.....:command not found的解决办法
- TP:E84BF8F4 基于cocos2d-x,CSV文件读取类。
- Android 4.3中的视觉边界布局(Optical bounds layout)
- UIView_TableView 分割线不全的问题
- 黑马day01xml 解析方式与原理分析
- linux文件夹对比命令——meld
- SQL SERVER实现相同性质的不同行对应整形字段的乘法运算(sql server 行之间乘法运算)
- Hadoop2.6.0 + Zookeeper3.4.6 + HBase0.98.9hadoop2环境搭建示例
- LCN 逻辑频道号(DVB)
- Asp.Net HttpContext.RemapHandler 用法