00001 #ifndef DATA_H
00002 #define DATA_H
00003
00004 #include <string>
00005 #include <iostream>
00006 #include <fstream>
00007 #include <vector>
00008 #include <cstdlib>
00009
00010 namespace mimas {
00011
00012 template <typename T> class data;
00013
00014 template <typename T>
00015 ostream &operator<<(ostream &output, const data<T> &outdata);
00016
00017
00028 template <typename T>
00029 class data
00030 {
00031 friend ostream &operator<<(ostream &output, const data<T> &outdata);
00032
00033 private:
00034 bool getHeader(ifstream &infile);
00035 string toLower(string &instring);
00036 int getIntAfterString(ifstream &infile, const char *cheader);
00037 void stripComment(string &instring);
00038 void trimDelimeters(string &instring);
00039 void trimWhiteSpaces(string &instring);
00040
00041 float refNumber;
00042
00043 protected:
00044 int numInputTuples, numOutputTuples;
00045 int numLines;
00046 vector < vector <T> > inputTuple;
00047 vector < vector <T> > outputTuple;
00048
00049 public:
00050 int readFromFile(const char *fn);
00051 int writeToFile(const char *fn);
00052 T getInputValue(int line, int tuple);
00053 void setInputValue(int line, int tuple, T val);
00054 T getOutputValue(int line, int tuple);
00055 void setOutputValue(int line, int tuple, T val);
00056 void init(int lines, int inputtuples, int outputtuples);
00057 void clear(void);
00058
00059 data(void);
00060 ~data(void);
00061
00062
00063
00064 };
00065
00066
00067 template <typename T>
00068 data<T>::data(void)
00069 {
00070 numInputTuples=-1;
00071 numOutputTuples=-1;
00072 numLines=-1;
00073 }
00074
00075 template <typename T>
00076 data<T>::~data(void)
00077 {
00078 }
00079
00080 template <typename T>
00081 void data<T>::init(int lines, int inputtuples, int outputtuples)
00082 {
00083 clear();
00084 vector <T> v;
00085
00086 for (int i=0; i<inputtuples; i++)
00087 v.push_back((T)0);
00088
00089 for (int i=0; i<lines; i++)
00090 inputTuple.push_back(v);
00091
00092 v.clear();
00093
00094 for (int i=0; i<outputtuples; i++)
00095 v.push_back((T)0);
00096
00097 for (int i=0; i<lines; i++)
00098 outputTuple.push_back(v);
00099
00100 numLines=lines;
00101 numInputTuples=inputtuples;
00102 numOutputTuples=outputtuples;
00103 }
00104
00105
00106 template <typename T>
00107 void data<T>::clear(void)
00108 {
00109 for (int i=0; i<numInputTuples; i++)
00110 inputTuple[i].clear();
00111 inputTuple.clear();
00112
00113 for (int i=0; i<numOutputTuples; i++)
00114 outputTuple[i].clear();
00115 outputTuple.clear();
00116
00117 numLines=0;
00118 numInputTuples=0;
00119 numOutputTuples=0;
00120 }
00121
00122
00123 template <typename T>
00124 T data<T>::getInputValue(int line, int tuple)
00125 {
00126 T retval;
00127
00128 if ((line>=0) && (line<numLines) && (tuple>=0) && (tuple<numInputTuples))
00129 retval=inputTuple[line][tuple];
00130 else
00131 {
00132 cerr << "ERROR: data::getInputValue out of range" << std::endl;
00133 exit(1);
00134 }
00135
00136 return retval;
00137 }
00138
00139 template <typename T>
00140 T data<T>::getOutputValue(int line, int tuple)
00141 {
00142 T retval;
00143
00144 if ((line>=0) && (line<numLines) && (tuple>=0) && (tuple<numOutputTuples))
00145 retval=outputTuple[line][tuple];
00146 else
00147 {
00148 cerr << "ERROR: data::getOutputValue out of range" << std::endl;
00149 exit(1);
00150 }
00151
00152 return retval;
00153 }
00154
00155
00156
00157 template <typename T>
00158 void data<T>::setInputValue(int line, int tuple, T val)
00159 {
00160
00161 if ((line>=0) && (line<numLines) && (tuple>=0) && (tuple<numInputTuples))
00162 inputTuple[line][tuple]=val;
00163 else
00164 {
00165 cerr << "ERROR: data::setInputValue out of range" << std::endl;
00166 exit(1);
00167 }
00168
00169 }
00170
00171
00172
00173 template <typename T>
00174 void data<T>::setOutputValue(int line, int tuple, T val)
00175 {
00176
00177 if ((line>=0) && (line<numLines) && (tuple>=0) && (tuple<numOutputTuples))
00178 outputTuple[line][tuple]=val;
00179 else
00180 {
00181 cerr << "ERROR: data::setInputValue out of range" << std::endl;
00182 exit(1);
00183 }
00184
00185 }
00186
00187
00188
00189
00190 template <typename T>
00191 string data<T>::toLower(string &instring)
00192 {
00193 string lower;
00194
00195 for (int k=0; k<instring.length(); k++)
00196 lower+=tolower(instring[k]);
00197
00198 return lower;
00199 }
00200
00201 template <typename T>
00202 bool data<T>::getHeader(ifstream &infile)
00203 {
00204 string line, lcaseline;
00205 string header="# mimas datafile format";
00206
00207 while (getline(infile, line))
00208 {
00209 if (toLower(line).find(header,0)==-1) continue;
00210 cout << line << std::endl;
00211 refNumber=atof(line.substr(header.length(),line.length()-header.length()).c_str());
00212 cout << refNumber << std::endl;
00213 break;
00214 }
00215
00216 return true;
00217 }
00218
00219
00220 template <typename T>
00221 int data<T>::getIntAfterString(ifstream &infile, const char *cheader)
00222 {
00223 string line, lcaseline;
00224 string header(cheader);
00225 int retint;
00226
00227 retint=-1;
00228 while (getline(infile, line))
00229 {
00230 if (toLower(line).find(header,0)==-1) continue;
00231 cout << line << std::endl;
00232 retint=atoi(line.substr(header.length(),line.length()-header.length()).c_str());
00233 break;
00234 }
00235
00236 cout << header << std::endl;
00237 return retint;
00238 }
00239
00240 template <typename T>
00241 void data<T>::stripComment(string &instring)
00242 {
00243 string comment="#";
00244
00245 instring=instring.substr(0,instring.find(comment,0));
00246 }
00247
00248 template <typename T>
00249 void data<T>::trimDelimeters(string &instring)
00250 {
00251 string unwantedstring;
00252
00253
00254
00255 unwantedstring=',';
00256 while (instring.find(unwantedstring,0)!=-1)
00257 {
00258 int unwantedcharloc=instring.find(unwantedstring,0);
00259 instring.replace(unwantedcharloc,1,' ');
00260 }
00261
00262
00263 unwantedstring="\'";
00264 while (instring.find(unwantedstring,0)!=-1)
00265 {
00266 int unwantedcharloc=instring.find(unwantedstring,0);
00267 instring.replace(unwantedcharloc,1,' ');
00268 }
00269
00270
00271 unwantedstring="\"";
00272 while (instring.find(unwantedstring,0)!=-1)
00273 {
00274 int unwantedcharloc=instring.find(unwantedstring,0);
00275 instring.replace(unwantedcharloc,1,' ');
00276 }
00277 }
00278
00279
00280 template <typename T>
00281 void data<T>::trimWhiteSpaces(string &instring)
00282 {
00283 if (instring=="") return;
00284
00285 string unwantedstring;
00286
00287
00288 unwantedstring='\t';
00289 while (instring.find(unwantedstring,0)!=-1)
00290 {
00291 int unwantedcharloc=instring.find(unwantedstring,0);
00292 instring.replace(unwantedcharloc,1,' ');
00293 }
00294
00295
00296 unwantedstring='\r';
00297 while (instring.find(unwantedstring,0)!=-1)
00298 {
00299 int unwantedcharloc=instring.find(unwantedstring,0);
00300 instring.replace(unwantedcharloc,1,' ');
00301 }
00302
00303
00304 unwantedstring='\n';
00305 while (instring.find(unwantedstring,0)!=-1)
00306 {
00307 int unwantedcharloc=instring.find(unwantedstring,0);
00308 instring.replace(unwantedcharloc,1,' ');
00309 }
00310
00311
00312 unwantedstring=" ";
00313 while (instring.find(unwantedstring,0)!=-1)
00314 {
00315 int unwantedcharloc=instring.find(unwantedstring,0);
00316 instring.erase(unwantedcharloc,1);
00317 }
00318
00319
00320
00321 unwantedstring=" ";
00322 while (instring.find(unwantedstring,0)==0)
00323 {
00324 instring.erase(0,1);
00325 }
00326 if (instring=="") return;
00327
00328
00329 unwantedstring=' ';
00330 while (instring.find(unwantedstring,instring.length()-1)==instring.length()-1)
00331 {
00332 instring.erase(instring.length()-1,1);
00333 }
00334
00335 }
00336
00337
00338 template <typename T>
00339 ostream &operator<<(ostream &output, const data<T> &outdata)
00340 {
00341 output << "# Mimas datafile format" << std::endl;
00342 output << "# input tuples: " << outdata.numInputTuples << std::endl;
00343 output << "# output tuples: " << outdata.numOutputTuples << std::endl;
00344
00345
00346
00347 for (int l=0; l<outdata.numLines; l++)
00348 {
00349 for (int i=0; i<outdata.numInputTuples; i++)
00350 output << " " << outdata.inputTuple[l][i] ;
00351
00352 output << " " ;
00353
00354 for (int i=0; i<outdata.numOutputTuples; i++)
00355 output << " " << outdata.outputTuple[l][i] ;
00356
00357 output << std::endl;
00358
00359 }
00360 return output;
00361 }
00362
00363
00364
00365 template <typename T>
00366 int data<T>::readFromFile(const char *fn)
00367 {
00368 ifstream infile(fn);
00369 getHeader(infile);
00370
00371 numInputTuples=getIntAfterString(infile, "# input tuples:");
00372 numOutputTuples=getIntAfterString(infile, "# output tuples:");
00373
00374 cout << "Inputs : " << numInputTuples << std::endl;
00375 cout << "Outputs : " << numOutputTuples << std::endl;
00376
00377
00378 string line;
00379 int incount=0, outcount=0;
00380 numLines=0;
00381 vector<T> in,out;
00382
00383 while(getline(infile,line))
00384 {
00385 trimDelimeters(line);
00386 trimWhiteSpaces(line);
00387 stripComment(line);
00388 if (line.length()==0) continue;
00389
00390
00391 char *charline=(char *)line.c_str();
00392 char *pEnd, *pStart, *pPrev;
00393 double dblval;
00394
00395 pStart=charline;
00396 pPrev=pStart;
00397 while (strlen(pStart)!=0)
00398 {
00399 if ((outcount+incount)>=(numOutputTuples+numInputTuples))
00400 {
00401 inputTuple.push_back(in);
00402 outputTuple.push_back(out);
00403 in.clear();
00404 out.clear();
00405 incount=0;
00406 outcount=0;
00407 numLines++;
00408 }
00409
00410 dblval = strtod (pStart,&pEnd);
00411
00412 if (incount!=numInputTuples)
00413 {
00414 in.push_back((T)dblval);
00415 incount++;
00416 }
00417 else
00418 {
00419 if (outcount!=numOutputTuples)
00420 {
00421 out.push_back((T)dblval);
00422 outcount++;
00423 }
00424 }
00425
00426 cout << "extracted : " << dblval << std::endl;
00427 pPrev=pStart;
00428 pStart=pEnd;
00429
00430 if (pPrev==pStart)
00431 {
00432 cerr << "Error" << std::endl;
00433 exit(1);
00434 }
00435
00436 }
00437
00438 }
00439
00440
00441 if ((outcount+incount)>=(numOutputTuples+numInputTuples))
00442 {
00443 inputTuple.push_back(in);
00444 outputTuple.push_back(out);
00445 in.clear();
00446 out.clear();
00447 numLines++;
00448 }
00449 else
00450 cout << "# Warning: ignoring some lines due to incomplete data" << std::endl;
00451
00452 cout << numLines << " " << incount << " " << outcount << std::endl;
00453
00454 return 0;
00455 }
00456
00457 template <typename T>
00458 int data<T>::writeToFile(const char *fn)
00459 {
00460 ofstream outfile(fn);
00461
00462 outfile << (*this) ;
00463
00464 outfile.close();
00465
00466 return 0;
00467 }
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491 }
00492
00493 #endif