contrib/mul/mbl/mbl_save_text_file.txx
Go to the documentation of this file.
00001 #ifndef mbl_save_text_file_txx_
00002 #define mbl_save_text_file_txx_
00003 //:
00004 // \file
00005 // \brief Functions to save objects to text file
00006 // \author dac
00007 
00008 #include "mbl_save_text_file.h"
00009 
00010 #include <mbl/mbl_exception.h>
00011 #include <vcl_fstream.h>
00012 
00013 //: Save vector to file with format  { n v1 v2 .. vn }
00014 template <class T>
00015 bool mbl_save_text_file(const vcl_vector<T >& v, const vcl_string& path)
00016 {
00017   vcl_ofstream ofs(path.c_str());
00018   if (!ofs)
00019   {
00020     mbl_exception_throw_os_error( path, "mbl_save_text_file: failed to save" );
00021     return false;
00022   }
00023 
00024   //ofs<<v.size()<<'\n';
00025   for (unsigned i=0;i<v.size();++i)
00026   {
00027     ofs<<v[i]<<'\n';
00028   }
00029   ofs.close();
00030   return true;
00031 }
00032 
00033 //: Save vector to stream with format  { n v1 v2 .. vn }
00034 template <class T>
00035 bool mbl_save_text_file(const vcl_vector<T >& v, vcl_ostream &os, const vcl_string& delim)
00036 {
00037   for (typename vcl_vector<T >::const_iterator it=v.begin(), end=v.end(); it!=end; ++it)
00038     os << *it << delim;
00039 
00040   return true;
00041 }
00042 
00043 
00044 #undef MBL_SAVE_TEXT_FILE_INSTANTIATE_PATH
00045 #define MBL_SAVE_TEXT_FILE_INSTANTIATE_PATH(T ) \
00046 template bool mbl_save_text_file(const vcl_vector<T >& v, const vcl_string& path)
00047 #undef MBL_SAVE_TEXT_FILE_INSTANTIATE_STREAM
00048 #define MBL_SAVE_TEXT_FILE_INSTANTIATE_STREAM(T ) \
00049 template bool mbl_save_text_file(const vcl_vector<T >& v, vcl_ostream &os, const vcl_string &delim)
00050 
00051 #endif // mbl_save_text_file_txx_