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