csv2txt.cpp

#include <iostream>

#include <fstream.h>

#include <windows.h>

#include <iomanip.h>



#pragma once

#include <process.h>

#include <map>

#include <vector>

#include <queue>

#include <set>

#include <string>

#include <list>



typedef char                    i8;

typedef unsigned char           u8;

typedef short                   i16;

typedef unsigned short          u16;

typedef long int                i32;

typedef unsigned long           u32;



class CCSVOperator

{

public:

    CCSVOperator(){};

    ~CCSVOperator(){};

    CCSVOperator(const char* path);

    bool LoadCSV(const char* path);

    bool SaveCSV(const char* path = NULL);

    bool GetInt(u32 uiLine, u32 uiRow, int& iValue);

    std::string* GetString(u32 uiLine, u32 uiRow);

    std::map<u32, std::map<u32, std::string> >& GetCSVMap(){return m_StringKeyMap;}

protected:

    std::string m_CSVName;

    std::map<u32, std::map<u32, std::string> > m_StringKeyMap;



};



namespace StringParser{

//从分隔符中获得数据

inline int GetParamFromString(std::string Str, std::vector<i32>& IntVec, char Delim = ',')

{

    char* p = strtok((char*)Str.c_str(), &Delim); 

    while (p)

    {

        IntVec.push_back(atoi(p));

        p = strtok(NULL, &Delim); 

    }

    return IntVec.size();

}



inline int GetParamFromString(std::string Str, std::vector<float>& FloatVec, char Delim = ',')

{

    char* p = strtok((char*)Str.c_str(), &Delim); 

    while (p)

    {

        FloatVec.push_back(atof(p));

        p = strtok(NULL, &Delim); 

    }

    return FloatVec.size();

}



inline int GetParamFromString(std::string Str, std::vector<u32>& uiIntVec, char Delim = ',')

{

    char* p = strtok((char*)Str.c_str(), &Delim); 

    while (p)

    {

        uiIntVec.push_back(strtoul(p, NULL, 10));

        p = strtok(NULL, &Delim); 

    }

    return uiIntVec.size();

}



inline int GetParamFromString(std::string Str, std::vector<std::string>& StringVec, char Delim = ',')

{

    char* p = strtok((char*)Str.c_str(), &Delim); 

    while (p)

    {

        std::string buffer = p;

        StringVec.push_back(buffer);

        p = strtok(NULL, &Delim); 

    }

    return StringVec.size();

}



//以左右符号得到括号中的数据ex:[3.1415;0.125][1000;9999]

template<typename T>

int GetParamFromArea(std::string Str, std::vector<std::vector<T> >& IntVec, char left = '[', char right = ']', char Delim = ';')

{

    char* pTarget = (char*)Str.c_str();

    for (;;)

    {

        char* pLeft = strchr(pTarget, left);

        char* pRight = strchr(pTarget, right);

        if (pLeft && pRight)

        {

            std::string strbuff;

            strbuff.insert(0, ++pLeft, pRight-pLeft);



            std::vector<T> Intbuff;

            if (GetParamFromString(strbuff, Intbuff, Delim))

            {

                IntVec.push_back(Intbuff);

            }

            pTarget = ++pRight;

        }

        else

        {

            break;

        }

    }

    return IntVec.size();

}

};



CCSVOperator::CCSVOperator(const char* path)

{

    LoadCSV(path);

}



bool CCSVOperator::LoadCSV(const char* path)

{

    FILE* pfile = fopen(path, "r");

    if (pfile)

    {

        fseek(pfile,0,SEEK_END);

        u32 dwsize = ftell(pfile);

        rewind(pfile);



        char* filebuffer = new char[dwsize];

        fread(filebuffer, 1, dwsize, pfile);



        std::map<u32, std::string> StringMap;

        char* pBegin = filebuffer;

        char* pEnd = strchr(filebuffer, '\n');

        u32 uiIndex = 1;

        while (pEnd != NULL)

        {

            std::string strbuff;

            strbuff.insert(0, pBegin, pEnd-pBegin);

            if (!strbuff.empty())

            {

                StringMap[uiIndex] = strbuff;

            }

            pBegin = pEnd + 1;

            pEnd = strchr(pEnd + 1, '\n');

            ++uiIndex;

        }

        delete[] filebuffer;



        std::map<u32, std::string>::iterator iter = StringMap.begin();

        for (; iter != StringMap.end(); ++iter)

        {

            std::vector<std::string> StringVec;

            std::map<u32, std::string> l_StringMap;

            StringParser::GetParamFromString(iter->second, StringVec);

            for (int i = 0; i < StringVec.size(); ++i)

            {

                l_StringMap[i+1] = StringVec.at(i);

            }

            m_StringKeyMap[iter->first] = l_StringMap;

        }

        fclose(pfile);

        m_CSVName = path;

        return true;

    }

    return false;

}



