zoukankan      html  css  js  c++  java
  • cocos2d-x CSV文件读取 (Excel生成csv文件)

    实现类

    CCSVParse.h

    #ifndef __C_CSV_PARSE__
    #define __C_CSV_PARSE__
    
    #include "cocos2d.h"
    #include <vector>
    using namespace std;
    
    class CCSVParse
    {
    public:
        //CCSVParse(void);
        ~CCSVParse(void);
    
        CCSVParse(istream& fin=cin, string sep=","):
            fieldsep(sep),
            cols(0)
        {
    
        }
    
        //用以存储数据
        std::vector<std::vector<std::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:
        bool openFile(const char* fileName);
        const char* getData(unsigned int rows, unsigned int cols);
        int findColsData(int cols, const char* value);
    
        inline int getCols(){return cols;}
        inline int getRows(){return data.size();};
    };
    
    #endif //__C_CSV_PARSE__

    CCSVParse.cpp

    #include "CSVParse.h"
    
    using namespace cocos2d;
    
    
    // CCSVParse::CCSVParse(void)
    // {
    // }
    
    CCSVParse::~CCSVParse(void)
    {
    }
    
    void CCSVParse::StringSplit( const 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 CCSVParse::split( vector<string>& field, string line )
    {
        string fld;
        unsigned int i,j=0;
    
        if( line.length() ==0 )
            return;
        i=0;
    
        do 
        {
            if(j<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 CCSVParse::advplain( const string& s, string& fld, int i)
    {
        unsigned int j;
        j = s.find_first_of(fieldsep, i);
        if(j>s.length())
            j=s.length();
        fld = string(s,i,j-i);
        return j;
    }
    
    int CCSVParse::advquoted( const string& s, string& fld, int i)
    {
        unsigned int j;
        fld = "";
        for (j=i; j<s.length(); ++j)
        {
            if(s[j]=='"' && s[++j]!='"')
            {
                unsigned int k = s.find_first_of(fieldsep, j);
                if(k>s.length())
                    k = s.length();
                for(k-=j; k-->0;)
                    fld += s[j++];
                break;
            }
            fld += s[j];
        }
        return j;
    }
    
    //解析 CVS 文件
    bool CCSVParse::openFile( const char* fileName )
    {
        string pathKey = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName);
        unsigned char* pBuffer = nullptr;
        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, '
    ');
        for(unsigned 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;
    }
    
    //获取指定行列的数据
    const char* CCSVParse::getData(unsigned int rows, unsigned int cols )
    {
        if (rows<0 || rows>=data.size() || cols<0 || cols>=data[rows].size())
        {
            return "";
        }
        return data[rows][cols].c_str();
    }
    
    //获取指定数据的列下标
    int CCSVParse::findColsData( int cols, const char* value )
    {
        for (unsigned int i=0; i<data.size(); ++i)
        {
            if(strcmp(getData(i,cols),value)==0)
                return i;
        }
        return -1;
    }

    HelloWorld.cpp 中 init()  函数中添加

    CCSVParse* csvFile = new CCSVParse();
            csvFile->openFile("Book1.csv");
            for (int i=0; i<csvFile->getCols(); ++i)
            {
                string strLine = "";
                for(int j=0; j<csvFile->getRows(); ++j)
                {
                    strLine += csvFile->getData(i,j);
                    strLine += ",";
                }
                CCLabelTTF* pLab = CCLabelTTF::create(strLine.c_str(),"Arial",20);
                pLab->setPosition(ccp(size.width/2,size.height-90-i*30));
                this->addChild(pLab,2);
            }

    Book1.csv 内容  注意将文件保存为UTF-8格式的(否则中文显示乱码)

    星期一,1,10000,HP1,MP1,数值1,Icon1.png
    星期二,2,10001,HP2,MP2,数值2,Icon2.png
    星期三,3,10002,HP3,MP3,数值3,Icon3.png
    星期四,4,10003,HP4,MP4,数值4,Icon4.png
    星期五,5,10004,HP5,MP5,数值5,Icon5.png
    星期六,6,10005,HP6,MP6,数值6,Icon6.png
    星期日,7,10006,HP7,MP7,数值7,Icon7.png

    win32 平台 显示结果:(保存文件 非UTF-8格式)中文显示乱码

    win32 平台 显示结果:(保存文件 为UTF-8格式) 正常显示

    不早了,洗洗睡吧 , 明天又起不来了

  • 相关阅读:
    中文词频统计及词云制作 25
    实验一 DOS实验 25
    字符串练习 25
    Python、循环的练习 25
    用requests库和BeautifulSoup4库爬取新闻列表 25
    爬取新闻列表 25
    Mockito使用总结
    20121116
    20121123
    20121115
  • 原文地址:https://www.cnblogs.com/MrGreen/p/3295662.html
Copyright © 2011-2022 走看看