Back to index

nordugrid-arc-nox  1.1.0~rc6
conf.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <arc/StringConv.h>
00006 
00007 #include "conf.h"
00008 #include "../misc/escaped.h"
00009 #include "environment.h"
00010 
00011 #if defined __GNUC__ && __GNUC__ >= 3
00012 
00013 #include <limits>
00014 #define istream_readline(__f,__s,__n) {      \
00015    __f.get(__s,__n,__f.widen('\n'));         \
00016    if(__f.fail()) __f.clear();               \
00017    __f.ignore(std::numeric_limits<std::streamsize>::max(), __f.widen('\n')); \
00018 }
00019 
00020 #else
00021 
00022 #define istream_readline(__f,__s,__n) {      \
00023    __f.get(__s,__n,'\n');         \
00024    if(__f.fail()) __f.clear();               \
00025    __f.ignore(INT_MAX,'\n'); \
00026 }
00027 
00028 #endif
00029 
00030 bool config_open(std::ifstream &cfile) {
00031   return config_open(cfile,nordugrid_config_loc());
00032 }
00033 
00034 bool config_open(std::ifstream &cfile,const std::string &name) {
00035   cfile.open(name.c_str(),std::ifstream::in);
00036   return cfile.is_open();
00037 }
00038 
00039 bool config_close(std::ifstream &cfile) {
00040   if(cfile.is_open()) cfile.close();
00041   return true;
00042 }
00043 
00044 std::string config_read_line(std::istream &cfile,std::string &rest,char separator) {
00045   rest = config_read_line(cfile);
00046   return config_next_arg(rest,separator);
00047 }
00048 
00049 std::string config_read_line(std::istream &cfile) {
00050   std::string rest;
00051   for(;;) {
00052     if(cfile.eof()) { rest=""; return rest; };
00053     {
00054       char buf[4096];
00055       istream_readline(cfile,buf,sizeof(buf));
00056       rest=buf;
00057     };
00058     std::string::size_type n=rest.find_first_not_of(" \t");
00059     if(n == std::string::npos) continue; /* empty string - skip */
00060     if(rest[n] == '#') continue; /* comment - skip */
00061     break;
00062   };
00063   return rest;
00064 }
00065 
00066 std::string config_next_arg(std::string &rest,char separator) {
00067   int n;
00068   std::string arg;
00069   n=input_escaped_string(rest.c_str(),arg,separator);
00070   rest=rest.substr(n);
00071   return arg;
00072 }    
00073 
00074 config_file_type config_detect(std::istream& in) {
00075   char inchar;
00076   while(in.good()) {
00077     inchar = (char)(in.get());
00078     if(isspace(inchar)) continue;
00079     if(inchar == '<') {
00080       // XML starts from < even if it is comment
00081       in.putback(inchar);
00082       return config_file_XML;
00083     };
00084     if((inchar == '#') || (inchar = '[')) {
00085       // INI file starts from comment or section
00086       in.putback(inchar);
00087       return config_file_INI;
00088     };
00089   };
00090   in.putback(inchar);
00091   return config_file_unknown;
00092 }
00093 
00094 
00095 bool elementtobool(Arc::XMLNode pnode,const char* ename,bool& val,Arc::Logger* logger) {
00096   std::string v = ename?pnode[ename]:pnode;
00097   if(v.empty()) return true; // default
00098   if((v == "true") || (v == "1")) {
00099     val=true;
00100     return true;
00101   };
00102   if((v == "false") || (v == "0")) {
00103     val=false;
00104     return true;
00105   };
00106   if(logger && ename) logger->msg(Arc::ERROR,"wrong boolean in %s: %s",ename,v.c_str());
00107   return false;
00108 }
00109 
00110 bool elementtoint(Arc::XMLNode pnode,const char* ename,unsigned int& val,Arc::Logger* logger) {
00111   std::string v = ename?pnode[ename]:pnode;
00112   if(v.empty()) return true; // default
00113   if(Arc::stringto(v,val)) return true;
00114   if(logger && ename) logger->msg(Arc::ERROR,"wrong number in %s: %s",ename,v.c_str());
00115   return false;
00116 }
00117 
00118 bool elementtoint(Arc::XMLNode pnode,const char* ename,int& val,Arc::Logger* logger) {
00119   std::string v = ename?pnode[ename]:pnode;
00120   if(v.empty()) return true; // default
00121   if(Arc::stringto(v,val)) return true;
00122   if(logger && ename) logger->msg(Arc::ERROR,"wrong number in %s: %s",ename,v.c_str());
00123   return false;
00124 }
00125