#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, ));
        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(, ++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,,SEEK_END);
        u32 dwsize = ftell(pfile);
        rewind(pfile);

        char* filebuffer = new char[dwsize];
        fread(filebuffer, , dwsize, pfile);

        std::map<u32, std::string> StringMap;
        char* pBegin = filebuffer;
        char* pEnd = strchr(filebuffer, '\n');
        u32 uiIndex = ;
        while (pEnd != NULL)
        {
            std::string strbuff;
            strbuff.insert(, pBegin, pEnd-pBegin);
            if (!strbuff.empty())
            {
                StringMap[uiIndex] = strbuff;
            }
            pBegin = pEnd + ;
            pEnd = strchr(pEnd + , '\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);
            ; i < StringVec.size(); ++i)
            {
                l_StringMap[i+] = 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=,uiColumn=;
    out1<<CSVOperator.GetString(,)->c_str()<<,)->c_str()<<'\n';
    out1<<CSVOperator.GetString(,)->c_str()<<'\n';
    out1<<CSVOperator.GetString(,)->c_str()<<"\n\n";
    out1<<CSVOperator.GetString(,)->c_str()<<,)->c_str()<<'\n';
    ;uiRow<=;uiRow++)
    {
        ;uiColumn<=;uiColumn++)
        {
            out1<<setiosflags(ios::left)<<setw()<<CSVOperator.GetString(uiRow,uiColumn)->c_str();
        }
        out1<<'\n';
    }
    out1<<)->c_str()<<)->c_str()<<'\n';
    ;uiRow++)
    {
        ;uiColumn<=;uiColumn++)
        {
            out1<<setiosflags(ios::left)<<setw()<<CSVOperator.GetString(uiRow,uiColumn)->c_str();
        }
        out1<<'\n';
    }

    //OTDR Test Datas
    ofstream out2("OTDR_Test_Datas.txt");
    out2<< CSVOperator.GetString(,)->c_str() << ,)->c_str() ;
    CSVOperator.GetInt(,,OtdrTestsNum);//get datas num
    out2 << ,)->c_str();
    ;uiRow<OtdrTestsNum+;uiRow++)
    {
        out2 <<  <<)->c_str();
    }

    //OTDR Events
    ofstream out3("OTDR_Events.txt");
    out3<< CSVOperator.GetString(+OtdrTestsNum,)->c_str()<<+OtdrTestsNum,)->c_str();
    CSVOperator.GetInt(OtdrTestsNum+,,OtdrEventsNum);//get Events num
    +OtdrTestsNum+;uiRow<+OtdrTestsNum+OtdrEventsNum+;uiRow++)
    {
        out3 << '\n';
        ;uiColumn<=;uiColumn++)
        {
            std::string* pString = CSVOperator.GetString(uiRow,uiColumn);
            if (pString)
            {
                )!=)
                    out3<<setiosflags(ios::left)<<setw()<<pString->c_str();
                else
                    out3<<pString->c_str();
            }
        }
    }
    ;
}

csv2txt.cpp的更多相关文章

  1. 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码

    前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目. 在Cocos2d-x被整合到Cocos引 ...

  2. Json CPP 中文支持与入门示例

    在每一个Json Cpp自带*.cpp文件头加上: #include "stdafx.h" 将Json Cpp对自带的头文件的引用修改为单引号方式,例如json_reader.cp ...

  3. cpp 调用python

    在用cpp调用python时, 出现致命错误: no module named site  ,  原因解释器在搜索路径下没有找到python库.可以在调用Py_Initialize前,调用 Py_Se ...

  4. nginx+fastcgi+c/cpp

    参考:http://github.tiankonguse.com/blog/2015/01/19/cgi-nginx-three/ 跟着做了一遍,然后根据记忆写的,不清楚有没错漏步骤,希望多多评论多多 ...

  5. APM程序分析-ArduCopter.cpp

    该文件是APM的主文件. #define SCHED_TASK(func, rate_hz, max_time_micros) SCHED_TASK_CLASS(Copter, &copter ...

  6. APM程序分析-AC_WPNav.cpp

    APM程序分析 主程序在ArduCopter.cpp的loop()函数. /// advance_wp_target_along_track - move target location along ...

  7. Dev Cpp 输出中文字符问题

    最近 c++ 上机作业,vc++6.0 挂了没法用,只好用 Dev Cpp 先顶替一下,然而在遇到输出中文字符的时候出现了乱码的情况,但这种情况又非常诡异.于是简单了解了一下写成此博客. [写在前面] ...

  8. 【安卓】aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creating directories: Invalid argument

    这几天在使用.aidl文件的时候eclipse的控制台总是爆出如下提示: aidl.exe E 10744 10584 io_delegate.cpp:102] Error while creatin ...

  9. Identify Memory Leaks in Visual CPP Applications —— VLD内存泄漏检测工具

    原文地址:http://www.codeproject.com/Articles/1045847/Identify-Memory-Leaks-in-Visual-CPP-Applications 基于 ...

随机推荐

  1. 易普优APS混流排序算法助力汽车整车厂的均衡生产

    一.汽车整车厂生产排序的难点 “ 冲压-焊接-涂装-总装”是汽车整车生产的四大工艺类型,它们存在上下游关联关系,每个车间都有自己的优化排序目标,汽车混流生产模式使得生产过程更加复杂,从而生产管控的难度 ...

  2. Educational Codeforces Round 44 (Rated for Div. 2) F - Isomorphic Strings

    F - Isomorphic Strings 题目大意:给你一个长度为n 由小写字母组成的字符串,有m个询问, 每个询问给你两个区间, 问你xi,yi能不能形成映射关系. 思路:这个题意好难懂啊... ...

  3. MVC设计模式一

    一:基础知识 1.mvc model view control 2.模型 是应用程序的主体部分,模型表示业务数据与业务逻辑. 一个模型可以为多个视图提供数据 提高了代码的可重用性 3.视图 用户看到的 ...

  4. java 代码中设置 临时 环境变量

    System.setProperty("hadoop.home.dir", "D:\\software\\software_install\\dev_install\\h ...

  5. 【面试总结】2019校招京东一面二面,及深信服技术面(已拿深信服offer),还有百度一面

    百度一面: 1.自我介绍+项目介绍 2.进程和线程的区别 3.常用linux命令列举 4.堆排序 5.快速排序 反问环节. 现在的状态是岗位转推,毕竟百度投的是开发岗. 京东一面: 1.C++三大特性 ...

  6. 异步任务 -- FutureTask

    任务提交 之前在分析线程池的时候,提到过 AbstractExecutorService 的实现: public Future<?> submit(Runnable task) { if ...

  7. Spring 定时操作业务需求

    1.定时分析 在业务需求中有的需要检测用户的状态,通过对用户状态的检测做出对此状态相应的操作,如果这种检测由运营人工检测,不仅工作量大,而且准确性不高,人工无法很好的完成工作: 问题根源:在检测用户状 ...

  8. 网络数据修改工具netsed

    网络数据修改工具netsed   通过修改网络数据,可以绕过软件和防火墙的限制,达到特定的目的.Kali Linux提供一个简易数据修改工具netsed.该工具支持对TCP和UDP的数据进行修改.渗透 ...

  9. 解决python2.x文件读写编码问题

    转自: https://xrlin.github.io/%E8%A7%A3%E5%86%B3python2.x%E6%96%87%E4%BB%B6%E8%AF%BB%E5%86%99%E7%BC%96 ...

  10. [ 转载 ]学习笔记-深入剖析Java中的装箱和拆箱

    深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...