#include <CSVParse.h>
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 |
Definition at line 26 of file CSVParse.h.
CCSVParse::CCSVParse | ( | ) |
CCSVParse::~CCSVParse | ( | ) | [virtual] |
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.
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 }
string CCSVParse::getfield | ( | int | n | ) |
int CCSVParse::getnfield | ( | ) | const [inline] |
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 }
int CCSVParse::endofline | ( | char | c | ) | [private] |
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 }
ifstream CCSVParse::fin [private] |
Definition at line 40 of file CSVParse.h.
Referenced by CloseFile(), endofline(), getline(), and OpenFile().
string CCSVParse::line [private] |
vector<string> CCSVParse::field [private] |
int CCSVParse::nfield [private] |
string CCSVParse::fieldsep [private] |