惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Spread Privacy
Spread Privacy
T
Threatpost
L
LINUX DO - 热门话题
Google Online Security Blog
Google Online Security Blog
I
InfoQ
大猫的无限游戏
大猫的无限游戏
博客园_首页
爱范儿
爱范儿
有赞技术团队
有赞技术团队
V
Visual Studio Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Jina AI
Jina AI
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CERT Recently Published Vulnerability Notes
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
K
Kaspersky official blog
L
LangChain Blog
G
GRAHAM CLULEY
B
Blog RSS Feed
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
Help Net Security
Help Net Security
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
雷峰网
雷峰网
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Vercel News
Vercel News
T
Tor Project blog
T
The Exploit Database - CXSecurity.com
T
Troy Hunt's Blog
W
WeLiveSecurity
T
Threat Research - Cisco Blogs

博客园 - 周伟

域名解析文件hosts文件是什么?如何修改hosts文件? PowerDesigner 使用心得 实现TOMCAT服务下一个ip绑定多域名绑定的方法 C++导入导出CSV文件 struct+vector实现树结构 WebService 随写 mysql相关文章收集 Linux下两种自动启动Tomcat的方法 linux自动启动mysql LINUX下mysql的大小写区分设置 Jdom使用指南 vector+struct在数据接口中的应用 UML用例中使用和扩展意义 枚举和字符串互转 单件模式的C++标准实现 设计模式 Add Crash Reporting to Your Applications with the CrashRpt Library 设计模式——单键模式(singleton)C++实现 Singleton的C++实现 及相关问题
CSV扩展类
周伟 · 2009-09-04 · via 博客园 - 周伟

#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;
typedef std::vector<std::string> row_t;

class CCsvEx
{
public:
 CCsvEx(void);
 virtual ~CCsvEx(void);
 
 bool Open(string sFileName,char cDelimiter);

 void Clear();

 bool GetList(string sFileName,char cDelimiter,vector<row_t> &list);

 void CSVLine_Populate(row_t &row, const string& line, char delimiter);

 unsigned long GetRowCount();

 const std::string GetItemValue(unsigned long row,
  unsigned int index);

 const std::string GetItemString(unsigned long row,
  unsigned int index);

 float GetItemFloat(unsigned long row,
  const unsigned int index);

 long GetItemLong(unsigned long row,
  const unsigned int index);

 SYSTEMTIME GetItemSTTime(unsigned long row,
  const unsigned int index);


private:
 row_t * FindRow(unsigned long findrow);
 std::vector<row_t> m_recordset;
};

#include "StdAfx.h"
#include ".\csvex.h"

CCsvEx::CCsvEx(void)
{
}

CCsvEx::~CCsvEx(void)
{
}

bool CCsvEx::Open(string sFileName,char cDelimiter)
{
 string line;
 ifstream in(sFileName.c_str());
 if (in.fail()) 
 {
  return false;
 }
 m_recordset.clear();
 row_t row;
 while(getline(in, line)  && in.good())
 {
  row.clear();
  this->CSVLine_Populate(row, line, cDelimiter);
  m_recordset.push_back(row);
 }
 in.close();
 return true;
}

void CCsvEx::CSVLine_Populate(row_t &row, const string& line, char delimiter)
{
 int linepos=0;
 int inquotes=false;
 char c;
 int i;
 int linemax=line.length();
 string curstring;
 row.clear();

 while(line[linepos]!=0 && linepos < linemax)
 {

  c = line[linepos];

  if (!inquotes && curstring.length()==0 && c=='"')
  {
   //beginquotechar
   inquotes=true;
  }
  else if (inquotes && c=='"')
  {
   //quotechar
   if ( (linepos+1 <linemax) && (line[linepos+1]=='"') )
   {
    //encountered 2 double quotes in a row (resolves to 1 double quote)
    curstring.push_back(c);
    linepos++;
   }
   else
   {
    //endquotechar
    inquotes=false;
   }
  }
  else if (!inquotes && c==delimiter)
  {
   //end of field
   row.push_back( curstring );
   curstring="";
  }
  else if (!inquotes && (c=='\r' || c=='\n') )
  {
   row.push_back( curstring );
   return;
  }
  else
  {
   curstring.push_back(c);
  }
  linepos++;
 }
 row.push_back( curstring );
 return;
}

unsigned long CCsvEx::GetRowCount()
{
 return m_recordset.size();
}

row_t * CCsvEx::FindRow(unsigned long findrow)
{
 if(findrow >= this->GetRowCount() || m_recordset.size() == 0)
 {
  return NULL;
 }

 return &m_recordset[findrow];
}

const std::string CCsvEx::GetItemValue(unsigned long row,
           unsigned int index)
{
 row_t * rowvalue = FindRow(row);
 if(rowvalue ==  NULL)
 {
  return "";
 }

 return (*rowvalue)[index];
}

const std::string CCsvEx::GetItemString(unsigned long row,
        unsigned int index)
{
 return GetItemValue(row,index);
}

float CCsvEx::GetItemFloat(unsigned long row,
       const unsigned int index)
{
 return atof(GetItemValue(row,index).c_str());
}

long CCsvEx::GetItemLong(unsigned long row,
     const unsigned int index)
{
 return atol(GetItemValue(row,index).c_str());
}

SYSTEMTIME CCsvEx::GetItemSTTime(unsigned long row,
       const unsigned int index)
{
 SYSTEMTIME sysTime;
 try
 {
  COleDateTime oleTime;
  oleTime.ParseDateTime(GetItemValue(row,index).c_str()); 
  VariantTimeToSystemTime(oleTime, &sysTime);
 }
 catch (...)
 {
  GetSystemTime(&sysTime);
 }
 return sysTime;
}

bool CCsvEx::GetList(string sFileName,char cDelimiter,vector<row_t> &list)
{
 string line;
 ifstream in(sFileName.c_str());
 if (in.fail()) 
 {
  return false;
 }
 row_t row;
 while(getline(in, line)  && in.good())
 {
  row.clear();
  this->CSVLine_Populate(row, line, cDelimiter);
  list.push_back(row);
 }
 in.close();
}