[cocos2dx utils] cocos2dx读取,解析csv文件
在我们的游戏中,经常需要将策划的数值配置成csv文件,所以解析csv文件就是一个很common的logic,
例如如下csv文件:

下面是一个基于cocos2dx 2.2.4的实现类:
#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__) */
#include "GCCsvHelper.h" GCCsvHelper::GCCsvHelper()
:m_seperator(",")
,m_colLength()
{ } GCCsvHelper::~GCCsvHelper()
{ } #pragma region reselove the content begin... bool GCCsvHelper::openAndResolveFile(const char *fileName)
{
std::string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
unsigned char* pBuffer = NULL;
unsigned long bufferSize = ;
pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(), "r", &bufferSize); std::string tmpStr = (char*)pBuffer;
std::string fileContent = tmpStr.substr(, bufferSize); std::vector<std::string> line;
rowSplit(line, fileContent, '\n');
for (unsigned int i = ; 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, );
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() - ] == '\r') {
line = line.substr(, line.length() - );
} std::string field;
unsigned int i = , j = ;
while (j < line.length()) {
if (line[i] == '"') {
//有引号
j = getFieldWithQuoted(line, field, i);
} else {
j = getFieldNoQuoted(line, field, i);
} fields.push_back(field);
i = j + ; //解析下一个field, +1为了跳过当前的分隔符
}
} int GCCsvHelper::getFieldWithQuoted(const std::string &line, std::string &field, int i)
{
unsigned int j = ;
field = std::string();
if (line[i] != '"') {
//不是引号起始,有问题
CCLOGERROR("start char is not quote when call %s", __FUNCTION__);
return -;
} for (j = i + ; j < line.length() - ; ++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 = ;
//找到下一个分隔符位置
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 >= getRowLength() || colIndex >= getColLength()) {
return "";
} if (colIndex >= data[rowIndex].size()) {
return "";
} return data[rowIndex][colIndex].c_str();
}
Test Case:
void CsvHelperTesterScene::onEnter()
{
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); GCCsvHelper *csv = new GCCsvHelper();
csv->openAndResolveFile("test.csv"); std::string line = "";
for (int i = ; i < csv->getRowLength(); ++i) {
for (int j = ; j < csv->getColLength(); ++j) {
line += csv->getData(i, j);
line += ";";
} CCLabelTTF *pLbl = CCLabelTTF::create(line.c_str(), "Arial", );
pLbl->setColor(ccc3(, , ));
pLbl->setPosition(ccp(visibleSize.width / , visibleSize.height - - i * ));
addChild(pLbl); line = "";
} delete csv;
}
测试结果:
[cocos2dx utils] cocos2dx读取,解析csv文件的更多相关文章
- 如何用Java解析CSV文件
首先看一下csv文件的规则: csv(Comma Separate Values)文件即逗号分隔符文件,它是一种文本文件,可以直接以文本打开,以逗号分隔.windows默认用excel打开.它的格式包 ...
- .NET 上传并解析CSV文件存库
1.前端: 放置浏览按钮 <div class="row inner_table text-center"> <input id="fileId&quo ...
- java opencsv解析csv文件
记一次使用opencsv解析csv文件时碰到的坑 最近在开发过程中需要解析csv文件,公司用的解析工具是opencsv,在根据opencsv的官方文档去解析时发现csv文件中含有繁体字,使用其自带的C ...
- C语言读取写入CSV文件 [一]基础篇
本系列文章目录 [一] 基础篇 [二] 进阶篇--写入CSV [三] 进阶篇--读取CSV 什么是CSV? CSV 是一种以纯文本形式存储的表格数据,具体介绍如下(来自维基百科): 逗号分隔值(Com ...
- php解析.csv文件
public function actionImport() { //post请求过来的 $fileName = $_FILES['file']['name']; $fileTmpName = $_F ...
- boost::property_tree读取解析.xml文件
boost::property_tree读取解析.xml文件 1)read_xml 支持中文路径 boost::property_tree::wptree wpt; std::locale:: ...
- boost::property_tree读取解析ini文件--推荐
boost::property_tree读取解析ini文件 #include "stdafx.h" #include <iostream> #include <b ...
- 读取gzmt.csv文件,计算均值及概率
问题: 读取gzmt.csv文件所有数据,选取收盘价格(倒数第二列),计算20天均值,权重取成交量(选做:时间权重为半衰期为15天):将该均值修剪为超过600的都设置为1000,并打印出该均值超过55 ...
- jmeter读取本地CSV文件
用jmeter录制考试上传成绩等脚本时,出现的问题及解决方法如下: 1.beanshell前置处理器,不能读取本地csv文件里的数据: 方法一: 在beanshell里不能直接从本地的csv文件里读取 ...
随机推荐
- iphone 弹出键盘,文本框自动向上移动。
1.让类继承UITextViewDelegate UITextView *inputTextView;UIScrollView * _scrollView; 2.在init函数中先创建scrollVi ...
- MacOS内核调试环境搭建
http://ddeville.me/2015/08/using-the-vmware-fusion-gdb-stub-for-kernel-debugging-with-lldb http://dd ...
- SqlServer2000事件探测器的使用
由于公司自己开发的项目,需要与第三方软件数据库对接.我们项目用的数据库是MySQL而第三方用的是sqlserver2000.项目框架用的thinkphp5.0,通过thinkPHP新建一个模块,单独连 ...
- 一些常用的集合工具的代码块(缓慢更新XD,更新了多属性过滤:) )
更新记录 虽然经常放鸽子,但是还是要记录一下更新 2017.8.30 更新了listToMap的方法,现在可以指定多个属性进行分组了,例如你要指定一个学生集合,按照名字和年龄相同的放在一组,现在只要调 ...
- java面试题:如果一串字符如"aaaabbc中国1512"要分别统计英文字符的数量,中文字符的数量,和数字字符的数量,假设字符中没有中文字符、英文字符、数字字符之外的其他特殊字符。
package com.swift; public class TotalNumber_String { public static void main(String[] args) { /* * 如 ...
- C++ 学习笔记 (七)继承与多态 virtual关键字的使用场景
在上一篇 C++ 学习笔记 (六) 继承- 子类与父类有同名函数,变量 中说了当父类子类有同名函数时在外部调用时如果不加父类名则会默认调用子类的函数.C++有函数重写的功能需要添加virtual关键字 ...
- JS - OOP-继承的最佳实现方式
如上图,使用第三种方式实现继承最好,也就是加了下划线的. 但是Object.create方法是ES6才支持的,所以,右边就写了一个实现其同样功能的函数.
- 三、Shell 传递参数
Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 实例 以 ...
- LRU算法原理解析
LRU是Least Recently Used的缩写,即最近最少使用,常用于页面置换算法,是为虚拟页式存储管理服务的. 现代操作系统提供了一种对主存的抽象概念虚拟内存,来对主存进行更好地管理.他将主存 ...
- UML类图关系模式(C++代码说明)
在UML类图中的关系模式主要有以下几种: 泛化(Generalization), 实现(Realization), 关联(Association), 聚合(Aggregation), 依赖(Depe ...