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;
}
时间: 2024-11-09 03:03:25

csv2txt.cpp的相关文章

函数模版和主函数分别在.h .cpp中(要包含.cpp)

Complex.h #pragma once #include<iostream> using namespace std;//这句还必须加,要不然致错,不懂为啥呢 template <typename T> class Complex { public: Complex( T a); ~Complex(); Complex operator + (Complex & c1); public: friend ostream & operator << &

QThread的源码(直接搜索&quot;thread.cpp&quot;即可,或者在github里搜)

void QThread::run() { (void) exec(); } int QThread::exec() { Q_D(QThread); QMutexLocker locker(&d->mutex); d->data->quitNow = false; if (d->exited) { d->exited = false; return d->returnCode; } locker.unlock(); QEventLoop eventLoop; i

Caffe 源碼閱讀(三) caffe.cpp

從main函數說起: 1.gflags庫中爲main函數設置usage信息 是google的一個開源的處理命令行的參數的庫.在使用命令行參數的文件夾文件中(源文件或頭文件),首先使用以下定義語句進行變量的定義. DEFINE_int32, DEFINE_int64, DEFINE_bool等, 語法爲:DEFINE_int32(name,default_value,"description").接着你就可以使用FLAGS_name變量了,這些變量的值則是由命令行參數傳遞,無則爲默認值,

[hge] distort.h distort.cpp

荡漾   --写在开头 在两个文件组合起来要实现的功能就是使得一个画片图水波般荡漾 首先来看头文件中的东西 Chapter I: distort.h Part Ⅰ:attributes private: hgeDistortionMesh(); // 构造函数,话说放在 private 是为了屏蔽这个接口,使得初始化过程中必须有参数 static HGE *hge; // 引擎指针 hgeVertex *disp_array; // 顶点数组 int nRows, nCols; // 行数.列数

map——映射(message.cpp)

信息交换 (message.cpp) [题目描述] Byteland战火又起,农夫John派他的奶牛潜入敌国获取情报信息. Cow历尽千辛万苦终于将敌国的编码规则总结如下: 1 编码是由大写字母组成的字符串. 2 设定了密字.加密的过程就是把原信息的字母替换成对应密字. 3 一个字母有且仅有一个对应密字,不同字母对应不同密字. 如今,Cow终于获取了敌国发送的一条加密信息和对应的原信息.Cow如下破解密码:扫描原信息,对于原信息中的字母x,找到它在加密信息中的对应大写字母y,且认为y是x的密字.

Identify Memory Leaks in Visual CPP Applications(VLD内存泄漏检测工具)

原文地址:http://www.codeproject.com/Articles/1045847/Identify-Memory-Leaks-in-Visual-CPP-Applications 基于CPOL License Identify Memory Leaks in Visual CPP Applications Visual Leak Detector (VLD) is an easy to use memory leak detection system. The installat

[C/CPP系列知识] C++中extern “C” name mangling -- Name Mangling and extern “C” in C++

http://www.geeksforgeeks.org/extern-c-in-c/ C++函数重载(function overloading),但是C++编译器是如何区分不同的函数的呢?----是通过在函数名是加些信息来区不同的函数,即所谓的Name Mangling.C++标准并没有对name mangling技术,各个编译器可以添加不同的信息. 考虑下面的函数 int f (void) { return 1; } int f (int) { return 0; } void g (voi

如何在安卓环境下自动编译所有cpp文件

正常情况下,需要在Android.mk文件下面一个一个手动添加cpp文件,如果文件较多,这样就太麻烦了. 解决办法如下: 把Android.mk文件里面的这段代码: LOCAL_SRC_FILES := hellocpp/main.cpp ../../Classes/AppDelegate.cpp 改为: FILE_LIST := hellocpp/main.cpp FILE_LIST += $(wildcard $(LOCAL_PATH)/../../Classes/*.cpp) LOCAL_

c/cpp中怎样切割字符串,相似于split的功能

在python中,假设要求当前时间的unix时间戳,我特别喜欢这么用: import time timestr = time.time() timestamp = int(timestr.split('.')[0]) 这里的split函数,我非常喜欢,在java.c#和python中都有,非常方便,不用操心踩地雷,可是C/CPP中,就没有了,这点比較遗憾. 假设要处理一个字符串型的"192.168.1.254",想把每一个字段都分开,怎么办呢,C标准库中有函数strtok()的实现,能