contrib/mul/mbl/mbl_load_text_file.txx
Go to the documentation of this file.
00001 #ifndef mbl_load_text_file_txx_
00002 #define mbl_load_text_file_txx_
00003 //:
00004 // \file
00005 // \brief Functions to load objects from text file
00006 // \author dac
00007 
00008 #include "mbl_load_text_file.h"
00009 #include <mbl/mbl_exception.h>
00010 #include <vcl_fstream.h>
00011 #include <vcl_iostream.h>
00012 #include <vcl_iterator.h>
00013 #include <vcl_algorithm.h>
00014 
00015 //: Load vector from file with format "v1 v2 .. vn"
00016 template <class T>
00017 bool mbl_load_text_file(vcl_vector<T>& v, const vcl_string& path)
00018 {
00019   v.resize(0);
00020 
00021   vcl_ifstream ifs(path.c_str());
00022   if (!ifs)
00023   {
00024     mbl_exception_throw_os_error( path, "mbl_load_text_file: failed to load" );
00025     return false;
00026   }
00027 
00028   vcl_copy(vcl_istream_iterator<T> (ifs), vcl_istream_iterator<T>(),
00029            vcl_back_insert_iterator< vcl_vector<T> > (v) );
00030   if (ifs.eof())
00031     return true;
00032 
00033   mbl_exception_warning( mbl_exception_parse_file_error(
00034     "mbl_load_text_file: parse failure before end of file", path ) );
00035   return true;
00036 }
00037 
00038 #undef MBL_LOAD_TEXT_FILE_INSTANTIATE
00039 #define MBL_LOAD_TEXT_FILE_INSTANTIATE(T ) \
00040 template bool mbl_load_text_file(vcl_vector<T >& v, const vcl_string& path)
00041 
00042 #endif //mbl_load_text_file_txx_