#include <ModulesReader.h>
Classes | |
struct | Option_t |
Public Member Functions | |
reader () | |
virtual | ~reader () |
int | ReadFile (const char *name) |
void | PrintAllOptions () const |
int | HowMany (const std::string &name) const |
bool | DumpInputFile () const |
size_t | GetNumModules () const |
std::string | GetModule (unsigned int i) const |
modules::options * | GetOptions (unsigned int i) const |
void | SetDebug () |
void | SetDebugAll () |
Private Types | |
enum | OptionMode_t { kSet, kAppend } |
typedef std::vector< std::string > | OptionsList |
typedef std::map< std::string, modules::options * > | SectionsList |
typedef std::vector< std::pair < std::string, modules::options * > > | ModuleList |
typedef std::map< std::string, int > | ModuleCounts |
Private Member Functions | |
int | OpenFile (const char *name, std::ifstream &infile) |
bool | AddSection (const std::string &name, const std::string &type="") |
int | AddModule (std::string line) |
void | ProcessGlobalOption (Option_t opt) |
void | AddOption (const std::string &module, const Option_t &opt) |
void | AddOption (const std::string &module, const std::string &flag) |
void | AddOption (modules::options *module, const Option_t &opt) |
void | AddOption (modules::options *module, const std::string &flag) |
void | AddOptionAll (const std::string &key, const std::string &value="") |
void | AddOptionAll (const Option_t &opt) |
Option_t | SplitOption (const std::string &line) |
int | MakeModules (const SectionsList &) |
bool | isComment (std::stringstream &line) |
std::string | findSectionName (const std::string &line) |
std::ostream & | PrintProblem () |
Private Attributes | |
SectionsList | fAllOptions |
ModuleList | fModules |
ModuleCounts | fModulesCounts |
int | fLineNumber |
bool | fShouldPrint |
bool | fDebugAll |
bool | fDumpContents |
bool | fDumpInputFile |
Static Private Attributes | |
static const char * | fGlobalModule = "global" |
Definition at line 15 of file ModulesReader.h.
typedef std::map<std::string,int > modules::reader::ModuleCounts [private] |
Definition at line 21 of file ModulesReader.h.
typedef std::vector<std::pair<std::string, modules::options*> > modules::reader::ModuleList [private] |
Definition at line 20 of file ModulesReader.h.
typedef std::vector<std::string> modules::reader::OptionsList [private] |
Definition at line 18 of file ModulesReader.h.
typedef std::map<std::string,modules::options* > modules::reader::SectionsList [private] |
Definition at line 19 of file ModulesReader.h.
enum modules::reader::OptionMode_t [private] |
modules::reader::reader | ( | ) | [inline] |
Definition at line 31 of file ModulesReader.h.
00031 :fShouldPrint(false),fDebugAll(false),fDumpContents(false),fDumpInputFile(false){};
virtual modules::reader::~reader | ( | ) | [inline, virtual] |
Definition at line 34 of file ModulesReader.h.
int modules::reader::AddModule | ( | std::string | line | ) | [private] |
Definition at line 104 of file ModulesReader.cpp.
References modules::options::AddArgument(), AddOption(), AddSection(), modules::parser::Constructor_t::before, fAllOptions, fDebugAll, fModules, fModulesCounts, fShouldPrint, modules::parser::Constructor_t::inside, modules::parser::ParseConstructor(), PrintProblem(), modules::options::SetAlias(), modules::parser::TokeniseByDelimiter(), and modules::parser::TrimWhiteSpaceBeforeAfter().
Referenced by ReadFile().
00104 { 00105 std::string alias, type; 00106 00107 // How is this module specified ? 00108 // If the line contains an equals sign, assume we have a alias and a type 00109 // (and possibly arguments), if not, assume it's just a type (and possibly 00110 // arguments) 00111 Constructor_t constructor=ParseConstructor(line,'(',')'); 00112 00113 std::vector<std::string> name_alias; 00114 TokeniseByDelimiter(constructor.before, name_alias,"="); 00115 if(name_alias.size()>2) { 00116 PrintProblem()<<"Multiple equals signs (=) in line gives an ambiguous alias / alias."<<std::endl; 00117 return 1; 00118 }else if(name_alias.size()==2) { 00119 alias=name_alias[0]; 00120 type=name_alias[1]; 00121 TrimWhiteSpaceBeforeAfter(alias); 00122 }else if(name_alias.size()==1) type=name_alias[0]; 00123 else { 00124 PrintProblem()<<"No module requested"<<std::endl; 00125 return 2; 00126 } 00127 TrimWhiteSpaceBeforeAfter(type); 00128 00129 // Get arguments to the module if they're provided 00130 std::vector<std::string> args; 00131 TokeniseByDelimiter(constructor.inside,args,","); 00132 00133 if(fShouldPrint){ 00134 std::cout<<"Adding module: '"<<type<<"'"<<std::endl; 00135 std::cout<<" with alias: '"<<alias<<"'"<<std::endl; 00136 std::cout<<" and "<<args.size()<<" arguments:"<<std::endl; 00137 for(unsigned i=0;i<args.size() ;i++) 00138 std::cout<<" "<<i<<'\t'<<args[i]<<std::endl; 00139 } 00140 00141 //Add the module to the list of modules 00142 modules::options* opts; 00143 if(alias==""){ 00144 opts=new modules::options(type); 00145 }else{ 00146 // since the module has been named, create a section to store other 00147 // options that may be provided subsequently 00148 if(!AddSection(alias,type)){ 00149 PrintProblem()<<" module alias is being reused"<<std::endl; 00150 return 1; 00151 } 00152 opts=fAllOptions[alias]; 00153 opts->SetAlias(alias); 00154 } 00155 // Add all arguments passed to this module to it's options 00156 for(unsigned i=0;i<args.size();i++) { 00157 opts->AddArgument(i,args[i]); 00158 } 00159 00160 // Stick this module into the list of modules 00161 fModules.push_back(std::make_pair(type,opts)); 00162 fModulesCounts[type]++; 00163 00164 // Apply current global options to new module 00165 if(fDebugAll) AddOption(opts,"debug"); 00166 00167 return 0; 00168 }
void modules::reader::AddOption | ( | modules::options * | module, | |
const std::string & | flag | |||
) | [private] |
Definition at line 211 of file ModulesReader.cpp.
References modules::options::SetOption().
void modules::reader::AddOption | ( | modules::options * | module, | |
const Option_t & | opt | |||
) | [private] |
Definition at line 193 of file ModulesReader.cpp.
References modules::options::AppendToOption(), fShouldPrint, kAppend, modules::reader::Option_t::key, kSet, modules::reader::Option_t::mode, modules::options::SetOption(), and modules::reader::Option_t::value.
00193 { 00194 bool exists; 00195 switch (opt.mode){ 00196 case kSet: 00197 options->SetOption(opt.key,opt.value); 00198 break; 00199 case kAppend: 00200 exists= options->AppendToOption(opt.key,opt.value); 00201 if(!exists &&fShouldPrint){ 00202 std::cout<<"Cannot append to '"<<opt.key<<"' as it doesn't already exist"<<std::endl; 00203 } 00204 break; 00205 default: 00206 std::cout<<"modules::reader::AddOption(): That's odd! How did I end up here?"<<std::endl; 00207 break; 00208 } 00209 }
void modules::reader::AddOption | ( | const std::string & | module, | |
const std::string & | flag | |||
) | [inline, private] |
Definition at line 54 of file ModulesReader.h.
References AddOption(), and fAllOptions.
Referenced by AddOption().
00054 { AddOption(fAllOptions[module],flag); }
void modules::reader::AddOption | ( | const std::string & | module, | |
const Option_t & | opt | |||
) | [inline, private] |
Definition at line 53 of file ModulesReader.h.
References AddOption(), and fAllOptions.
Referenced by AddModule(), AddOption(), and ReadFile().
00053 { AddOption(fAllOptions[module],opt); }
void modules::reader::AddOptionAll | ( | const Option_t & | opt | ) | [inline, private] |
Definition at line 86 of file ModulesReader.h.
References AddOptionAll(), modules::reader::Option_t::key, and modules::reader::Option_t::value.
00086 { 00087 AddOptionAll(opt.key,opt.value); 00088 }
void modules::reader::AddOptionAll | ( | const std::string & | key, | |
const std::string & | value = "" | |||
) | [private] |
Definition at line 259 of file ModulesReader.cpp.
References fAllOptions, and fGlobalModule.
Referenced by AddOptionAll(), and SetDebugAll().
00259 { 00260 for(SectionsList::iterator it_sec=fAllOptions.begin(); it_sec != fAllOptions.end();it_sec++){ 00261 if(it_sec->first==fGlobalModule) continue; 00262 it_sec->second->SetOption(key,value); 00263 } 00264 }
bool modules::reader::AddSection | ( | const std::string & | name, | |
const std::string & | type = "" | |||
) | [inline, private] |
Definition at line 78 of file ModulesReader.h.
References fAllOptions.
Referenced by AddModule(), and ReadFile().
00078 { 00079 if(fAllOptions.find(name)==fAllOptions.end()){ 00080 fAllOptions[name] =new modules::options(type); 00081 return true; 00082 } 00083 return false; 00084 }
bool modules::reader::DumpInputFile | ( | ) | const [inline] |
Definition at line 40 of file ModulesReader.h.
References fDumpInputFile.
Referenced by modules::navigator::LoadConfigFile().
00040 {return fDumpInputFile;}
std::string modules::reader::findSectionName | ( | const std::string & | line | ) | [private] |
Definition at line 98 of file ModulesReader.cpp.
References modules::parser::Constructor_t::inside, and modules::parser::ParseConstructor().
Referenced by ReadFile().
00098 { 00099 // sections are marked with [] either side of the name 00100 Constructor_t section= modules::parser::ParseConstructor(line,'[',']'); 00101 return section.inside; 00102 }
std::string modules::reader::GetModule | ( | unsigned int | i | ) | const [inline] |
Definition at line 43 of file ModulesReader.h.
References fModules.
Referenced by load_config_file(), and modules::navigator::MakeModules().
00043 {return fModules[i].first;};
size_t modules::reader::GetNumModules | ( | ) | const [inline] |
Definition at line 42 of file ModulesReader.h.
References fModules.
Referenced by load_config_file(), modules::navigator::LoadConfigFile(), and modules::navigator::MakeModules().
00042 {return fModules.size();};
modules::options* modules::reader::GetOptions | ( | unsigned int | i | ) | const [inline] |
Definition at line 44 of file ModulesReader.h.
References fModules.
Referenced by load_config_file(), and modules::navigator::MakeModules().
00044 {return fModules[i].second;};
int modules::reader::HowMany | ( | const std::string & | name | ) | const |
Definition at line 299 of file ModulesReader.cpp.
References fModulesCounts.
Referenced by modules::navigator::HowMany().
00299 { 00300 ModuleCounts::const_iterator it=fModulesCounts.find(name); 00301 if(it!=fModulesCounts.end()) return it->second; 00302 return 0; 00303 }
bool modules::reader::isComment | ( | std::stringstream & | line | ) | [private] |
Definition at line 86 of file ModulesReader.cpp.
Referenced by ReadFile().
int modules::reader::MakeModules | ( | const SectionsList & | ) | [private] |
int modules::reader::OpenFile | ( | const char * | name, | |
std::ifstream & | infile | |||
) | [private] |
Definition at line 75 of file ModulesReader.cpp.
References fShouldPrint.
Referenced by ReadFile().
00075 { 00076 infile.close(); 00077 infile.open(name, std::ifstream::in); 00078 if(!infile.is_open()) { 00079 std::cout<<"Problem opening modules file: "<<name<<std::endl; 00080 return 1; 00081 } 00082 if(fShouldPrint) std::cout<<"Found modules file: "<<name<<std::endl; 00083 return 0; 00084 }
void modules::reader::PrintAllOptions | ( | ) | const |
Definition at line 215 of file ModulesReader.cpp.
References fAllOptions, fGlobalModule, and fModules.
Referenced by load_config_file(), and ReadFile().
00215 { 00216 // Global options that were set 00217 SectionsList::const_iterator it_sec=fAllOptions.find(fGlobalModule); 00218 if(it_sec!=fAllOptions.end() && it_sec->second->GetNumOptions()>0){ 00219 std::cout<<"== Global otions =="<<std::endl; 00220 it_sec->second->DumpOptions(); 00221 std::cout<<std::endl; 00222 } 00223 00224 // Modules that were requested 00225 std::cout<<"== All modules =="<<std::endl; 00226 ModuleList::const_iterator it_mod; 00227 for(it_mod=fModules.begin(); it_mod != fModules.end();it_mod++){ 00228 std::cout<<"Module: "<<it_mod->first<<std::endl; 00229 it_mod->second->DumpOptions(); 00230 std::cout<<std::endl; 00231 } 00232 00233 // Sections that were added, but don't correspond to a module 00234 std::cout<<"== Unused sections =="<<std::endl; 00235 for(it_sec=fAllOptions.begin(); it_sec != fAllOptions.end();it_sec++){ 00236 if(it_sec->first==fGlobalModule) continue; 00237 for(it_mod=fModules.begin(); it_mod != fModules.end();it_mod++){ 00238 if(it_sec->second==it_mod->second ) break; 00239 } 00240 if(it_mod!=fModules.end()) continue; 00241 std::cout<<"Section: "<<it_sec->first<<std::endl; 00242 it_sec->second->DumpOptions(); 00243 std::cout<<std::endl; 00244 } 00245 }
std::ostream & modules::reader::PrintProblem | ( | ) | [private] |
Definition at line 295 of file ModulesReader.cpp.
References fLineNumber.
Referenced by AddModule().
00295 { 00296 return std::cout<<"Problem on line "<<fLineNumber<<": "; 00297 }
void modules::reader::ProcessGlobalOption | ( | Option_t | opt | ) | [private] |
Definition at line 266 of file ModulesReader.cpp.
References fDumpContents, fDumpInputFile, fShouldPrint, modules::factory::Instance(), modules::navigator::Instance(), kAppend, modules::reader::Option_t::key, modules::reader::Option_t::mode, TemplateFactory< BaseModule, OptionsType >::PrintPossibleModules(), SetDebug(), modules::navigator::SetDebug(), SetDebugAll(), modules::parser::TokeniseByWhiteSpace(), and modules::reader::Option_t::value.
Referenced by ReadFile().
00266 { 00267 if (opt.mode==kAppend) { 00268 if(fShouldPrint) std::cout<<"Warning: over-writing '"<<opt.key<<"' with '" 00269 << opt.value<<"' despite append operator being used"<<std::endl; 00270 } 00271 if (opt.key=="debug"){ 00272 std::vector<std::string> args; 00273 modules::parser::TokeniseByWhiteSpace(opt.value,args ); 00274 for(unsigned i=0;i<args.size();i++){ 00275 if (args[i]=="all"){ 00276 SetDebugAll(); 00277 }else if (args[i]=="modules_navigator"){ 00278 modules::navigator::Instance()->SetDebug(); 00279 }else if (args[i]=="modules_reader"){ 00280 SetDebug(); 00281 } 00282 } 00283 }else if(opt.key=="list_modules"){ 00284 modules::factory::Instance()->PrintPossibleModules(); 00285 }else if(opt.key=="show_input_data"){ 00286 fDumpInputFile=true; 00287 }else if(opt.key=="dump_contents"){ 00288 fDumpContents=true; 00289 }else { 00290 if(fShouldPrint) std::cout<<"Warning: Unknown global option given, '"<<opt.key<<"'"<<std::endl; 00291 return; 00292 } 00293 }
int modules::reader::ReadFile | ( | const char * | name | ) |
Definition at line 17 of file ModulesReader.cpp.
References AddModule(), AddOption(), AddSection(), fDumpContents, fGlobalModule, findSectionName(), fLineNumber, fShouldPrint, isComment(), OpenFile(), PrintAllOptions(), ProcessGlobalOption(), and SplitOption().
Referenced by load_config_file(), and modules::navigator::LoadConfigFile().
00017 { 00018 00019 // Open config file 00020 std::ifstream infile; 00021 int ret = OpenFile(name,infile); 00022 if(ret!=0) return ret; 00023 00024 // make sure we have a default section set up 00025 AddSection(fGlobalModule); 00026 00027 // Loop over each line 00028 std::string full_line; 00029 std::string section=fGlobalModule, new_section; 00030 Option_t current_opt; 00031 fLineNumber=0; 00032 while(std::getline(infile,full_line)){ 00033 fLineNumber++; 00034 00035 // tokenize the line straight away 00036 std::stringstream line(full_line); 00037 00038 // check if this line is a comment 00039 if(isComment(line) ) continue; 00040 00041 // If this line contains a section name make sure we begin a new section 00042 new_section=findSectionName(full_line); 00043 if(new_section!="") { 00044 section=new_section; 00045 if(fShouldPrint) std::cout<<"Found new section: '"<<section<<"'"<<std::endl; 00046 if(section!="MODULES") { 00047 AddSection(section); 00048 } 00049 continue; 00050 } 00051 00052 if(section=="MODULES") { 00053 // In the MODULES secton each line specifies a module to use 00054 ret=AddModule(full_line); 00055 if(ret!=0) return ret; 00056 }else{ 00057 // Parse the contents of this option 00058 current_opt=SplitOption(full_line); 00059 00060 // Handle the option as required 00061 if(section==fGlobalModule) { 00062 ProcessGlobalOption(current_opt); 00063 } else{ 00064 AddOption(section,current_opt); 00065 } 00066 if(fShouldPrint) std::cout<<"Found option: "<<full_line<<std::endl; 00067 } 00068 } 00069 00070 // were we asked to dump everything that's been processed? 00071 if(fDumpContents) PrintAllOptions(); 00072 return 0; 00073 }
void modules::reader::SetDebug | ( | ) |
Definition at line 247 of file ModulesReader.cpp.
References fShouldPrint, modules::factory::Instance(), and TemplateFactory< BaseModule, OptionsType >::SetDebug().
Referenced by ProcessGlobalOption().
00247 { 00248 std::cout<<"Debug mode activated"<<std::endl; 00249 fShouldPrint=true; 00250 modules::factory::Instance()->SetDebug(); 00251 }
void modules::reader::SetDebugAll | ( | ) |
Definition at line 253 of file ModulesReader.cpp.
References AddOptionAll(), and fDebugAll.
Referenced by ProcessGlobalOption().
00253 { 00254 //SetDebug(); 00255 fDebugAll=true; 00256 AddOptionAll("debug"); 00257 }
modules::reader::Option_t modules::reader::SplitOption | ( | const std::string & | line | ) | [private] |
Definition at line 170 of file ModulesReader.cpp.
References kAppend, modules::reader::Option_t::key, kSet, modules::reader::Option_t::mode, and modules::reader::Option_t::value.
Referenced by ReadFile().
00170 { 00171 Option_t opt; 00172 opt.value=""; 00173 opt.mode=kSet; 00174 00175 //split the line around any equals sign 00176 size_t equ_pos=line.find('='); 00177 if(equ_pos==std::string::npos) { 00178 // no equal sign, treat option as bool flag 00179 opt.key=line; 00180 }else if(line[equ_pos-1]=='+'){ 00181 // option should be appended to an existing one 00182 opt.key=line.substr(0,equ_pos-1); 00183 opt.value=line.substr(equ_pos+1); 00184 opt.mode=kAppend; 00185 }else{ 00186 // option is simple key value pair 00187 opt.key=line.substr(0,equ_pos); 00188 opt.value=line.substr(equ_pos+1); 00189 } 00190 return opt; 00191 }
SectionsList modules::reader::fAllOptions [private] |
Definition at line 67 of file ModulesReader.h.
Referenced by AddModule(), AddOption(), AddOptionAll(), AddSection(), and PrintAllOptions().
bool modules::reader::fDebugAll [private] |
Definition at line 73 of file ModulesReader.h.
Referenced by AddModule(), and SetDebugAll().
bool modules::reader::fDumpContents [private] |
Definition at line 74 of file ModulesReader.h.
Referenced by ProcessGlobalOption(), and ReadFile().
bool modules::reader::fDumpInputFile [private] |
Definition at line 75 of file ModulesReader.h.
Referenced by DumpInputFile(), and ProcessGlobalOption().
const char * modules::reader::fGlobalModule = "global" [static, private] |
Definition at line 70 of file ModulesReader.h.
Referenced by AddOptionAll(), PrintAllOptions(), and ReadFile().
int modules::reader::fLineNumber [private] |
Definition at line 71 of file ModulesReader.h.
Referenced by PrintProblem(), and ReadFile().
ModuleList modules::reader::fModules [private] |
Definition at line 68 of file ModulesReader.h.
Referenced by AddModule(), GetModule(), GetNumModules(), GetOptions(), and PrintAllOptions().
ModuleCounts modules::reader::fModulesCounts [private] |
Definition at line 69 of file ModulesReader.h.
Referenced by AddModule(), and HowMany().
bool modules::reader::fShouldPrint [private] |
Definition at line 72 of file ModulesReader.h.
Referenced by AddModule(), AddOption(), OpenFile(), ProcessGlobalOption(), ReadFile(), and SetDebug().