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 "BESContainerStorageException.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 BESContainerStorageException( 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 BESContainerStorageException( 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 string s = "Incomplete container persistence line in file "
00120 + _file ;
00121 throw BESContainerStorageException( s, __FILE__, __LINE__ ) ;
00122 }
00123 if( dummy != "" )
00124 {
00125 delete c ;
00126 string s = "Too many fields in persistence file "
00127 + _file ;
00128 throw BESContainerStorageException( s, __FILE__, __LINE__ ) ;
00129 }
00130 _container_list[c->_symbolic_name] = c ;
00131 }
00132 }
00133 persistence_file.close() ;
00134 }
00135
00136 BESContainerStorageFile::~BESContainerStorageFile()
00137 {
00138 BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00139 BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00140 for( ; i != ie; i++ )
00141 {
00142 BESContainerStorageFile::container *c = (*i).second ;
00143 delete c ;
00144 }
00145 }
00146
00158 BESContainer *
00159 BESContainerStorageFile::look_for( const string &sym_name )
00160 {
00161 BESFileContainer *ret_container = 0 ;
00162 BESContainerStorageFile::Container_citer i ;
00163 i = _container_list.find( sym_name ) ;
00164 if( i != _container_list.end() )
00165 {
00166 BESContainerStorageFile::container *c = (*i).second;
00167 ret_container = new BESFileContainer( c->_symbolic_name,
00168 c->_real_name,
00169 c->_container_type ) ;
00170 }
00171
00172 return ret_container ;
00173 }
00174
00185 void
00186 BESContainerStorageFile::add_container( const string &sym_name,
00187 const string &real_name,
00188 const string &type )
00189 {
00190 string err = "Unable to add a container to a file, not yet implemented" ;
00191 throw BESContainerStorageException( err, __FILE__, __LINE__ ) ;
00192 }
00193
00203 bool
00204 BESContainerStorageFile::del_container( const string &s_name )
00205 {
00206 bool ret = false ;
00207 BESContainerStorageFile::Container_iter i ;
00208 i = _container_list.find( s_name ) ;
00209 if( i != _container_list.end() )
00210 {
00211 BESContainerStorageFile::container *c = (*i).second;
00212 _container_list.erase( i ) ;
00213 delete c ;
00214 ret = true ;
00215 }
00216 return ret ;
00217 }
00218
00226 bool
00227 BESContainerStorageFile::del_containers( )
00228 {
00229 while( _container_list.size() != 0 )
00230 {
00231 Container_iter ci = _container_list.begin() ;
00232 BESContainerStorageFile::container *c = (*ci).second;
00233 _container_list.erase( ci ) ;
00234 if( c )
00235 {
00236 delete c ;
00237 }
00238 }
00239 return true ;
00240 }
00241
00258 void
00259 BESContainerStorageFile::show_containers( BESInfo &info )
00260 {
00261 info.add_tag( "name", get_name() ) ;
00262 BESContainerStorageFile::Container_citer i ;
00263 i = _container_list.begin() ;
00264 for( i = _container_list.begin(); i != _container_list.end(); i++ )
00265 {
00266 info.begin_tag( "container" ) ;
00267 BESContainerStorageFile::container *c = (*i).second;
00268 string sym = c->_symbolic_name ;
00269 info.add_tag( "symbolicName", sym ) ;
00270 string real = c->_real_name ;
00271 info.add_tag( "realName", real ) ;
00272 string type = c->_container_type ;
00273 info.add_tag( "dataType", type ) ;
00274 info.end_tag( "container" ) ;
00275 }
00276 }
00277
00285 void
00286 BESContainerStorageFile::dump( ostream &strm ) const
00287 {
00288 strm << BESIndent::LMarg << "BESContainerStorageFile::dump - ("
00289 << (void *)this << ")" << endl ;
00290 BESIndent::Indent() ;
00291 strm << BESIndent::LMarg << "name: " << get_name() << endl ;
00292 strm << BESIndent::LMarg << "file: " << _file << endl ;
00293 if( _container_list.size() )
00294 {
00295 strm << BESIndent::LMarg << "containers:" << endl ;
00296 BESIndent::Indent() ;
00297 BESContainerStorageFile::Container_citer i = _container_list.begin() ;
00298 BESContainerStorageFile::Container_citer ie = _container_list.end() ;
00299 for( i = _container_list.begin(); i != ie; i++ )
00300 {
00301 BESContainerStorageFile::container *c = (*i).second;
00302 strm << BESIndent::LMarg << c->_symbolic_name ;
00303 strm << ", " << c->_real_name ;
00304 strm << ", " << c->_container_type ;
00305 strm << endl ;
00306 }
00307 BESIndent::UnIndent() ;
00308 }
00309 else
00310 {
00311 strm << BESIndent::LMarg << " containers: none" << endl ;
00312 }
00313 BESIndent::UnIndent() ;
00314 }
00315