00001 /* 00002 ------------------------------------------------------------------- 00003 00004 Copyright (C) 2006, 2007, 2008, 2009, Andrew W. Steiner 00005 00006 This file is part of O2scl. 00007 00008 O2scl is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 O2scl is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with O2scl. If not, see <http://www.gnu.org/licenses/>. 00020 00021 ------------------------------------------------------------------- 00022 */ 00023 #ifndef O2SCL_USER_IO_H 00024 #define O2SCL_USER_IO_H 00025 00026 #include <o2scl/collection.h> 00027 00028 /** \file user_io.h 00029 \brief Support functions for I/O 00030 00031 \todo Do input for array and 2d array objects, finish output 00032 functions. Make a macro for I/O for doubles, ints and other 00033 objects w/o a type() function. 00034 */ 00035 00036 #ifndef DOXYGENP 00037 namespace o2scl { 00038 #endif 00039 00040 /** 00041 \brief Input one object from a file 00042 00043 This requires a default copy constructor and a type() function. 00044 00045 This does not disturb any objects in the collection. The 00046 pointer specified does not need to be in the collection and is 00047 not added to the collection. 00048 */ 00049 template<class obj_t> 00050 int o2scl_input(in_file_format *ins, obj_t *&obj, 00051 std::string &name, size_t &sz, size_t &sz2, 00052 bool verbose=false) { 00053 00054 obj_t odum; 00055 std::string stype=odum.type(); 00056 00057 // Read the file 00058 collection co; 00059 int ret=co.fin(ins); 00060 if (ret!=0) { 00061 O2SCL_ERR_RET("Read of file failed in o2scl_input().",gsl_efailed); 00062 } 00063 00064 // Look for the object in the file 00065 collection::type_iterator it=co.begin(stype); 00066 if (it==co.end(stype)) { 00067 O2SCL_ERR_RET((((std::string)"Couldn't find object of type '")+stype+ 00068 "' in collection in function o2scl_input(in_"+ 00069 "file_format,obj_t,string,size_t,size_t,bool).").c_str(), 00070 gsl_efailed); 00071 } 00072 // Double check that the pointer is nonzero 00073 if (it->data==0) { 00074 O2SCL_ERR_RET("Null pointer in function o2scl_input().", 00075 gsl_efailed); 00076 } 00077 // Check that sizes are correct 00078 if (it->size==0 || it->size2==0) { 00079 O2SCL_ERR_RET("Size of object incorrect in o2scl_input().", 00080 gsl_efailed); 00081 } 00082 00083 // Assign name, pointer, and size 00084 name=it.name(); 00085 sz=it->size; 00086 sz2=it->size2; 00087 obj=(obj_t *)it->data; 00088 00089 // Give ownership to the user 00090 co.disown(name); 00091 00092 // Added on 4/20/06...good idea? (6/6/08 - I think this is good, 00093 // but this should all be done in the collection destructor anyway) 00094 co.clear(); 00095 00096 return ret; 00097 } 00098 00099 /** 00100 \brief Input one object from a file 00101 00102 This requires a default copy constructor and a type() function. 00103 00104 This does not disturb any objects in the collection. The 00105 pointer specified does not need to be in the collection and is 00106 not added to the collection. 00107 */ 00108 template<class obj_t> 00109 int o2scl_input(in_file_format *ins, obj_t *&obj, 00110 std::string &name, size_t &sz, bool verbose=false) { 00111 00112 obj_t odum; 00113 std::string stype=odum.type(); 00114 00115 // Read the file 00116 collection co; 00117 int ret=co.fin(ins); 00118 if (ret!=0) { 00119 O2SCL_ERR_RET("Read of file failed in o2scl_input().",gsl_efailed); 00120 } 00121 00122 // Look for the object in the file 00123 collection::type_iterator it=co.begin(stype); 00124 if (it==co.end(stype)) { 00125 O2SCL_ERR_RET((((std::string)"Couldn't find object of type '")+stype+ 00126 "' in collection in function o2scl_input().").c_str(), 00127 gsl_efailed); 00128 } 00129 // Double check that the pointer is nonzero 00130 if (it->data==0) { 00131 O2SCL_ERR_RET("Null pointer in function o2scl_input().", 00132 gsl_efailed); 00133 } 00134 // Check that sizes are correct 00135 if (it->size==0 || it->size2>0) { 00136 O2SCL_ERR_RET("Size of object incorrect in o2scl_input().", 00137 gsl_efailed); 00138 } 00139 00140 // Assign name, pointer, and size 00141 name=it.name(); 00142 sz=it->size; 00143 obj=(obj_t *)it->data; 00144 00145 // Give ownership to the user 00146 co.disown(name); 00147 00148 // Added on 4/20/06...good idea? (6/6/08 - I think this is good, 00149 // but this should all be done in the collection destructor anyway) 00150 co.clear(); 00151 00152 return ret; 00153 } 00154 00155 /** 00156 \brief Input one object from a file 00157 00158 This requires a default copy constructor and a type() function. 00159 00160 This does not disturb any objects in the collection. The 00161 pointer specified does not need to be in the collection and is 00162 not added to the collection. 00163 */ 00164 template<class obj_t> 00165 int o2scl_input(in_file_format *ins, obj_t *&obj, 00166 std::string &name, bool verbose=false) { 00167 00168 obj_t odum; 00169 std::string stype=odum.type(); 00170 00171 // Read the file 00172 collection co; 00173 int ret=co.fin(ins); 00174 if (ret!=0) { 00175 O2SCL_ERR_RET("Read of file failed in o2scl_input().",gsl_efailed); 00176 } 00177 00178 // Look for the object in the file 00179 collection::type_iterator it=co.begin(stype); 00180 if (it==co.end(stype)) { 00181 O2SCL_ERR_RET((((std::string)"Couldn't find object of type '")+stype+ 00182 "' in collection in function o2scl_input("+ 00183 "in_file_format,obj_t,string,bool).").c_str(), 00184 gsl_efailed); 00185 } 00186 // Double check that the pointer is nonzero 00187 if (it->data==0) { 00188 O2SCL_ERR_RET("Null pointer in function o2scl_input().", 00189 gsl_efailed); 00190 } 00191 // Check that sizes are correct 00192 if (it->size>0 || it->size2>0) { 00193 O2SCL_ERR_RET("Size of object larger than expected in o2scl_input().", 00194 gsl_efailed); 00195 } 00196 00197 // Assign name and pointer 00198 name=it.name(); 00199 obj=(obj_t *)it->data; 00200 00201 // Give ownership to the user 00202 co.disown(name); 00203 00204 // Added on 4/20/06...good idea? (6/6/08 - I think this is good, 00205 // but this should all be done in the collection destructor anyway) 00206 co.clear(); 00207 00208 return ret; 00209 } 00210 00211 /** 00212 \brief Input one object from a file 00213 00214 This does not disturb any objects in the collection. The 00215 pointer specified does not need to be in the collection and is 00216 not added to the collection. 00217 */ 00218 template<class obj_t> 00219 int o2scl_input_text(std::string fname, obj_t *&obj, 00220 std::string &name, size_t &sz, size_t &sz2, 00221 bool verbose=false) { 00222 text_in_file *tif=new text_in_file(fname); 00223 int ret=o2scl_input(tif,obj,name,sz,sz2,verbose); 00224 delete tif; 00225 return ret; 00226 } 00227 00228 /** 00229 \brief Input one object from a file 00230 00231 This does not disturb any objects in the collection. The 00232 pointer specified does not need to be in the collection and is 00233 not added to the collection. 00234 */ 00235 template<class obj_t> 00236 int o2scl_input_text(std::string fname, obj_t *&obj, 00237 std::string &name, size_t &sz, bool verbose=false) { 00238 text_in_file *tif=new text_in_file(fname); 00239 int ret=o2scl_input(tif,obj,name,sz,verbose); 00240 delete tif; 00241 return ret; 00242 } 00243 00244 /** 00245 \brief Input one object from a file 00246 00247 This does not disturb any objects in the collection. The 00248 pointer specified does not need to be in the collection and is 00249 not added to the collection. 00250 */ 00251 template<class obj_t> 00252 int o2scl_input_text(std::string fname, obj_t *&obj, 00253 std::string &name, bool verbose=false) { 00254 text_in_file *tif=new text_in_file(fname); 00255 int ret=o2scl_input(tif,obj,name,verbose); 00256 delete tif; 00257 return ret; 00258 } 00259 00260 /** 00261 \brief Input one object from a file 00262 00263 This requires a default copy constructor and a type() function. 00264 00265 This does not disturb any objects in the collection. The 00266 pointer specified does not need to be in the collection and is 00267 not added to the collection. 00268 */ 00269 template<class obj_t> 00270 int o2scl_input_name(in_file_format *ins, obj_t *&obj, 00271 std::string name, bool verbose=false) { 00272 00273 obj_t odum; 00274 std::string stype=odum.type(); 00275 00276 // Read the file 00277 collection co; 00278 int ret=co.fin(ins); 00279 if (ret!=0) { 00280 O2SCL_ERR_RET("Read failed in o2scl_input_name().",gsl_efailed); 00281 } 00282 00283 // Look for the object in the file 00284 collection::type_iterator it=co.begin(stype); 00285 while(name!=it.name() && it!=co.end(stype)) it++; 00286 if (it==co.end(stype)) { 00287 O2SCL_ERR_RET("Couldn't find that type in function o2scl_input_name().", 00288 gsl_efailed); 00289 } 00290 if (it->data==0) { 00291 O2SCL_ERR_RET("Null pointer in function o2scl_input_name().", 00292 gsl_esanity); 00293 } 00294 00295 // Assign name and pointer 00296 obj=(obj_t *)it->data; 00297 00298 // Check that sizes are correct 00299 ret=0; 00300 if (it->size>0 || it->size2>0) { 00301 O2SCL_ERR("Size of object larger than expected in o2scl_input_name().", 00302 gsl_efailed); 00303 ret=gsl_efailed; 00304 } 00305 00306 // Give ownership to the user 00307 co.disown(name); 00308 00309 // Added on 4/20/06...good idea? (6/6/08 - I think this is good, 00310 // but this should all be done in the collection destructor anyway) 00311 co.clear(); 00312 00313 return ret; 00314 } 00315 00316 /** 00317 \brief Input one object from a file 00318 00319 This does not disturb any objects in the collection. The 00320 pointer specified does not need to be in the collection and is 00321 not added to the collection. 00322 */ 00323 template<class obj_t> 00324 int o2scl_input_name_text(std::string fname, obj_t *&obj, 00325 std::string &name, bool verbose=false) { 00326 text_in_file *tif=new text_in_file(fname); 00327 int ret=o2scl_input_name(tif,obj,name,verbose); 00328 delete tif; 00329 return ret; 00330 } 00331 00332 /** 00333 \brief Input one object from a file 00334 00335 This requires a default copy constructor and a type() function. 00336 00337 This does not disturb any objects in the collection. The 00338 pointer specified does not need to be in the collection and is 00339 not added to the collection. 00340 */ 00341 template<class obj_t> 00342 int o2scl_input(in_file_format *ins, obj_t *&obj, 00343 bool verbose=false) { 00344 00345 obj_t odum; 00346 std::string stype=odum.type(); 00347 00348 // Read the file 00349 collection co; 00350 int ret=co.fin(ins); 00351 if (ret!=0) { 00352 O2SCL_ERR_RET("Read failed in o2scl_input().",gsl_efailed); 00353 } 00354 00355 // Look for the object in the file 00356 collection::type_iterator it=co.begin(stype); 00357 if (it==co.end(stype)) { 00358 O2SCL_ERR_RET("Couldn't find that type in function o2scl_input().", 00359 gsl_efailed); 00360 } 00361 if (it->data==0) { 00362 O2SCL_ERR_RET("Null pointer in function o2scl_input().", 00363 gsl_esanity); 00364 } 00365 00366 // Assign name and pointer 00367 std::string name=it.name(); 00368 obj=(obj_t *)it->data; 00369 00370 // Check that sizes are correct 00371 ret=0; 00372 if (it->size>0 || it->size2>0) { 00373 O2SCL_ERR("Size of object larger than expected in o2scl_input().", 00374 gsl_efailed); 00375 ret=gsl_efailed; 00376 } 00377 00378 // Give ownership to the user 00379 co.disown(name); 00380 00381 // Added on 4/20/06...good idea? (6/6/08 - I think this is good, 00382 // but this should all be done in the collection destructor anyway) 00383 co.clear(); 00384 00385 return ret; 00386 } 00387 00388 /** 00389 \brief Input one object from a file 00390 00391 This does not disturb any objects in the collection. The 00392 pointer specified does not need to be in the collection and is 00393 not added to the collection. 00394 */ 00395 template<class obj_t> 00396 int o2scl_input_text(std::string fname, obj_t *&obj, 00397 bool verbose=false) { 00398 text_in_file *tif=new text_in_file(fname); 00399 int ret=o2scl_input(tif,obj,verbose); 00400 delete tif; 00401 return ret; 00402 } 00403 00404 /** 00405 \brief Input one object from a file 00406 00407 This requires a default copy constructor and a type() function. 00408 00409 This does not disturb any objects in the collection. The 00410 pointer specified does not need to be in the collection and is 00411 not added to the collection. 00412 00413 \todo Do input for array and 2d array objects 00414 */ 00415 template<class obj_t> 00416 int o2scl_output(out_file_format *outs, obj_t *obj, 00417 std::string name, size_t &sz, size_t &sz2, 00418 bool verbose=false) { 00419 00420 obj_t odum; 00421 std::string stype=odum.type(); 00422 00423 collection co; 00424 void *vp=obj; 00425 00426 return co.out_one(outs,stype,name,vp,sz,sz2); 00427 } 00428 00429 /** 00430 \brief Input one object from a file 00431 00432 This requires a default copy constructor and a type() function. 00433 00434 This does not disturb any objects in the collection. The 00435 pointer specified does not need to be in the collection and is 00436 not added to the collection. 00437 00438 \todo Do input for array and 2d array objects 00439 */ 00440 template<class obj_t> 00441 int o2scl_output(out_file_format *outs, obj_t *obj, 00442 std::string name, size_t &sz, bool verbose=false) { 00443 00444 obj_t odum; 00445 std::string stype=odum.type(); 00446 00447 collection co; 00448 void *vp=obj; 00449 int isz; 00450 int ret=co.out_one(outs,stype,name,vp,isz); 00451 sz=(size_t)isz; 00452 return ret; 00453 } 00454 00455 /** 00456 \brief Input one object from a file 00457 00458 This requires a default copy constructor and a type() function. 00459 00460 This does not disturb any objects in the collection. The 00461 pointer specified does not need to be in the collection and is 00462 not added to the collection. 00463 00464 \todo Do input for array and 2d array objects 00465 */ 00466 template<class obj_t> 00467 int o2scl_output(out_file_format *outs, obj_t *obj, 00468 std::string name, bool verbose=false) { 00469 00470 obj_t odum; 00471 std::string stype=odum.type(); 00472 00473 collection co; 00474 void *vp=obj; 00475 return co.out_one(outs,stype,name,vp); 00476 } 00477 00478 /** 00479 \brief Input one object from a file 00480 00481 This does not disturb any objects in the collection. The 00482 pointer specified does not need to be in the collection and is 00483 not added to the collection. 00484 */ 00485 template<class obj_t> 00486 int o2scl_output_text(std::string fname, obj_t *obj, 00487 std::string name, size_t sz, 00488 size_t sz2, bool verbose=false) { 00489 text_out_file *tof=new text_out_file(fname); 00490 int ret=o2scl_output(tof,obj,name,sz,sz2,verbose); 00491 delete tof; 00492 return ret; 00493 } 00494 00495 /** 00496 \brief Input one object from a file 00497 00498 This does not disturb any objects in the collection. The 00499 pointer specified does not need to be in the collection and is 00500 not added to the collection. 00501 */ 00502 template<class obj_t> 00503 int o2scl_output_text(std::string fname, obj_t *obj, 00504 std::string name, size_t sz, 00505 bool verbose=false) { 00506 text_out_file *tof=new text_out_file(fname); 00507 int ret=o2scl_output(tof,obj,name,sz,verbose); 00508 delete tof; 00509 return ret; 00510 } 00511 00512 /** 00513 \brief Input one object from a file 00514 00515 This does not disturb any objects in the collection. The 00516 pointer specified does not need to be in the collection and is 00517 not added to the collection. 00518 */ 00519 template<class obj_t> 00520 int o2scl_output_text(std::string fname, obj_t *obj, 00521 std::string name, bool verbose=false) { 00522 text_out_file *tof=new text_out_file(fname); 00523 int ret=o2scl_output(tof,obj,name,verbose); 00524 delete tof; 00525 return ret; 00526 } 00527 00528 00529 #ifndef DOXYGENP 00530 } 00531 #endif 00532 00533 #endif
Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.
Project hosting provided by
,
O2scl Sourceforge Project Page