(4)cocos2dx读取csv数据文件

cocos2dx中读取数据文件可能有很多种,像读取xml,lua,csv,json等,这些都可以作为配置数据的格式。

最近用到了读取csv数据文件,所以在网上找了一下关于这方面的技术博客。果然,网上各路大神都是不吝啬的,

不说废话了,直接上代码。代码如下(测试通过,可读取数据):

.h头文件

 

//
//  QLCSVFile.h
//
//  Created by quasi_lee on 15-7-7.
//
//

#ifndef __QLCSVFile__
#define __QLCSVFile__

#include <stdio.h>
#include "cocos2d.h"

USING_NS_CC;
using namespace std;

class QLCSVFile {
public:
    QLCSVFile();
    ~QLCSVFile();
    
    //用以存储数据
    vector<vector<string> > data;
private:
    string fieldsep;
    int cols;
    void StringSplit(const string& str,vector<string>& tokens,const char& delimiters);
    void split(vector<string>& field,string line);
    int advplain(const string& line,string& fld,int);
    int advquoted(const string& line,string& fld,int);
public:
    //打开CSV文件
    bool openFile(const char*fileName);
    //根据行列获取数据
    const char* getData(int rows,int cols);
    //获取指定数据的列下标
    int findColsData(int cols,const char* value);
    //得到总列数
    inline int getCols(){return cols;}
    //得到总行数
    inline int getRows(){return data.size();}
};


#endif /* defined(__QLCSVFile__) */


.cpp文件

 

 

//
//  QLCSVFile.cpp
//
//  Created by quasi_lee on 15-7-7.
//
//

#include "QLCSVFile.h"

QLCSVFile::QLCSVFile()
: fieldsep(",")
, cols(0)
{
    
}

//获取指定行列的数据
const char* QLCSVFile::getData(int rows, int cols)
{
    if(rows < 0
       || rows >= data.size()
       || cols < 0
       || cols >= data[rows].size())
    {
        return "";
    }
    return data[rows][cols].c_str();
}

//获取指定数据的列下标
int QLCSVFile::findColsData(int cols, const char *value)
{
    for(int i = 0; i < data.size(); i++)
    {
        if(strcmp(getData(i, cols), value) == 0)
        {
            return i;
        }
    }
    return -1;
}

//解析CSV文件
bool QLCSVFile::openFile(const char *fileName)
{
    string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
    unsigned char* pBuffer = NULL;
    unsigned long bufferSize = 0;
    pBuffer = CCFileUtils::sharedFileUtils()->getFileData(pathKey.c_str(), "r", &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());
    }
    return true;
}

void QLCSVFile::StringSplit(const std::string &str, vector<string> &tokens, const char &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);
    }
}

void QLCSVFile::split(vector<string>& field,string line)
{
    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());
}

int QLCSVFile::advquoted(const string &line, string &fld, int i)
{
    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;
}

int QLCSVFile::advplain(const string &line, string &fld, int i)
{
    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();
}

 

 

代码下载地址:http://download.csdn.net/detail/u010170012/8911095
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值