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 "BESSyntaxUserError.h"
00047 #include "BESInfo.h"
00048
00078 BESContainerStorageFile::BESContainerStorageFile( const string &n )
00079 : BESContainerStorage( n )
00080 {
00081
00082
00083
00084
00085 string key = "BES.Container.Persistence.File." + n ;
00086 bool found = false ;
00087 TheBESKeys::TheKeys()->get_value( key, _file, found ) ;
00088 if( _file == "" )
00089 {
00090 string s = key + " not defined in BES configuration file" ;
00091 throw BESSyntaxUserError( s, __FILE__, __LINE__ ) ;
00092 }
00093
00094 ifstream persistence_file( _file.c_str() ) ;
00095 int myerrno = errno ;
00096 if( !persistence_file )
00097 {
00098 char *err = strerror( myerrno ) ;
00099 string s = "Unable to open persistence file " + _file + ": " ;
00100 if( err )
00101 s += err ;
00102 else
00103 s += "Unknown error" ;
00104
00105 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00106 }
00107
00108 char cline[80] ;
00109
00110 while( !persistence_file.eof() )
00111 {
00112 stringstream strm ;
00113 persistence_file.getline( cline, 80 ) ;
00114 if( !persistence_file.eof() )
00115 {
00116 strm << cline ;
00117 BESContainerStorageFile::container *c =
00118 new BESContainerStorageFile::container ;
00119 strm >> c->_symbolic_name ;
00120 strm >> c->_real_name ;
00121 strm >> c->_container_type ;
00122 string dummy ;
00123 strm >> dummy ;
00124 if( c->_symbolic_name == "" ||
00125 c->_real_name == "" ||
00126 c->_container_type == "" )
00127 {
00128 delete c ;
00129 persistence_file.close() ;
00130 string s = "Incomplete container persistence line in file "
00131 + _file ;
00132 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00133 }
00134 if( dummy != "" )
00135 {
00136 persistence_file.close() ;
00137 delete c ;
00138 string s = "Too many fields in persistence file "
00139 + _file ;
00140 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00141 }
00142 _container_list[c->_symbolic_name] = c ;
00143 }
00144 }
00145 persistence_file.close() ;
00146 }
00147
00148 BESContainerStorageFile::~BESContainerStorageFile()
00149 {
00150 BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00151 BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00152 for( ; i != ie; i++ )
00153 {
00154 BESContainerStorageFile::container *c = (*i).second ;
00155 delete c ;
00156 }
00157 }
00158
00170 BESContainer *
00171 BESContainerStorageFile::look_for( const string &sym_name )
00172 {
00173 BESFileContainer *ret_container = 0 ;
00174 BESContainerStorageFile::Container_citer i ;
00175 i = _container_list.find( sym_name ) ;
00176 if( i != _container_list.end() )
00177 {
00178 BESContainerStorageFile::container *c = (*i).second;
00179 ret_container = new BESFileContainer( c->_symbolic_name,
00180 c->_real_name,
00181 c->_container_type ) ;
00182 }
00183
00184 return ret_container ;
00185 }
00186
00197 void
00198 BESContainerStorageFile::add_container( const string &sym_name,
00199 const string &real_name,
00200 const string &type )
00201 {
00202 string err = "Unable to add a container to a file, not yet implemented" ;
00203 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00204 }
00205
00215 bool
00216 BESContainerStorageFile::del_container( const string &s_name )
00217 {
00218 bool ret = false ;
00219 BESContainerStorageFile::Container_iter i ;
00220 i = _container_list.find( s_name ) ;
00221 if( i != _container_list.end() )
00222 {
00223 BESContainerStorageFile::container *c = (*i).second;
00224 _container_list.erase( i ) ;
00225 if( c )
00226 {
00227 delete c ;
00228 }
00229 ret = true ;
00230 }
00231 return ret ;
00232 }
00233
00241 bool
00242 BESContainerStorageFile::del_containers( )
00243 {
00244 while( _container_list.size() != 0 )
00245 {
00246 Container_iter ci = _container_list.begin() ;
00247 BESContainerStorageFile::container *c = (*ci).second;
00248 _container_list.erase( ci ) ;
00249 if( c )
00250 {
00251 delete c ;
00252 }
00253 }
00254 return true ;
00255 }
00256
00273 void
00274 BESContainerStorageFile::show_containers( BESInfo &info )
00275 {
00276 BESContainerStorageFile::Container_citer i ;
00277 i = _container_list.begin() ;
00278 for( i = _container_list.begin(); i != _container_list.end(); i++ )
00279 {
00280 BESContainerStorageFile::container *c = (*i).second;
00281 string sym = c->_symbolic_name ;
00282 string real = c->_real_name ;
00283 string type = c->_container_type ;
00284 show_container( sym, real, type, info ) ;
00285 }
00286 }
00287
00295 void
00296 BESContainerStorageFile::dump( ostream &strm ) const
00297 {
00298 strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
00299 << (void *)this << ")" << endl ;
00300 BESIndent::Indent() ;
00301 strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00302 strm << BESIndent::LMarg << "file: " << _file << endl ;
00303 if( _container_list.size() )
00304 {
00305 strm << BESIndent::LMarg << "containers:" << endl ;
00306 BESIndent::Indent() ;
00307 BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00308 BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00309 for( i = _container_list.begin(); i != ie; i++ )
00310 {
00311 BESContainerStorageFile::container *c = (*i).second;
00312 strm << BESIndent::LMarg << c->_symbolic_name ;
00313 strm << ", " << c->_real_name ;
00314 strm << ", " << c->_container_type ;
00315 strm << endl ;
00316 }
00317 BESIndent::UnIndent() ;
00318 }
00319 else
00320 {
00321 strm << BESIndent::LMarg << " containers: none" << endl ;
00322 }
00323 BESIndent::UnIndent() ;
00324 }
00325