core/vsl/vsl_quick_file.h
Go to the documentation of this file.
00001 // This is core/vsl/vsl_quick_file.h
00002 #ifndef vsl_quick_file_h_
00003 #define vsl_quick_file_h_
00004 //:
00005 // \file
00006 // \brief Functions for quickly loading and saving binary files.
00007 // \author Ian Scott
00008 //
00009 // All the functions return true if successful.
00010 // The functions will also output a success or failure message to stderr by
00011 // default, although you may substitute any vcl_ostream, or \c (vcl_ostream*)0 to avoid the
00012 // message.
00013 //
00014 // For these templated functions to work, the object must have vsl_b_read and
00015 // vsl_b_write functions defined for them
00016 
00017 #include <vcl_string.h>
00018 #include <vcl_iostream.h>
00019 #include <vcl_cerrno.h>
00020 #include <vsl/vsl_binary_io.h>
00021 
00022 
00023 //: Load something from a file
00024 template <class T>
00025 inline bool vsl_quick_file_load(T &data,
00026                                 const vcl_string& path,
00027                                 vcl_ostream* errorStream = &vcl_cerr)
00028 {
00029   vsl_b_ifstream bfs(path);
00030   if ( !(!bfs))
00031   {
00032     vsl_b_read(bfs,data);
00033     if (!(!bfs))
00034     {
00035       // Check that we have reached the end of the file.
00036       char dummy;
00037       vsl_b_read(bfs,dummy);
00038       if (bfs.is().eof())
00039       {
00040         bfs.close();
00041         if (errorStream)
00042           *errorStream << "Successfully loaded: " << path << '\n';
00043         return true;
00044       }
00045     }
00046   }
00047   bfs.close();
00048   if (errorStream)
00049     *errorStream << "Unable to load: "<< path <<'\n';
00050   return false;
00051 }
00052 
00053 
00054 //: Save something to a file
00055 template <class T>
00056 inline bool vsl_quick_file_save(const T &data,
00057                                 const vcl_string& path,
00058                                 vcl_ostream* errorStream = &vcl_cerr)
00059 {
00060   vsl_b_ofstream bfs(path);
00061   if (!(!bfs))
00062   {
00063     vsl_b_write(bfs,data);
00064     if (!(!bfs))
00065     {
00066       bfs.close();
00067       if (errorStream)
00068         *errorStream << "Successfully saved: "<< path <<'\n';
00069       return true;
00070     }
00071   }
00072   bfs.close();
00073   if (errorStream)
00074     *errorStream << "Unable to save: " << path << '\n';
00075   return false;
00076 }
00077 
00078 // Load two objects from a file
00079 template <class T, class S>
00080 inline bool vsl_quick_file_load(T &data1,
00081                                 S &data2,
00082                                 const vcl_string& path,
00083                                 vcl_ostream* errorStream = &vcl_cerr)
00084 {
00085   vsl_b_ifstream bfs(path);
00086   int reason = errno;
00087   if ( !(!bfs))
00088   {
00089     vsl_b_read(bfs,data1);
00090     vsl_b_read(bfs,data2);
00091     if (!(!bfs))
00092     {
00093       // Check that we have reached the end of the file.
00094       char dummy;
00095       vsl_b_read(bfs,dummy);
00096       if (bfs.is().eof())
00097       {
00098         bfs.close();
00099         if (errorStream)
00100           *errorStream << "Successfully loaded: " << path << '\n';
00101         return true;
00102       }
00103     }
00104   }
00105   reason = errno;
00106   bfs.close();
00107   if (errorStream)
00108     *errorStream << "Unable to load: "<< path <<'\n';
00109   return false;
00110 }
00111 
00112 // Save two objects to a file
00113 template <class T, class S>
00114 inline bool vsl_quick_file_save(const T &data1,
00115                                 const S &data2,
00116                                 const vcl_string& path,
00117                                 vcl_ostream* errorStream = &vcl_cerr)
00118 {
00119   vsl_b_ofstream bfs(path);
00120   if (!(!bfs))
00121   {
00122     vsl_b_write(bfs,data1);
00123     vsl_b_write(bfs,data2);
00124     if (!(!bfs))
00125     {
00126       bfs.close();
00127       if (errorStream)
00128         *errorStream << "Successfully saved: "<< path <<'\n';
00129       return true;
00130     }
00131   }
00132   bfs.close();
00133   if (errorStream)
00134     *errorStream << "Unable to save: " << path << '\n';
00135   return false;
00136 }
00137 
00138 // Load three objects from a file
00139 template <class T, class S, class U>
00140 inline bool vsl_quick_file_load(T &data1,
00141                                 S &data2, U &data3,
00142                                 const vcl_string& path,
00143                                 vcl_ostream* errorStream = &vcl_cerr)
00144 {
00145   vsl_b_ifstream bfs(path);
00146   if ( !(!bfs))
00147   {
00148     vsl_b_read(bfs,data1);
00149     vsl_b_read(bfs,data2);
00150     vsl_b_read(bfs,data3);
00151     if (!(!bfs))
00152     {
00153       // Check that we have reached the end of the file.
00154       char dummy;
00155       vsl_b_read(bfs,dummy);
00156       if (bfs.is().eof())
00157       {
00158         bfs.close();
00159         if (errorStream)
00160           *errorStream << "Successfully loaded: " << path << '\n';
00161         return true;
00162       }
00163     }
00164   }
00165   bfs.close();
00166   if (errorStream)
00167     *errorStream << "Unable to load: "<< path <<'\n';
00168   return false;
00169 }
00170 
00171 // Save three objects to a file
00172 template <class T, class S, class U>
00173 inline bool vsl_quick_file_save(const T &data1,
00174                                 const S &data2, const U &data3,
00175                                 const vcl_string& path,
00176                                 vcl_ostream* errorStream = &vcl_cerr)
00177 {
00178   vsl_b_ofstream bfs(path);
00179   if (!(!bfs))
00180   {
00181     vsl_b_write(bfs,data1);
00182     vsl_b_write(bfs,data2);
00183     vsl_b_write(bfs,data3);
00184     if (!(!bfs))
00185     {
00186       bfs.close();
00187       if (errorStream)
00188         *errorStream << "Successfully saved: "<< path <<'\n';
00189       return true;
00190     }
00191   }
00192   bfs.close();
00193   if (errorStream)
00194     *errorStream << "Unable to save: " << path << '\n';
00195   return false;
00196 }
00197 
00198 // Load four objects from a file
00199 template <class T, class S, class U, class V>
00200 inline bool vsl_quick_file_load(T &data1,
00201                                 S &data2, U &data3, V &data4,
00202                                 const vcl_string& path,
00203                                 vcl_ostream* errorStream = &vcl_cerr)
00204 {
00205   vsl_b_ifstream bfs(path);
00206   if ( !(!bfs))
00207   {
00208     vsl_b_read(bfs,data1);
00209     vsl_b_read(bfs,data2);
00210     vsl_b_read(bfs,data3);
00211     vsl_b_read(bfs,data4);
00212     if (!(!bfs))
00213     {
00214       // Check that we have reached the end of the file.
00215       char dummy;
00216       vsl_b_read(bfs,dummy);
00217       if (bfs.is().eof())
00218       {
00219         bfs.close();
00220         if (errorStream)
00221           *errorStream << "Successfully loaded: " << path << '\n';
00222         return true;
00223       }
00224     }
00225   }
00226   bfs.close();
00227   if (errorStream)
00228     *errorStream << "Unable to load: "<< path <<'\n';
00229   return false;
00230 }
00231 
00232 // Save four objects to a file
00233 template <class T, class S, class U, class V>
00234 inline bool vsl_quick_file_save(const T &data1, const S &data2,
00235                                 const U &data3, const V &data4,
00236                                 const vcl_string& path,
00237                                 vcl_ostream* errorStream = &vcl_cerr)
00238 {
00239   vsl_b_ofstream bfs(path);
00240   if (!(!bfs))
00241   {
00242     vsl_b_write(bfs,data1);
00243     vsl_b_write(bfs,data2);
00244     vsl_b_write(bfs,data3);
00245     vsl_b_write(bfs,data4);
00246     if (!(!bfs))
00247     {
00248       bfs.close();
00249       if (errorStream)
00250         *errorStream << "Successfully saved: "<< path <<'\n';
00251       return true;
00252     }
00253   }
00254   bfs.close();
00255   if (errorStream)
00256     *errorStream << "Unable to save: " << path << '\n';
00257   return false;
00258 }
00259 
00260 // Load five objects from a file
00261 template <class T, class S, class U, class V, class W>
00262 inline bool vsl_quick_file_load(T &data1,
00263                                 S &data2, U &data3, V &data4, W &data5,
00264                                 const vcl_string& path,
00265                                 vcl_ostream* errorStream = &vcl_cerr)
00266 {
00267   vsl_b_ifstream bfs(path);
00268   if ( !(!bfs))
00269   {
00270     vsl_b_read(bfs,data1);
00271     vsl_b_read(bfs,data2);
00272     vsl_b_read(bfs,data3);
00273     vsl_b_read(bfs,data4);
00274     vsl_b_read(bfs,data5);
00275     if (!(!bfs))
00276     {
00277       // Check that we have reached the end of the file.
00278       char dummy;
00279       vsl_b_read(bfs,dummy);
00280       if (bfs.is().eof())
00281       {
00282         bfs.close();
00283         if (errorStream)
00284           *errorStream << "Successfully loaded: " << path << '\n';
00285         return true;
00286       }
00287     }
00288   }
00289   bfs.close();
00290   if (errorStream)
00291     *errorStream << "Unable to load: "<< path <<'\n';
00292   return false;
00293 }
00294 
00295 // Save five objects to a file
00296 template <class T, class S, class U, class V, class W>
00297 inline bool vsl_quick_file_save(const T &data1, const S &data2, const U &data3,
00298                                 const V &data4, const W &data5,
00299                                 const vcl_string& path,
00300                                 vcl_ostream* errorStream = &vcl_cerr)
00301 {
00302   vsl_b_ofstream bfs(path);
00303   if (!(!bfs))
00304   {
00305     vsl_b_write(bfs,data1);
00306     vsl_b_write(bfs,data2);
00307     vsl_b_write(bfs,data3);
00308     vsl_b_write(bfs,data4);
00309     vsl_b_write(bfs,data5);
00310     if (!(!bfs))
00311     {
00312       bfs.close();
00313       if (errorStream)
00314         *errorStream << "Successfully saved: "<< path <<'\n';
00315       return true;
00316     }
00317   }
00318   bfs.close();
00319   if (errorStream)
00320     *errorStream << "Unable to save: " << path << '\n';
00321   return false;
00322 }
00323 
00324 
00325 // Load six objects from a file
00326 template <class T, class S, class U, class V, class W, class X>
00327 inline bool vsl_quick_file_load(T &data1, S &data2, U &data3,
00328                                 V &data4, W &data5, X &data6,
00329                                 const vcl_string& path,
00330                                 vcl_ostream* errorStream = &vcl_cerr)
00331 {
00332   vsl_b_ifstream bfs(path);
00333   if ( !(!bfs))
00334   {
00335     vsl_b_read(bfs,data1);
00336     vsl_b_read(bfs,data2);
00337     vsl_b_read(bfs,data3);
00338     vsl_b_read(bfs,data4);
00339     vsl_b_read(bfs,data5);
00340     vsl_b_read(bfs,data6);
00341     if (!(!bfs))
00342     {
00343       // Check that we have reached the end of the file.
00344       char dummy;
00345       vsl_b_read(bfs,dummy);
00346       if (bfs.is().eof())
00347       {
00348         bfs.close();
00349         if (errorStream)
00350           *errorStream << "Successfully loaded: " << path << '\n';
00351         return true;
00352       }
00353     }
00354   }
00355   bfs.close();
00356   if (errorStream)
00357     *errorStream << "Unable to load: "<< path <<'\n';
00358   return false;
00359 }
00360 
00361 // Save six objects to a file
00362 template <class T, class S, class U, class V, class W, class X>
00363 inline bool vsl_quick_file_save(const T &data1, const S &data2, const U &data3,
00364                                 const V &data4, const W &data5, const X &data6,
00365                                 const vcl_string& path,
00366                                 vcl_ostream* errorStream = &vcl_cerr)
00367 {
00368   vsl_b_ofstream bfs(path);
00369   if (!(!bfs))
00370   {
00371     vsl_b_write(bfs,data1);
00372     vsl_b_write(bfs,data2);
00373     vsl_b_write(bfs,data3);
00374     vsl_b_write(bfs,data4);
00375     vsl_b_write(bfs,data5);
00376     vsl_b_write(bfs,data6);
00377     if (!(!bfs))
00378     {
00379       bfs.close();
00380       if (errorStream)
00381         *errorStream << "Successfully saved: "<< path <<'\n';
00382       return true;
00383     }
00384   }
00385   bfs.close();
00386   if (errorStream)
00387     *errorStream << "Unable to save: " << path << '\n';
00388   return false;
00389 }
00390 #endif // vsl_quick_file_h_