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 <sstream>
00034 #include <fstream>
00035 #include <iostream>
00036
00037 using std::stringstream ;
00038 using std::ifstream ;
00039
00040 #include "BESContainerStorageFile.h"
00041 #include "BESFileContainer.h"
00042 #include "TheBESKeys.h"
00043 #include "BESInternalError.h"
00044 #include "BESInfo.h"
00045
00075 BESContainerStorageFile::BESContainerStorageFile( const string &n )
00076 : BESContainerStorage( n )
00077 {
00078
00079
00080
00081
00082 string key = "BES.Container.Persistence.File." + n ;
00083 bool found = false ;
00084 _file = TheBESKeys::TheKeys()->get_key( key, found ) ;
00085 if( _file == "" )
00086 {
00087 string s = key + " not defined in key file" ;
00088 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00089 }
00090
00091 ifstream persistence_file( _file.c_str() ) ;
00092 if( !persistence_file )
00093 {
00094 string s = "Unable to open persistence file " + _file ;
00095 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00096 }
00097
00098 char cline[80] ;
00099
00100 while( !persistence_file.eof() )
00101 {
00102 stringstream strm ;
00103 persistence_file.getline( cline, 80 ) ;
00104 if( !persistence_file.eof() )
00105 {
00106 strm << cline ;
00107 BESContainerStorageFile::container *c =
00108 new BESContainerStorageFile::container ;
00109 strm >> c->_symbolic_name ;
00110 strm >> c->_real_name ;
00111 strm >> c->_container_type ;
00112 string dummy ;
00113 strm >> dummy ;
00114 if( c->_symbolic_name == "" ||
00115 c->_real_name == "" ||
00116 c->_container_type == "" )
00117 {
00118 delete c ;
00119 persistence_file.close() ;
00120 string s = "Incomplete container persistence line in file "
00121 + _file ;
00122 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00123 }
00124 if( dummy != "" )
00125 {
00126 persistence_file.close() ;
00127 delete c ;
00128 string s = "Too many fields in persistence file "
00129 + _file ;
00130 throw BESInternalError( s, __FILE__, __LINE__ ) ;
00131 }
00132 _container_list[c->_symbolic_name] = c ;
00133 }
00134 }
00135 persistence_file.close() ;
00136 }
00137
00138 BESContainerStorageFile::~BESContainerStorageFile()
00139 {
00140 BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00141 BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00142 for( ; i != ie; i++ )
00143 {
00144 BESContainerStorageFile::container *c = (*i).second ;
00145 delete c ;
00146 }
00147 }
00148
00160 BESContainer *
00161 BESContainerStorageFile::look_for( const string &sym_name )
00162 {
00163 BESFileContainer *ret_container = 0 ;
00164 BESContainerStorageFile::Container_citer i ;
00165 i = _container_list.find( sym_name ) ;
00166 if( i != _container_list.end() )
00167 {
00168 BESContainerStorageFile::container *c = (*i).second;
00169 ret_container = new BESFileContainer( c->_symbolic_name,
00170 c->_real_name,
00171 c->_container_type ) ;
00172 }
00173
00174 return ret_container ;
00175 }
00176
00187 void
00188 BESContainerStorageFile::add_container( const string &sym_name,
00189 const string &real_name,
00190 const string &type )
00191 {
00192 string err = "Unable to add a container to a file, not yet implemented" ;
00193 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00194 }
00195
00205 bool
00206 BESContainerStorageFile::del_container( const string &s_name )
00207 {
00208 bool ret = false ;
00209 BESContainerStorageFile::Container_iter i ;
00210 i = _container_list.find( s_name ) ;
00211 if( i != _container_list.end() )
00212 {
00213 BESContainerStorageFile::container *c = (*i).second;
00214 _container_list.erase( i ) ;
00215 delete c ;
00216 ret = true ;
00217 }
00218 return ret ;
00219 }
00220
00228 bool
00229 BESContainerStorageFile::del_containers( )
00230 {
00231 while( _container_list.size() != 0 )
00232 {
00233 Container_iter ci = _container_list.begin() ;
00234 BESContainerStorageFile::container *c = (*ci).second;
00235 _container_list.erase( ci ) ;
00236 if( c )
00237 {
00238 delete c ;
00239 }
00240 }
00241 return true ;
00242 }
00243
00260 void
00261 BESContainerStorageFile::show_containers( BESInfo &info )
00262 {
00263 info.add_tag( "name", get_name() ) ;
00264 BESContainerStorageFile::Container_citer i ;
00265 i = _container_list.begin() ;
00266 for( i = _container_list.begin(); i != _container_list.end(); i++ )
00267 {
00268 info.begin_tag( "container" ) ;
00269 BESContainerStorageFile::container *c = (*i).second;
00270 string sym = c->_symbolic_name ;
00271 info.add_tag( "symbolicName", sym ) ;
00272 string real = c->_real_name ;
00273 info.add_tag( "realName", real ) ;
00274 string type = c->_container_type ;
00275 info.add_tag( "dataType", type ) ;
00276 info.end_tag( "container" ) ;
00277 }
00278 }
00279
00287 void
00288 BESContainerStorageFile::dump( ostream &strm ) const
00289 {
00290 strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
00291 << (void *)this << ")" << endl ;
00292 BESIndent::Indent() ;
00293 strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00294 strm << BESIndent::LMarg << "file: " << _file << endl ;
00295 if( _container_list.size() )
00296 {
00297 strm << BESIndent::LMarg << "containers:" << endl ;
00298 BESIndent::Indent() ;
00299 BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00300 BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00301 for( i = _container_list.begin(); i != ie; i++ )
00302 {
00303 BESContainerStorageFile::container *c = (*i).second;
00304 strm << BESIndent::LMarg << c->_symbolic_name ;
00305 strm << ", " << c->_real_name ;
00306 strm << ", " << c->_container_type ;
00307 strm << endl ;
00308 }
00309 BESIndent::UnIndent() ;
00310 }
00311 else
00312 {
00313 strm << BESIndent::LMarg << " containers: none" << endl ;
00314 }
00315 BESIndent::UnIndent() ;
00316 }
00317