Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

GetDefaultValue.h

Go to the documentation of this file.
00001 //-----------------------------------------------------------------------------
00002 //
00003 //  originally taken from:
00004 //
00005 //  COOL Program Library  
00006 //  Copyright (C) CERES collaboration, 1996
00007 //
00008 //  Returns resource value read from file. If not available uses default
00009 //  value passed as argument.
00010 //  The resource line must be given as:
00011 //          resource: value
00012 //  Comments beginning with '!' or '#' can be added to the resource file.
00013 //
00014 //  05-aug-99 ap: modified to not use RW any longer (use SimpleTokenizer instead)
00015 //                limitation for T=string: returns only the first token after the name
00016 //
00017 //  10-aug-99 ap: added some further readers to increase flexibility (by G. Dissertori)
00018 //
00019 //  summer 2001 : added some further readers to handle very specific cases (by C. Delaere)
00020 //
00021 //
00022 //-----------------------------------------------------------------------------
00023 
00024 #ifndef _GETDEFAULT_H_
00025 #define _GETDEFAULT_H_
00026 
00027 #include <fstream>
00028 
00029 #ifdef WIN32
00030 # include <strstrea.h>
00031 #else
00032 # include <strstream.h>
00033 #endif /* ifndef WIN32 */
00034 
00035 #include <vector>
00036 
00037 #include "SimpleTokenizer.h"
00038 
00039 
00040 //-----------------------------------------------------------------------------
00041 //-----------------------------------------------------------------------------
00042 //-----------------------------------------------------------------------------
00043 
00044 
00046 template<class T>
00047 T GetDefaultValue(const char* filename, const char* inName, const T& defv)
00048 {
00049    T value = defv;
00050    ifstream ifs(filename);
00051    
00052    String name(inName);
00053 
00054    SimpleTokenizer *next;
00055    vector<String> tokens;
00056 
00057    char buffer[512];
00058    ifs.getline(buffer,512);
00059    String *line = new String(buffer);
00060 
00061    while (!ifs.eof()) {
00062       next = new SimpleTokenizer(" :\t", "!#\n\0"); // token delimiters: <space>, ":" or <tab>
00063                                                     // terminators: "!", "#", "\n", "\0"
00064       tokens = next->tokenize(*line);
00065       if (tokens.size() > 1) {
00066         if (tokens[0] == name) {                    // resource found
00067           istrstream ist( tokens[1].c_str(), tokens[1].size() );
00068           ist >> value;                             // let istream do the type conversion
00069           delete next;                              // remove old tokenizer
00070           tokens.clear();
00071           break;
00072         }
00073       }
00074       tokens.clear();
00075       delete next;                                  // remove old tokenizer
00076       
00077       delete line;                                  // get rid of old content
00078       ifs.getline(buffer,512);
00079       line = new String(buffer);
00080    }
00081    delete line;
00082    return value;
00083 }
00084 
00085 
00086 //-----------------------------------------------------------------------------
00087 //-----------------------------------------------------------------------------
00088 //-----------------------------------------------------------------------------
00089 
00091 template<class T>
00092 vector<T> GetManyDefaultValues(const char* filename, const char* inName, const T& defv)
00093 {
00094    T value = defv;
00095    ifstream ifs(filename);
00096    
00097    String name(inName);
00098 
00099    SimpleTokenizer *next;
00100    vector<String> tokens;
00101    vector<T> ValueVector;
00102 
00103    char buffer[512];
00104    ifs.getline(buffer,512);
00105    String *line = new String(buffer);
00106 
00107 
00108    next = new SimpleTokenizer(" :\t", "!#\n\0"); // token delimiters: <space>, ":" or <tab>
00109                                                     // terminators: "!", "#", "\n", "\0"
00110    while (!ifs.eof()) {
00111       tokens = next->tokenize(*line);
00112       if (tokens.size() > 1) {
00113         if (tokens[0] == name) {                    // resource found
00114           istrstream ist( tokens[1].c_str(), tokens[1].size() );
00115           ist >> value;                             // let istream do the type conversion
00116           
00117           tokens.clear();
00118           ValueVector.push_back(value);
00119         }
00120       }
00121       tokens.clear();
00122       
00123       delete line;                                  // get rid of old content
00124       ifs.getline(buffer,512);
00125       line = new String(buffer);
00126    }
00127    delete next;                                  // remove old tokenizer
00128    delete line;
00129 
00130    return ValueVector;
00131 }
00132 
00133 
00134 //-----------------------------------------------------------------------------
00135 //-----------------------------------------------------------------------------
00136 //-----------------------------------------------------------------------------
00137 
00140 template<class T>
00141 vector<T> GetFILIDefaultValues(const char* filename, const char* inName, const T& defv)
00142 {
00143    T value = defv;
00144    ifstream ifs(filename);
00145    
00146    String name(inName);
00147 
00148    SimpleTokenizer *next;
00149    vector<String> tokens;
00150    vector<T> ValueVector;
00151 
00152    char buffer[512];
00153    ifs.getline(buffer,512);
00154    String *line = new String(buffer);
00155 
00156 
00157    next = new SimpleTokenizer("'", "!#\n\0"); // token delimiters: "'"
00158                                                     // terminators: "!", "#", "\n", "\0"
00159    while (!ifs.eof()) {
00160       tokens = next->tokenize(*line);
00161       if (tokens.size() > 1) {
00162         if (tokens[0] == name) {                    // resource found
00163           char output[512];
00164           strncpy(output,tokens[1].c_str(),tokens[1].size());
00165           output[tokens[1].size()]=0;
00166           tokens.clear();
00167           value = output;
00168           ValueVector.push_back(value);
00169         }
00170       }
00171       tokens.clear();
00172       
00173       delete line;                                  // get rid of old content
00174       ifs.getline(buffer,512);
00175       line = new String(buffer);
00176    }
00177    delete next;                                  // remove old tokenizer
00178    delete line;
00179 
00180    return ValueVector;
00181 }
00182 
00183 
00184 //-----------------------------------------------------------------------------
00185 //-----------------------------------------------------------------------------
00186 //-----------------------------------------------------------------------------
00187 
00190 template<class T>
00191 vector<T> GetSRUNDefaultValues(const char* filename, const char* inName, const vector<T>& defv)
00192 {
00193    ifstream ifs(filename);
00194    
00195    String name(inName);
00196 
00197    SimpleTokenizer *next;
00198    vector<String> tokens;
00199    vector<T> ValueVector;
00200    T value;
00201    char buffer[512];
00202    ifs.getline(buffer,512);
00203    String *line = new String(buffer);
00204 
00205    while (!ifs.eof()) 
00206    {
00207       next = new SimpleTokenizer(" :\t", "!#\n\0"); // token delimiters: <space>, ":" or <tab>
00208                                                     // terminators: "!", "#", "\n", "\0"
00209       tokens = next->tokenize(*line);
00210       if (tokens.size() > 1) 
00211       {
00212         if (tokens[0] == name) 
00213         {                   // resource found
00214                 // loop over tokens
00215           for (uint i=1; i<tokens.size(); i++)
00216             {
00217               istrstream ist( tokens[i].c_str(), tokens[i].size() );
00218               ist >> value;           // let istream do the type conversion
00219 
00220               ValueVector.push_back(value);
00221             }
00222           delete next;                              // remove old tokenizer
00223           tokens.clear();
00224           delete line;
00225           ifs.getline(buffer,512);                  // read the next line
00226           line = new String(buffer);
00227           while (!ifs.eof()) 
00228           {
00229              next = new SimpleTokenizer(" :\t", "!#\n\0");  // token delimiters: <space>, ":" or <tab>
00230                                                             // terminators: "!", "#", "\n", "\0"
00231              tokens = next->tokenize(*line);
00232              if (tokens.size() > 0)                // loop over tokens until: EOF or no new value. 
00233              {
00234                      for (uint i=0; i<tokens.size(); i++)
00235                      {
00236                              istrstream ist( tokens[i].c_str(), tokens[i].size() );
00237                              ist >> value;
00238                              if(value == T(0)) 
00239                              {
00240                                      delete next;
00241                                      tokens.clear();
00242                                      delete line;
00243                                      return ValueVector;
00244                              }
00245                              ValueVector.push_back(value);
00246                      }
00247              }
00248              else
00249              {
00250                      delete next;
00251                      tokens.clear();
00252                      delete line;
00253                      return ValueVector;
00254              }
00255              tokens.clear();
00256              delete next;                                  // remove old tokenizer
00257              delete line;                                  // get rid of old content
00258              ifs.getline(buffer,512);
00259              line = new String(buffer);
00260           }
00261           delete line;
00262           return ValueVector;
00263         }
00264       }
00265       tokens.clear();
00266       delete next;                                  // remove old tokenizer
00267       delete line;                                  // get rid of old content
00268       ifs.getline(buffer,512);
00269       line = new String(buffer);
00270    }
00271    delete line;
00272    return defv;
00273 }
00274 
00275 //-----------------------------------------------------------------------------
00276 //-----------------------------------------------------------------------------
00277 //-----------------------------------------------------------------------------
00278 
00281 template<class T>
00282 vector<T> GetDefaultValueVector(const char* filename, const char* inName, const vector<T>& defv)
00283 {
00284    ifstream ifs(filename);
00285    
00286    String name(inName);
00287 
00288    SimpleTokenizer *next;
00289    vector<String> tokens;
00290    vector<T> ValueVector;
00291    T value;
00292    char buffer[512];
00293    ifs.getline(buffer,512);
00294    String *line = new String(buffer);
00295 
00296    while (!ifs.eof()) {
00297       next = new SimpleTokenizer(" :\t", "!#\n\0"); // token delimiters: <space>, ":" or <tab>
00298                                                     // terminators: "!", "#", "\n", "\0"
00299       tokens = next->tokenize(*line);
00300       if (tokens.size() > 1) {
00301         if (tokens[0] == name) {                    // resource found
00302 
00303           // loop over tokens
00304           for (uint i=1; i<tokens.size(); i++)
00305             {
00306               istrstream ist( tokens[i].c_str(), tokens[i].size() );
00307               ist >> value;           // let istream do the type conversion
00308 
00309               ValueVector.push_back(value);
00310             }
00311           delete next;                              // remove old tokenizer
00312           tokens.clear();
00313           delete line;
00314           return ValueVector;
00315         }
00316       }
00317       tokens.clear();
00318       delete next;                                  // remove old tokenizer
00319       
00320       delete line;                                  // get rid of old content
00321       ifs.getline(buffer,512);
00322       line = new String(buffer);
00323    }
00324    delete line;
00325    return defv;
00326 }
00327 
00328 //-----------------------------------------------------------------------------
00329 //-----------------------------------------------------------------------------
00330 //-----------------------------------------------------------------------------
00331 
00335 template<class T>
00336 vector< pair< String, vector<T> > > GetUserDefaultValues(const char* filename, const char* inName, T dummy)
00337 {
00338    T value ;
00339    typedef vector<T> Tvector;
00340    typedef pair<String,Tvector> element;
00341    ifstream ifs(filename);
00342    
00343    String name(inName);
00344    String subname;
00345    
00346    SimpleTokenizer *next;
00347    vector<String> tokens;
00348    vector<T> ValueVector;
00349 
00350    element processed;
00351    vector<element> output;
00352    
00353    char buffer[512];
00354    ifs.getline(buffer,512);
00355    String *line = new String(buffer);
00356 
00357 
00358    next = new SimpleTokenizer(" :\t", "!#\n\0");    // token delimiters: <space>, ":" or <tab>
00359                                                     // terminators: "!", "#", "\n", "\0"
00360    while (!ifs.eof()) 
00361    {
00362       tokens = next->tokenize(*line);
00363       if (tokens.size() > 2) 
00364       {
00365         if (tokens[0] == name)                      // resource found
00366         {
00367           subname =  tokens[1];                     // the subname (user-defined)
00368           for (uint i=2; i<tokens.size(); i++)      // loop over tokens
00369             {
00370               istrstream ist( tokens[i].c_str(), tokens[i].size() );
00371               ist >> value;                         // let istream do the type conversion
00372               ValueVector.push_back(value);
00373             }
00374           processed.first  = subname;
00375           processed.second = ValueVector;
00376           output.push_back(processed);
00377         }
00378       }
00379       ValueVector.clear();
00380       tokens.clear();
00381       delete line;                                  // get rid of old content
00382       ifs.getline(buffer,512);
00383       line = new String(buffer);
00384    }
00385    delete next;                                     // remove old tokenizer
00386    delete line;
00387 
00388    return output;
00389 }
00390 
00391 
00392 
00393 //-----------------------------------------------------------------------------
00394 //-----------------------------------------------------------------------------
00395 //-----------------------------------------------------------------------------
00396 
00399 template<class T>
00400 void GetDefaultValue(const char*  filename, const char* inName, T* value, const int nitems)
00401 {  
00402    int i;
00403    ifstream ifs(filename);
00404    SimpleTokenizer *next;
00405    vector<String> tokens;
00406 
00407    String name(inName);
00408 
00409    char buffer[512];
00410    ifs.getline(buffer,512);
00411    String *line = new String(buffer);
00412    
00413    while (!ifs.eof()) {
00414       next = new SimpleTokenizer(" :\t", "!#\n\0"); // token delimiters: <space>, ":" or <tab>
00415                                                     // terminators: "!", "#", "\n", "\0"
00416       tokens = next->tokenize(*line);
00417       if (tokens.size() > 1) {
00418         if (tokens[0] == name) {                    // resource found
00419           for (i=0; i<nitems; i++) {
00420             if (tokens.size() > size_t(i) ) {
00421               istrstream ist( tokens[i+1].c_str(), tokens[i+1].size() );
00422               ist >> value[i];                        // let istream do the type conversion
00423             }
00424           }
00425          delete next;
00426          tokens.clear();
00427          break;
00428         }
00429       }
00430       delete next;                                // remove old tokenizer
00431       tokens.clear();
00432 
00433       delete line;                                  // get rid of old content
00434       ifs.getline(buffer,512);
00435       line = new String(buffer);
00436    }
00437   delete line;
00438 }
00439 
00440 
00441 #endif
00442 
00443 

Generated at Wed Jun 18 17:19:19 2003 for ALPHA++ by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001