Back to index

nordugrid-arc-nox  1.1.0~rc6
conf_sections.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <fstream>
00006 #include <string>
00007 #include <list>
00008 
00009 #include "conf.h"
00010 
00011 
00012 #include "conf_sections.h"
00013 
00014 
00015 ConfigSections::~ConfigSections(void) {
00016   if(fin && open) {
00017     ((std::ifstream*)(fin))->close();
00018     delete fin;
00019   };
00020 }
00021 
00022 ConfigSections::ConfigSections(std::istream& f):fin(NULL),open(false) {
00023   fin=&f;
00024   line_number=0;
00025   current_section_n=-1;
00026   current_section_p=section_names.end();
00027   current_section_changed=false;
00028 }
00029 
00030 ConfigSections::ConfigSections(const char* filename):fin(NULL),open(false) {
00031   line_number=0;
00032   current_section_n=-1;
00033   current_section_p=section_names.end();
00034   if(!filename) return;
00035   fin=new std::ifstream(filename);
00036   if(*fin) open=true;
00037   current_section_changed=false;
00038 }
00039 
00040 bool ConfigSections::AddSection(const char* name) {
00041   if(name) section_names.push_back(std::string(name));
00042   return true;
00043 }
00044 
00045 bool ConfigSections::ReadNext(std::string& line) {
00046   if(!fin) return false;
00047   if(!*fin) return false;
00048   current_section_changed=false;
00049   for(;;) {
00050     line=config_read_line(*fin);
00051     if(line=="") { // eof
00052       current_section="";
00053       current_section_n=-1;
00054       current_section_p=section_names.end();
00055       current_section_changed=true;
00056       return true;
00057     };
00058     std::string::size_type n=line.find_first_not_of(" \t");
00059     if(n == std::string::npos) continue; // should never happen
00060     if(line[n] == '[') {  // section
00061       n++; std::string::size_type nn = line.find(']',n);
00062       if(nn == std::string::npos) { line=""; return false; }; // missing ']'
00063       current_section=line.substr(n,nn-n);
00064       current_section_n=-1;
00065       current_section_p=section_names.end();
00066       current_section_changed=true;
00067       continue;
00068     };
00069     if(section_names.size()) { // only limited sections allowed
00070       bool match = false;
00071       int s_n = -1;
00072       for(std::list<std::string>::iterator sec = section_names.begin();
00073                                        sec!=section_names.end();++sec) {
00074         std::string::size_type len = sec->length();
00075         s_n++;
00076         if(strncasecmp(sec->c_str(),current_section.c_str(),len) != 0) continue;
00077         if(len != current_section.length()) {
00078           if(current_section[len] != '/') continue;
00079         };
00080         current_section_n=s_n;
00081         current_section_p=sec;
00082         match=true; break;
00083       };
00084       if(!match) continue;
00085     };
00086     line.erase(0,n);
00087     break;
00088   };
00089   return true;
00090 }
00091 
00092 bool ConfigSections::ReadNext(std::string& name,std::string& value) {
00093   if(!ReadNext(name)) return false;
00094   std::string::size_type n = name.find('=');
00095   if(n == std::string::npos) { value=""; return true; };
00096   value=name.c_str()+n+1;
00097   name.erase(n);
00098   std::string::size_type l = value.length();
00099   for(n = 0;n<l;n++) if((value[n] != ' ') && (value[n] != '\t')) break;
00100   if(n>=l) { value=""; return true; };
00101   if(n) value.erase(0,n);
00102   if(value[0] != '"') return true;
00103   std::string::size_type nn = value.rfind('"');
00104   if(nn == 0) return true; // strange
00105   std::string::size_type n_ = value.find('"',1);
00106   if((nn > n_) && (n_ != 1)) return true;
00107   value.erase(nn);
00108   value.erase(0,1);
00109   return true;
00110 }
00111 
00112 const char* ConfigSections::SubSectionMatch(const char* name) {
00113   const char* subsection = current_section.c_str();
00114   if(current_section_n>=0) subsection+=current_section_p->length()+1;
00115   int l = strlen(name);
00116   if(strncmp(name,subsection,l) != 0) return NULL;
00117   if(subsection[l] == 0) return (subsection+l);
00118   if(subsection[l] == '/') return (subsection+l+1);
00119   return NULL;
00120 }
00121