CSV扩展类

#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();
}

posted on 2009-09-04 15:00  周伟  阅读(417)  评论(0编辑  收藏  举报

导航