bool CCSVOperator::GetInt(u32 uiLine, u32 uiRow, int& iValue)

{

    std::string* pKey = GetString(uiLine, uiRow);

    if (pKey)

    {

        iValue = atoi(pKey->c_str());

        return true;

    }

    else

    {

        return false;

    }

}



std::string* CCSVOperator::GetString(u32 uiLine, u32 uiRow)

{

    std::map<u32, std::map<u32, std::string> >::iterator iterLine = m_StringKeyMap.find(uiLine);

    if (iterLine != m_StringKeyMap.end())

    {

        std::map<u32, std::string>& rStringMap = iterLine->second;

        std::map<u32, std::string>::iterator iterRow = rStringMap.find(uiRow);

        if (iterRow != rStringMap.end())

        {

            return &iterRow->second;

        }

        else

        {

            return NULL;

        }

    }

    else

    {

        return NULL;

    }

}



int main(int argc, char* argv[])

{

    int OtdrTestsNum;//total num of OTDR test datas

    int OtdrEventsNum;//total num of OTDR test datas

    CCSVOperator CSVOperator;

    if(!CSVOperator.LoadCSV("file.csv"))

    {

        printf("没有找到csv文档,请将csv文档放在本目录下!\n");

        system("pause");

        return false;

    }



    CreateDirectory("OTDR_Test_Out",NULL);

    if( !SetCurrentDirectory("OTDR_Test_Out"))

    {

        printf("设置输出目录失败!\n");

    }

    

    //OTDR Information

    ofstream out1("OTDR_Information.txt");

    u32 uiRow=1,uiColumn=1;

    out1<<CSVOperator.GetString(1,1)->c_str()<<": "<<CSVOperator.GetString(1,2)->c_str()<<'\n';

    out1<<CSVOperator.GetString(2,1)->c_str()<<'\n';

    out1<<CSVOperator.GetString(3,1)->c_str()<<"\n\n";

    out1<<CSVOperator.GetString(4,1)->c_str()<<": "<<CSVOperator.GetString(4,2)->c_str()<<'\n';

    for(uiRow=5;uiRow<=6;uiRow++)

    {

        for(uiColumn=1;uiColumn<=7;uiColumn++)

        {

            out1<<setiosflags(ios::left)<<setw(22)<<CSVOperator.GetString(uiRow,uiColumn)->c_str();

        }

        out1<<'\n';

    }

    out1<<'\n'<<CSVOperator.GetString(uiRow++,1)->c_str()<<": "<<CSVOperator.GetString(uiRow,2)->c_str()<<'\n';

    for(;uiRow<=9;uiRow++)

    {

        for(uiColumn=1;uiColumn<=3;uiColumn++)

        {

            out1<<setiosflags(ios::left)<<setw(22)<<CSVOperator.GetString(uiRow,uiColumn)->c_str();

        }

        out1<<'\n';

    }



    //OTDR Test Datas

    ofstream out2("OTDR_Test_Datas.txt");

    out2<< CSVOperator.GetString(10,1)->c_str() << ':' << ' ' << CSVOperator.GetString(10,2)->c_str() ;

    CSVOperator.GetInt(10,2,OtdrTestsNum);//get datas num

    out2 << '\n' << CSVOperator.GetString(11,1)->c_str();

    for(uiRow=12;uiRow<OtdrTestsNum+12;uiRow++)

    {

        out2 << '\n' << uiRow-11 <<' ' << CSVOperator.GetString(uiRow,1)->c_str();

    }



    //OTDR Events

    ofstream out3("OTDR_Events.txt");

    out3<< CSVOperator.GetString(12+OtdrTestsNum,1)->c_str()<<':'<<' '<< CSVOperator.GetString(12+OtdrTestsNum,2)->c_str();

    CSVOperator.GetInt(OtdrTestsNum+12,2,OtdrEventsNum);//get Events num

    for(uiRow=12+OtdrTestsNum+1;uiRow<12+OtdrTestsNum+OtdrEventsNum+1;uiRow++)

    {

        out3 << '\n';

        for(uiColumn=1;uiColumn<=3;uiColumn++)

        {

            std::string* pString = CSVOperator.GetString(uiRow,uiColumn);

            if (pString)

            {

                if((uiColumn%3)!=0)

                    out3<<setiosflags(ios::left)<<setw(16)<<pString->c_str();

                else

                    out3<<pString->c_str();

            }

        }

    }

    return 0;

}

 

你可能感兴趣的:(csv)