CCSVParse Class Reference

A class to parse Comma Seperated Values input. More...

#include <CSVParse.h>

List of all members.

Public Member Functions

void CloseFile ()
bool OpenFile (string inFile, string sep)
 CCSVParse ()
virtual ~CCSVParse ()
int getline (string &)
string getfield (int n)
int getnfield () const

Private Member Functions

int split ()
int endofline (char)
int advplain (const string &line, string &fld, int)
int advquoted (const string &line, string &fld, int)

Private Attributes

ifstream fin
string line
vector< string > field
int nfield
string fieldsep


Detailed Description

A class to parse Comma Seperated Values input.

Definition at line 26 of file CSVParse.h.


Constructor & Destructor Documentation

CCSVParse::CCSVParse (  ) 

Definition at line 19 of file CSVParse.cpp.

00020 {
00021                 
00022 }

CCSVParse::~CCSVParse (  )  [virtual]

Definition at line 24 of file CSVParse.cpp.

00025 {
00026 
00027 }


Member Function Documentation

void CCSVParse::CloseFile (  ) 

Definition at line 40 of file CSVParse.cpp.

References fin.

Referenced by CConfigData::ExtractData().

00041 {
00042         fin.close();
00043 }

bool CCSVParse::OpenFile ( string  inFile,
string  sep 
)

Definition at line 29 of file CSVParse.cpp.

References fieldsep, and fin.

Referenced by CConfigData::ReadData().

00030 {
00031         fin.open( inFile.c_str(), ios::in );
00032 
00033         if(!fin)
00034                 return false;
00035         
00036         fieldsep = sep;
00037         return true;
00038 }

int CCSVParse::getline ( string &  str  ) 

Definition at line 60 of file CSVParse.cpp.

References endofline(), fin, line, and split().

Referenced by CConfigData::GetNextDataLine().

00061 {       
00062         char c;
00063 
00064         for (line = ""; fin.get(c) && !endofline(c); )
00065                 line += c;
00066         split();
00067         str = line;
00068         return !fin.eof();
00069 }

Here is the call graph for this function:

string CCSVParse::getfield ( int  n  ) 

Definition at line 132 of file CSVParse.cpp.

References field, and nfield.

Referenced by CConfigData::stringToColour().

00133 {
00134         if (n < 0 || n >= nfield)
00135                 return "";
00136         else
00137                 return field[n];
00138 }

int CCSVParse::getnfield (  )  const [inline]

Definition at line 37 of file CSVParse.h.

References nfield.

00037 { return nfield; }

int CCSVParse::split (  )  [private]

Definition at line 72 of file CSVParse.cpp.

References advplain(), advquoted(), field, line, and nfield.

Referenced by getline().

00073 {
00074         string fld;
00075         int i, j;
00076 
00077         nfield = 0;
00078         if (line.length() == 0)
00079                 return 0;
00080         i = 0;
00081 
00082         do {
00083                 if (i < line.length() && line[i] == '"')
00084                         j = advquoted(line, fld, ++i);  // skip quote
00085                 else
00086                         j = advplain(line, fld, i);
00087                 if (nfield >= field.size())
00088                         field.push_back(fld);
00089                 else
00090                         field[nfield] = fld;
00091                 nfield++;
00092                 i = j + 1;
00093         } while (j < line.length());
00094 
00095         return nfield;
00096 }

Here is the call graph for this function:

int CCSVParse::endofline ( char  c  )  [private]

Definition at line 46 of file CSVParse.cpp.

References fin.

Referenced by getline().

00047 {
00048         int eol;
00049 
00050         eol = (c=='\r' || c=='\n');
00051         if (c == '\r') {
00052                 fin.get(c);
00053                 if (!fin.eof() && c != '\n')
00054                         fin.putback(c); // read too far
00055         }
00056         return eol;
00057 }

int CCSVParse::advplain ( const string &  line,
string &  fld,
int  i 
) [private]

Definition at line 119 of file CSVParse.cpp.

References fieldsep.

Referenced by split().

00120 {
00121         int j;
00122 
00123         j = s.find_first_of(fieldsep, i); // look for separator
00124         if (j > s.length())               // none found
00125                 j = s.length();
00126         fld = string(s, i, j-i);
00127         return j;
00128 }

int CCSVParse::advquoted ( const string &  line,
string &  fld,
int  i 
) [private]

Definition at line 99 of file CSVParse.cpp.

References fieldsep.

Referenced by split().

00100 {
00101         int j;
00102 
00103         fld = "";
00104         for (j = i; j < s.length(); j++) {
00105                 if (s[j] == '"' && s[++j] != '"') {
00106                         int k = s.find_first_of(fieldsep, j);
00107                         if (k > s.length())     // no separator found
00108                                 k = s.length();
00109                         for (k -= j; k-- > 0; )
00110                                 fld += s[j++];
00111                         break;
00112                 }
00113                 fld += s[j];
00114         }
00115         return j;
00116 }


Member Data Documentation

ifstream CCSVParse::fin [private]

Definition at line 40 of file CSVParse.h.

Referenced by CloseFile(), endofline(), getline(), and OpenFile().

string CCSVParse::line [private]

Definition at line 41 of file CSVParse.h.

Referenced by getline(), and split().

vector<string> CCSVParse::field [private]

Definition at line 42 of file CSVParse.h.

Referenced by getfield(), and split().

int CCSVParse::nfield [private]

Definition at line 43 of file CSVParse.h.

Referenced by getfield(), getnfield(), and split().

string CCSVParse::fieldsep [private]

Definition at line 44 of file CSVParse.h.

Referenced by advplain(), advquoted(), and OpenFile().


The documentation for this class was generated from the following files:

Generated on Wed Aug 20 00:48:42 2008 for EvolveTraffic by  doxygen 1.5.6