Changeset 718 in Sophya for trunk/SophyaLib/NTools/dvlist.cc
- Timestamp:
- Feb 6, 2000, 4:05:44 PM (26 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/NTools/dvlist.cc
r515 r718 27 27 // en particulier destinés à être inclus dans d'autres objets 28 28 // PPersist plus complexes. La classe DVList gère des 29 // variables de type entier ("int_ 4"), réél double précision ("double")29 // variables de type entier ("int_8"), réél double précision ("r_8") 30 30 // et de type chaine de caracteres ("string, char*", maxi 30 caracteres ). 31 31 // Une classe intermédiaire (*MuTyV*) est utilisée pour représenter une … … 117 117 for(it = dvl.mvlist.begin(); it != dvl.mvlist.end(); it++) 118 118 { 119 switch ((*it).second. typ)119 switch ((*it).second.elval.typ) 120 120 { 121 121 case 'I' : 122 SetI((*it).first, (*it).second. mtv.iv);122 SetI((*it).first, (*it).second.elval.mtv.iv); 123 123 break; 124 124 case 'D' : 125 SetD((*it).first, (*it).second. mtv.dv);125 SetD((*it).first, (*it).second.elval.mtv.dv); 126 126 break; 127 127 case 'S' : 128 SetS((*it).first, (*it).second. mtv.strv);128 SetS((*it).first, (*it).second.elval.mtv.strv); 129 129 break; 130 130 default : … … 138 138 139 139 //++ 140 // int_ 4 GetI(string const& key, int_4def=-1)141 // double GetD(string const& key, doubledef=-9.e19)140 // int_8 GetI(string const& key, int_8 def=-1) 141 // r_8 GetD(string const& key, r_8 def=-9.e19) 142 142 // string GetS(string const& key, char* def="") 143 143 // Retourne la valeur de la variable de nom "key" et de type entier, réél, 144 144 // chaine de caracteres. 145 145 // Si la variable n'existe pas, la valeur par défaut "def" est renvoyée. 146 //-- 147 148 /* --Methode-- */ 149 int_4 DVList::GetI(string const& key, int_4 def) 146 // string GetComment(string const& key) 147 // Retourne le commentaire associé à la variable de nom "key". 148 //-- 149 150 /* --Methode-- */ 151 int_8 DVList::GetI(string const& key, int_8 def) 150 152 { 151 153 ValList::iterator it = mvlist.find(key); 152 154 if (it == mvlist.end()) return(def); 153 if ( (*it).second. typ != 'I') return(def);154 return((*it).second. mtv.iv);155 } 156 157 /* --Methode-- */ 158 double DVList::GetD(string const& key, doubledef)155 if ( (*it).second.elval.typ != 'I') return(def); 156 return((*it).second.elval.mtv.iv); 157 } 158 159 /* --Methode-- */ 160 r_8 DVList::GetD(string const& key, r_8 def) 159 161 { 160 162 ValList::iterator it = mvlist.find(key); 161 163 if (it == mvlist.end()) return(def); 162 if ( (*it).second. typ != 'D') return(def);163 return((*it).second. mtv.dv);164 if ( (*it).second.elval.typ != 'D') return(def); 165 return((*it).second.elval.mtv.dv); 164 166 } 165 167 … … 169 171 ValList::iterator it = mvlist.find(key); 170 172 if (it == mvlist.end()) return(def); 171 if ( (*it).second.typ != 'S') return(def); 172 return((*it).second.mtv.strv); 173 } 174 175 //++ 176 // void SetI(string const& key, int_4 val) 177 // void SetD(string const& key, double val) 173 if ( (*it).second.elval.typ != 'S') return(def); 174 return((*it).second.elval.mtv.strv); 175 } 176 177 /* --Methode-- */ 178 string DVList::GetComment(string const& key) 179 { 180 ValList::iterator it = mvlist.find(key); 181 if (it == mvlist.end()) return(""); 182 return((*it).second.elcomm); 183 } 184 185 //++ 186 // void SetI(string const& key, int_8 val) 187 // void SetD(string const& key, r_8 val) 178 188 // void SetS(string const& key, char* val) 179 189 // void SetS(string const& key, string val) 180 190 // Crée la variable de nom "key", de type entier, double, string et 181 191 // lui attribue la valeur "val". Si une variable du même nom existe, 182 // sa valeur et eventuellement son type sont modifiés. 183 //-- 184 185 /* --Methode-- */ 186 void DVList::SetI(string const& key, int_4 val) 187 { 188 Get(key) = (int_4)val; 189 } 190 191 /* --Methode-- */ 192 void DVList::SetD(string const& key, double val) 193 { 194 Get(key) = (double)val; 192 // sa valeur et eventuellement son type sont modifiés. Les noms de 193 // variables ne doivent pas contenir de caractères spéciaux, 194 // en particulier pas de CR/LF. 195 // void SetComment(string const& key, string const& comm) 196 // Modifie le commentaire associé à la variable de nom "key", si 197 // celle-ci existe. Le texte du commentaire ne doit pas contenir 198 // de caractères spéciaux, et en particulier pas de CR/LF. 199 //-- 200 201 /* --Methode-- */ 202 void DVList::SetI(string const& key, int_8 val) 203 { 204 Get(key) = (int_8)val; 205 } 206 207 /* --Methode-- */ 208 void DVList::SetD(string const& key, r_8 val) 209 { 210 Get(key) = (r_8)val; 195 211 } 196 212 … … 209 225 } 210 226 227 /* --Methode-- */ 228 void DVList::SetComment(string const& key, string const& comm) 229 { 230 ValList::iterator it = mvlist.find(key); 231 if (it == mvlist.end()) return; 232 (*it).second.elcomm = comm; 233 } 211 234 212 235 //++ … … 228 251 if ( (l < 1) || (key.find_first_of(" ") < l) ) return(ddvdum); 229 252 ValList::iterator it = mvlist.find(key); 230 if (it == mvlist.end()) mvlist[key] = (int_4) 0; // $CHECK$ EA. Ambigu si pas de cast... 253 dvlElement xxx = {(int_8)0 , ""}; 254 if (it == mvlist.end()) mvlist[key] = xxx; 231 255 it = mvlist.find(key); 232 256 if (it == mvlist.end()) return(ddvdum); 233 else return((*it).second );257 else return((*it).second.elval); 234 258 } 235 259 … … 256 280 { 257 281 os << "DVList::Print() - NVar= " << (int)mvlist.size() << "\n"; 258 os << "Comment: " << comment << "\n";259 char buff[ 128];282 if (comment.length() > 0) os << comment << endl; 283 char buff[256]; 260 284 ValList::const_iterator it; 261 285 for(it = mvlist.begin(); it != mvlist.end(); it++) { 262 switch ((*it).second. typ)286 switch ((*it).second.elval.typ) 263 287 { 264 288 case 'I' : 265 sprintf(buff, "%s = %d (int) \n", (*it).first.substr(0,64).c_str(), (*it).second.mtv.iv ); 289 sprintf(buff, "%s = %d (int) %s\n", (*it).first.substr(0,64).c_str(), 290 (*it).second.elval.mtv.iv, (*it).second.elcomm.substr(0,128).c_str()); 266 291 break; 267 292 case 'D' : 268 sprintf(buff, "%s = %.20g (double) \n", (*it).first.substr(0,64).c_str(), (*it).second.mtv.dv ); 293 sprintf(buff, "%s = %.20g (double) %s\n", (*it).first.substr(0,64).c_str(), 294 (*it).second.elval.mtv.dv, (*it).second.elcomm.substr(0,128).c_str()); 269 295 break; 270 296 case 'S' : 271 sprintf(buff, "%s = %s (string) \n", (*it).first.substr(0,64).c_str(), (*it).second.mtv.strv ); 297 sprintf(buff, "%s = %s (string) %s\n", (*it).first.substr(0,64).c_str(), 298 (*it).second.elval.mtv.strv, (*it).second.elcomm.substr(0,128).c_str()); 272 299 break; 273 300 default : … … 287 314 //| mvu = 66.6; // et double (= 66.6) maintenant ... 288 315 //| MuTyV mvi(14); // On construit une variable entiere = 14 289 //| floatx = mvi; // x vaut 14.0316 //| r_4 x = mvi; // x vaut 14.0 290 317 //| MuTyV mvd(44.4); // Variable double = 44.4 291 318 //| int k = mvd; // k vaut 44 … … 305 332 //| DVList dvlr("dvlist.ppf"); 306 333 //| int k = dvlr["toto"] ; // k = 14 307 //| doubleb = dvlr["titi"] ; // b = 25.5334 //| r_8 b = dvlr["titi"] ; // b = 25.5 308 335 //| string s = dvlr["tata"] ; // s = "Bonjour, Ca va ?" 309 //| floatc = dvlr["Hello"] ; // c = 77.77336 //| r_4 c = dvlr["Hello"] ; // c = 77.77 310 337 //| int l = dvlr["Hello"] ; // l = 77 311 338 //| int m = dvlr["hello"] ; // m = 88 … … 321 348 void ObjFileIO<DVList>::WriteSelf(POutPersist& s) const 322 349 { 323 char buf[ 320];350 char buf[512]; 324 351 325 352 int lc = dobj->Comment().length(); 326 if (lc > 319) lc = 319;353 if (lc > 511) lc = 511; 327 354 if (lc > 0) { 328 355 sprintf(buf,"Comment: ( %6d ) ", lc); … … 333 360 DVList::ValList::const_iterator it; 334 361 for(it = dobj->Begin(); it != dobj->End(); it++) { 335 switch ((*it).second.typ) 336 { 362 switch ((*it).second.elval.typ) { 337 363 case 'I' : 338 sprintf(buf,"I %s %d", (*it).first.substr(0,64).c_str(), (*it).second. mtv.iv );364 sprintf(buf,"I %s %d", (*it).first.substr(0,64).c_str(), (*it).second.elval.mtv.iv ); 339 365 s.PutLine(buf); 340 366 break; 341 367 case 'D' : 342 sprintf(buf,"D %s %.20g", (*it).first.substr(0,64).c_str(), (*it).second. mtv.dv );368 sprintf(buf,"D %s %.20g", (*it).first.substr(0,64).c_str(), (*it).second.elval.mtv.dv ); 343 369 s.PutLine(buf); 344 370 break; 345 371 case 'S' : 346 sprintf(buf,"S %s %s", (*it).first.substr(0,64).c_str(), (*it).second. mtv.strv );372 sprintf(buf,"S %s %s", (*it).first.substr(0,64).c_str(), (*it).second.elval.mtv.strv ); 347 373 s.PutLine(buf); 348 374 break; 349 375 default : 350 376 break; 351 } 352 } 377 } 378 // Ecriture eventuelle du commentaire associe 379 if ((*it).second.elcomm.length() > 0) { 380 sprintf(buf,"# %s", (*it).second.elcomm.substr(0,256).c_str()); 381 s.PutLine(buf); 382 } 383 } 353 384 354 385 s.PutLine("ZZZZZ--End-of-Varible-List------"); … … 358 389 void ObjFileIO<DVList>::ReadSelf(PInPersist& s) 359 390 { 360 char buf[ 320];361 int_ 4j,iv;362 doubledv;391 char buf[512]; 392 int_8 j,iv; 393 r_8 dv; 363 394 bool ok=true; 364 395 buf[0] = '\0'; 365 396 dobj->Clear(); 366 397 367 s.GetLine(buf, 319); // Pour lire les "------- "398 s.GetLine(buf, 511); // Pour lire les "------- " 368 399 if (buf[0] != '-') { // Il y a un champ commentaire a lire 369 400 buf[18] ='\0'; 370 401 int lc = atoi(buf+11); 371 if (lc > 319) {402 if (lc > 511) { 372 403 cerr << "DVList::ReadSelf() Pb/Bug ?? CommentLength= " << lc << endl; 373 lc = 319;404 lc = 511; 374 405 } 375 406 s.GetBytes(buf, lc); … … 378 409 } 379 410 411 string key=""; 380 412 while(ok) { 381 s.GetLine(buf, 319);382 buf[ 319] = '\0';413 s.GetLine(buf, 511); 414 buf[511] = '\0'; 383 415 if (strncmp(buf,"ZZZZZ",5) == 0) { ok=false; break; } 416 if (buf[0] == '#') { 417 dobj->SetComment(key, buf+2); 418 continue; 419 } 384 420 j = posc(buf+2, ' ')+2; 385 421 buf[j] = '\0'; 386 switch (buf[0]) 387 { 422 switch (buf[0]) { 388 423 case 'I' : 389 iv = (int_4)atol(buf+j+1); 390 dobj->SetI(buf+2, iv); 424 iv = (int_8)atol(buf+j+1); 425 key = buf+2; 426 dobj->SetI(key, iv); 391 427 break; 392 428 case 'D' : 393 429 dv = atof(buf+j+1); 394 dobj->SetD(buf+2, dv); 430 key = buf+2; 431 dobj->SetD(key, dv); 395 432 break; 396 433 case 'S' : 397 dobj->SetS(buf+2, buf+j+1); 434 key = buf+2; 435 dobj->SetS(key, buf+j+1); 398 436 break; 399 437 default :
Note:
See TracChangeset
for help on using the changeset viewer.