00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef OCIMPORTPARSER_H
00012 #define OCIMPORTPARSER_H
00013
00014 struct ocFileCol
00015 {
00016 string name;
00017 string value;
00018 int iMapPos;
00019 ocFileCol():iMapPos(0L){;}
00020 ocFileCol( const ocFileCol & in )
00021 :name(in.name),value(in.value),iMapPos(in.iMapPos){;}
00022 ~ocFileCol(){;}
00023 ocFileCol & operator = (const ocFileCol & in )
00024 {
00025 name = in.name;
00026 value = in.value;
00027 iMapPos = in.iMapPos;
00028 return * this;
00029 }
00030 };
00031
00032 typedef vector<ocFileCol> ocFileCols;
00033
00034 class ocFileParser
00035 {
00036 string cDelim;
00037 string cField;
00038 int colCount;
00039 ocFileCols cols;
00040 ifstream inFile;
00041 ocString lineTokens;
00042 bool bTrimQuote;
00043 bool bCheckQuote;
00044 bool notDone;
00045
00046 public:
00047
00048 string attrSep;
00049 ocFileParser()
00050 :colCount(0),cDelim(","),cField("\""),cols(100)
00051 ,bTrimQuote(false),bCheckQuote(false)
00052 ,notDone(false),attrSep("~|~")
00053 {;}
00054 ~ocFileParser(){;}
00055
00056
00057 void clearData( void )
00058 {
00059 for( int i=0; i < cols.size(); i++ ) cols[i].value = "";
00060 }
00061
00062 bool openFile( string filePath )
00063 {
00064 inFile.open(filePath.c_str());
00065 return( inFile.is_open() );
00066 }
00067 bool closeFile( void )
00068 {
00069 if( inFile.is_open() )
00070 {
00071 inFile.close();
00072 inFile.clear();
00073 cols.clear();
00074 }
00075 return true;
00076 }
00077
00078 bool isEven( int cnt )
00079 {
00080 return cnt%2 == 0;
00081 }
00082
00083 bool getLine ( void )
00084 {
00085 bool bret = false;
00086 lineTokens = "";
00087 bool eol = false;
00088 char ch;
00089
00090
00091 int quoteCount = 0;
00092
00093
00094 if( inFile.is_open() )
00095 {
00096 while( inFile.eof() == false
00097 && eol == false )
00098 {
00099 ch = inFile.get();
00100
00101 if( ch == EOF ) break;
00102
00103
00104 if( ch == '\"' && bCheckQuote )
00105 {
00106 ++quoteCount;
00107 }
00108
00109 lineTokens += ch;
00110
00111 int len = lineTokens.length();
00112 if( len
00113 && lineTokens[len-1] == '\n'
00114 && isEven(quoteCount)
00115 )
00116 {
00117 lineTokens.resize(len-1);
00118 eol = true;
00119 len = lineTokens.length();
00120 if( lineTokens[len-1] == '\r' )
00121 {
00122 lineTokens.resize(len-1);
00123 }
00124 }
00125 }
00126 }
00127 if(lineTokens.length())
00128 {
00129 bret = true;
00130 }
00131 return bret;
00132 }
00133 bool parseLine( bool isLabel = false )
00134 {
00135 bool bret = false;
00136 if( getLine() )
00137 {
00138 bret = parse(isLabel);
00139 }
00140 return bret;
00141 }
00142 void trimQuote( string & tok )
00143 {
00144 int len = tok.length();
00145 if( len )
00146 {
00147 if( tok[0] == '\"' )
00148 {
00149 tok.erase(0,1);
00150 }
00151 len = tok.length();
00152 if( len )
00153 {
00154 if( tok[len-1] == '\"' )
00155 {
00156 tok.resize(len-1);
00157 }
00158 }
00159 }
00160 }
00161
00162 bool hasEvenQuotes( string & tok )
00163 {
00164 int cnt = 0;
00165 for( int i = 0; i < tok.length(); i++ )
00166 {
00167 if( tok[i] == '\"' ) ++cnt;
00168 }
00169 return isEven(cnt);
00170 }
00171
00172
00173
00174 bool checkQuote( string & tok )
00175 {
00176 int len = tok.length();
00177 bool evenQuotes = hasEvenQuotes(tok);
00178 bool bret = false;
00179 if( len )
00180 {
00181 if( tok[0] == '\"' )
00182 {
00183 bret = true;
00184 if( len > 1 && tok[len-1] == '\"' && evenQuotes )
00185 {
00186 tok.erase(0,1);
00187 len = tok.length();
00188 tok.resize(len-1);
00189 }
00190 else
00191 {
00192
00193 tok += cDelim;
00194 tok += lineTokens.parse(cDelim.c_str());
00195
00196 checkQuote(tok);
00197 }
00198 }
00199 }
00200 }
00201
00202 bool parse( bool isLabel = false )
00203 {
00204 lineTokens.parseInit();
00205 int colCount = 0;
00206 clearData();
00207
00208 while(lineTokens.endOfParse() == false )
00209 {
00210 string tok = lineTokens.parse(cDelim.c_str());
00211 if( bTrimQuote )
00212 {
00213 trimQuote( tok );
00214 }
00215 else if ( bCheckQuote )
00216 {
00217 if( checkQuote( tok ) ) trimQuote( tok );
00218 }
00219 ocFileCol & rfc = cols[colCount];
00220 if(isLabel)
00221 {
00222 rfc.name = tok;
00223 }
00224 else
00225 {
00226
00227
00228 if(rfc.value.length())
00229 {
00230 rfc.value += attrSep;
00231 }
00232 rfc.value += tok;
00233 }
00234 rfc.iMapPos = colCount;
00235 colCount ++;
00236 }
00237
00238 if( isLabel ) this->colCount = colCount;
00239
00240 return true;
00241 }
00242 inline int getColCount(void)
00243 {
00244 return colCount;
00245 }
00246 inline void setColCount( int iCount )
00247 {
00248 colCount = iCount ;
00249 }
00250 inline ocFileCols & getCols( void )
00251 {
00252 return cols;
00253 }
00254 ocFileParser & setDelim(string in)
00255 {
00256 cDelim = in;
00257 return * this;
00258 }
00259 ocFileParser & setTrimQuote( bool in )
00260 {
00261 bTrimQuote = in;
00262 return * this;
00263 }
00264 ocFileParser & setCheckQuote( bool in )
00265 {
00266 bCheckQuote = in;
00267 return * this;
00268 }
00269 ocFileParser & setLineTokens( string in )
00270 {
00271 lineTokens = in;
00272 return * this;
00273 }
00274 string getLineTokens( void )
00275 {
00276 return lineTokens;
00277 }
00278 };
00279
00280 #endif