00001 /*!@file Util/readConfig.C CINNIC classes */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: T. Nathan Mundhenk <mundhenk@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/readConfig.C $ 00035 // $Id: readConfig.C 9337 2008-02-27 12:13:43Z beobot $ 00036 // 00037 00038 #include "Util/readConfig.H" 00039 00040 #include "Util/Assert.H" 00041 #include "Util/log.H" 00042 #include "Util/sformat.H" 00043 00044 #include <fstream> 00045 00046 using std::string; 00047 00048 // note: as far as I know, memory management for vectors is all automatic; 00049 // so no need to resize them 00050 00051 readConfig::readConfig() 00052 { 00053 vectorValue = 25; 00054 first.resize(vectorValue); 00055 second.resize(vectorValue); 00056 isItem.resize(vectorValue); 00057 } 00058 00059 00060 readConfig::readConfig(int size) 00061 { 00062 vectorValue = size; 00063 first.resize(vectorValue); 00064 second.resize(vectorValue); 00065 isItem.resize(vectorValue); 00066 } 00067 00068 readConfig::~readConfig() 00069 { 00070 } 00071 00072 void readConfig::openFile(const char* filename, bool echo) 00073 { 00074 // LINFO("readConfig: Parsing file...\t%s\n", filename); 00075 SiZE = 0; 00076 fileName = filename; 00077 std::ifstream inFile(filename,std::ios::in); 00078 vectorSize = vectorValue; 00079 comment = false; 00080 item = true; 00081 while (inFile >> in) 00082 { 00083 if(!in.compare("#")) //comment code # found 00084 { 00085 if(!comment) 00086 { 00087 comment = true; 00088 } 00089 else //end of comment 00090 { 00091 comment = false; 00092 } 00093 } 00094 if((!comment) && in.compare("#")) //real line found 00095 { 00096 if(item) 00097 { 00098 if(SiZE >= (vectorSize-1)) //resize vector if 00099 //more then vectorSize lines in file 00100 { 00101 //LINFO("Resizing configfile to %d",vectorSize); 00102 vectorSize+=vectorValue; 00103 first.resize(vectorSize); 00104 second.resize(vectorSize); 00105 isItem.resize(vectorSize); 00106 } 00107 if(echo == true) 00108 LINFO("item:%d %s ", SiZE, in.c_str()); 00109 first[SiZE] = in; 00110 isItem[SiZE] = true; 00111 item = false; 00112 } 00113 else 00114 { 00115 if(echo == true) 00116 LINFO("%s\n", in.c_str()); 00117 second[SiZE] = in; 00118 item = true; 00119 SiZE++; 00120 } 00121 } 00122 } 00123 } 00124 00125 void readConfig::writeFile() 00126 { 00127 std::ofstream outfile(fileName.c_str(),std::ios::out); 00128 for(int i = 0; i < SiZE; i++) 00129 { 00130 if(isItem[i]) //check to make sure this is an item 00131 { 00132 outfile << first[i] << " " << second[i] << "\n"; 00133 } 00134 } 00135 outfile.close(); 00136 } 00137 00138 void readConfig::writeFile(const char* filename) 00139 { 00140 std::ofstream outfile(filename,std::ios::out); 00141 for(int i = 0; i < SiZE; i++) 00142 { 00143 if(isItem[i]) //check to make sure this is an item 00144 { 00145 outfile << first[i] << " " << second[i] << "\n"; 00146 } 00147 } 00148 outfile.close(); 00149 } 00150 00151 bool readConfig::readFileTrue(int itemNumber) 00152 { 00153 if(itemNumber < vectorSize) //make sure we don't go beyond vector size 00154 { 00155 return isItem[itemNumber]; 00156 } 00157 else 00158 { 00159 return false; //out of bounds of vector 00160 } 00161 } 00162 00163 int readConfig::readFileValueI(int itemNumber) 00164 { 00165 ASSERT(itemNumber < SiZE); 00166 return atoi(second[itemNumber].c_str()); 00167 } 00168 00169 float readConfig::readFileValueF(int itemNumber) 00170 { 00171 ASSERT(itemNumber < SiZE); 00172 return atof(second[itemNumber].c_str()); 00173 } 00174 00175 string readConfig::readFileValueS(int itemNumber) 00176 { 00177 ASSERT(itemNumber < SiZE); 00178 return second[itemNumber]; 00179 } 00180 00181 const char* readConfig::readFileValueC(int itemNumber) 00182 { 00183 ASSERT(itemNumber < SiZE); 00184 return second[itemNumber].c_str(); 00185 } 00186 00187 string readConfig::readFileValueName(int itemNumber) 00188 { 00189 ASSERT(itemNumber < SiZE); 00190 return first[itemNumber]; 00191 } 00192 00193 const char* readConfig::readFileValueNameC(int itemNumber) 00194 { 00195 ASSERT(itemNumber < SiZE); 00196 return first[itemNumber].c_str(); 00197 } 00198 00199 float readConfig::readFileValueNameF(int itemNumber) 00200 { 00201 ASSERT(itemNumber < SiZE); 00202 return atof(first[itemNumber].c_str()); 00203 } 00204 00205 float readConfig::getItemValueF(string itemName) 00206 { 00207 int x = 0; 00208 for(int i = 0; i < SiZE; i++) 00209 { 00210 if(!first[i].compare(itemName)) 00211 { 00212 x = 1; 00213 return atof(second[i].c_str()); 00214 } 00215 } 00216 if(x != 1) 00217 LFATAL("Requested item \"%s\" not found in config file \"%s\"", 00218 itemName.c_str(),fileName.c_str()); 00219 return 0.0F; 00220 } 00221 00222 string readConfig::getItemValueS(string itemName) 00223 { 00224 int x = 0; 00225 for(int i = 0; i < SiZE; i++) 00226 { 00227 if(!first[i].compare(itemName)) 00228 { 00229 x = 1; 00230 return second[i]; 00231 } 00232 } 00233 if(x != 1) 00234 LFATAL("Requested item \"%s\" not found in config file \"%s\"", 00235 itemName.c_str(),fileName.c_str()); 00236 return("Fluffy Bunny"); 00237 } 00238 00239 bool readConfig::getItemValueB(string itemName) 00240 { 00241 int x = 0; 00242 for(int i = 0; i < SiZE; i++) 00243 { 00244 if(!first[i].compare(itemName)) 00245 { 00246 x = 1; 00247 if(((int)atof(second[i].c_str())) == 1) 00248 return true; 00249 else 00250 return false; 00251 } 00252 } 00253 if(x != 1) 00254 LFATAL("Requested item \"%s\" not found in config file \"%s\"", 00255 itemName.c_str(),fileName.c_str()); 00256 return false; 00257 } 00258 00259 const char* readConfig::getItemValueC(string itemName) 00260 { 00261 int x = 0; 00262 for(int i = 0; i < SiZE; i++) 00263 { 00264 if(!first[i].compare(itemName)) 00265 { 00266 x = 1; 00267 return second[i].c_str(); 00268 } 00269 } 00270 if(x != 1) 00271 LFATAL("Requested item \"%s\" not found in config file \"%s\"", 00272 itemName.c_str(),fileName.c_str()); 00273 return("Blue Smurf"); 00274 } 00275 00276 void readConfig::setItemValue(string itemName, float _set) 00277 { 00278 int x = 0; 00279 for(int i = 0; i < SiZE; i++) 00280 { 00281 if(!first[i].compare(itemName)) 00282 { 00283 x = 1; 00284 second[i] = sformat("%f",_set); 00285 } 00286 } 00287 if(x != 1) 00288 LFATAL("Requested item \"%s\" not found in config file",itemName.c_str()); 00289 } 00290 00291 void readConfig::setItemValue(string itemName, string _set) 00292 { 00293 int x = 0; 00294 for(int i = 0; i < SiZE; i++) 00295 { 00296 if(!first[i].compare(itemName)) 00297 { 00298 x = 1; 00299 second[i] = _set; 00300 } 00301 } 00302 if(x != 1) 00303 LFATAL("Requested item \"%s\" not found in config file",itemName.c_str()); 00304 } 00305 00306 void readConfig::setItemValue(string itemName, const char* _set) 00307 { 00308 int x = 0; 00309 for(int i = 0; i < SiZE; i++) 00310 { 00311 if(!first[i].compare(itemName)) 00312 { 00313 x = 1; 00314 second[i] = _set; 00315 } 00316 } 00317 if(x != 1) 00318 LFATAL("Requested item \"%s\" not found in config file",itemName.c_str()); 00319 } 00320 00321 void readConfig::setFileValue(int itemNumber, float _set) 00322 { 00323 ASSERT(itemNumber < SiZE); 00324 second[itemNumber] = sformat("%f",_set); 00325 } 00326 00327 void readConfig::setFileValue(int itemNumber, string _set) 00328 { 00329 ASSERT(itemNumber < SiZE); 00330 second[itemNumber] = _set; 00331 } 00332 00333 void readConfig::setFileValue(int itemNumber, const char* _set) 00334 { 00335 ASSERT(itemNumber < SiZE); 00336 second[itemNumber] = _set; 00337 } 00338 00339 int readConfig::addItemValue(string itemName, float _set) 00340 { 00341 return addItemValue(itemName,sformat("%f",_set)); 00342 } 00343 00344 int readConfig::addItemValue(string itemName, string _set) 00345 { 00346 if(SiZE >= (vectorSize - 1)) 00347 { 00348 LINFO("Resizing configfile to %d",vectorSize); 00349 vectorSize+=vectorValue; 00350 first.resize(vectorSize); 00351 second.resize(vectorSize); 00352 isItem.resize(vectorSize); 00353 } 00354 first[SiZE] = itemName; 00355 second[SiZE] = _set; 00356 isItem[SiZE] = true; 00357 SiZE++; 00358 return SiZE; 00359 } 00360 00361 int readConfig::addItemValue(string itemName, const char* _set) 00362 { 00363 string str = _set; 00364 return addItemValue(itemName,str); 00365 } 00366 00367 int readConfig::itemCount() 00368 { 00369 return SiZE; 00370 } 00371 00372 // ###################################################################### 00373 /* So things look consistent in everyone's emacs... */ 00374 /* Local Variables: */ 00375 /* indent-tabs-mode: nil */ 00376 /* End: */