BESContainerStorageFile.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <cerrno>
00034 #include <sstream>
00035 #include <fstream>
00036 #include <iostream>
00037 #include <cstring>
00038
00039 using std::stringstream ;
00040 using std::ifstream ;
00041
00042 #include "BESContainerStorageFile.h"
00043 #include "BESFileContainer.h"
00044 #include "TheBESKeys.h"
00045 #include "BESInternalError.h"
00046 #include "BESInfo.h"
00047
00077 BESContainerStorageFile::BESContainerStorageFile( const string &n )
00078 : BESContainerStorage( n )
00079 {
00080
00081
00082
00083
00084 string key = "BES.Container.Persistence.File." + n ;
00085 bool found = false ;
00086 _file = TheBESKeys::TheKeys()->get_key( key, found ) ;
00087 if( _file == "" )
00088 {
00089 string s = key + " not defined in key file" ;
00090 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00091 }
00092
00093 ifstream persistence_file( _file.c_str() ) ;
00094 int myerrno = errno ;
00095 if( !persistence_file )
00096 {
00097 char *err = strerror( myerrno ) ;
00098 string s = "Unable to open persistence file " + _file + ": " ;
00099 if( err )
00100 s += err ;
00101 else
00102 s += "Unknown error" ;
00103
00104 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00105 }
00106
00107 char cline[80] ;
00108
00109 while( !persistence_file.eof() )
00110 {
00111 stringstream strm ;
00112 persistence_file.getline( cline, 80 ) ;
00113 if( !persistence_file.eof() )
00114 {
00115 strm << cline ;
00116 BESContainerStorageFile::container *c =
00117 new BESContainerStorageFile::container ;
00118 strm >> c->_symbolic_name ;
00119 strm >> c->_real_name ;
00120 strm >> c->_container_type ;
00121 string dummy ;
00122 strm >> dummy ;
00123 if( c->_symbolic_name == "" ||
00124 c->_real_name == "" ||
00125 c->_container_type == "" )
00126 {
00127 delete c ;
00128 persistence_file.close() ;
00129 string s = "Incomplete container persistence line in file "
00130 + _file ;
00131 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00132 }
00133 if( dummy != "" )
00134 {
00135 persistence_file.close() ;
00136 delete c ;
00137 string s = "Too many fields in persistence file "
00138 + _file ;
00139 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00140 }
00141 _container_list[c->_symbolic_name] = c ;
00142 }
00143 }
00144 persistence_file.close() ;
00145 }
00146
00147 BESContainerStorageFile::~BESContainerStorageFile()
00148 {
00149 BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00150 BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00151 for( ; i != ie; i++ )
00152 {
00153 BESContainerStorageFile::container *c = (*i).second ;
00154 delete c ;
00155 }
00156 }
00157
00169 BESContainer *
00170 BESContainerStorageFile::look_for( const string &sym_name )
00171 {
00172 BESFileContainer *ret_container = 0 ;
00173 BESContainerStorageFile::Container_citer i ;
00174 i = _container_list.find( sym_name ) ;
00175 if( i != _container_list.end() )
00176 {
00177 BESContainerStorageFile::container *c = (*i).second;
00178 ret_container = new BESFileContainer( c->_symbolic_name,
00179 c->_real_name,
00180 c->_container_type ) ;
00181 }
00182
00183 return ret_container ;
00184 }
00185
00196 void
00197 BESContainerStorageFile::add_container( const string &sym_name,
00198 const string &real_name,
00199 const string &type )
00200 {
00201 string err = "Unable to add a container to a file, not yet implemented" ;
00202 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00203 }
00204
00214 bool
00215 BESContainerStorageFile::del_container( const string &s_name )
00216 {
00217 bool ret = false ;
00218 BESContainerStorageFile::Container_iter i ;
00219 i = _container_list.find( s_name ) ;
00220 if( i != _container_list.end() )
00221 {
00222 BESContainerStorageFile::container *c = (*i).second;
00223 _container_list.erase( i ) ;
00224 delete c ;
00225 ret = true ;
00226 }
00227 return ret ;
00228 }
00229
00237 bool
00238 BESContainerStorageFile::del_containers( )
00239 {
00240 while( _container_list.size() != 0 )
00241 {
00242 Container_iter ci = _container_list.begin() ;
00243 BESContainerStorageFile::container *c = (*ci).second;
00244 _container_list.erase( ci ) ;
00245 if( c )
00246 {
00247 delete c ;
00248 }
00249 }
00250 return true ;
00251 }
00252
00269 void
00270 BESContainerStorageFile::show_containers( BESInfo &info )
00271 {
00272 BESContainerStorageFile::Container_citer i ;
00273 i = _container_list.begin() ;
00274 for( i = _container_list.begin(); i != _container_list.end(); i++ )
00275 {
00276 BESContainerStorageFile::container *c = (*i).second;
00277 string sym = c->_symbolic_name ;
00278 string real = c->_real_name ;
00279 string type = c->_container_type ;
00280 show_container( sym, real, type, info ) ;
00281 }
00282 }
00283
00291 void
00292 BESContainerStorageFile::dump( ostream &strm ) const
00293 {
00294 strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
00295 << (void *)this << ")" << endl ;
00296 BESIndent::Indent() ;
00297 strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00298 strm << BESIndent::LMarg << "file: " << _file << endl ;
00299 if( _container_list.size() )
00300 {
00301 strm << BESIndent::LMarg << "containers:" << endl ;
00302 BESIndent::Indent() ;
00303 BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00304 BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00305 for( i = _container_list.begin(); i != ie; i++ )
00306 {
00307 BESContainerStorageFile::container *c = (*i).second;
00308 strm << BESIndent::LMarg << c->_symbolic_name ;
00309 strm << ", " << c->_real_name ;
00310 strm << ", " << c->_container_type ;
00311 strm << endl ;
00312 }
00313 BESIndent::UnIndent() ;
00314 }
00315 else
00316 {
00317 strm << BESIndent::LMarg << " containers: none" << endl ;
00318 }
00319 BESIndent::UnIndent() ;
00320 }
00321