00001 // $Id$ 00002 // Author: John Wu <John.Wu at ACM.org> 00003 // Lawrence Berkeley National Laboratory 00004 // Copyright 2000-2012 the Regents of the University of California 00005 #ifndef IBIS_RESOURCE_H 00006 #define IBIS_RESOURCE_H 00007 00008 00009 #include "util.h" // ibis::util::strnewdup(), std::less<const char*> 00010 #include <fstream> // std::ofstream 00011 #include <map> // std::map 00012 00039 class FASTBIT_CXX_DLLSPEC ibis::resource { 00040 public: 00044 typedef std::map< const char*, resource*, ibis::lessi > gList; 00045 typedef std::map< const char*, char*, ibis::lessi > vList; 00046 00047 ~resource() {clear();}; 00049 resource() : prefix(0), context(0) {}; 00051 explicit resource(const char *fn) : prefix(0), context(0) {read(fn);} 00053 resource(const resource* ctx, const char* pfx) : 00054 prefix(ibis::util::strnewdup(pfx)), context(ctx) {} 00056 resource(const resource& rhs) : 00057 groups(rhs.groups), values(rhs.values), 00058 prefix(ibis::util::strnewdup(rhs.prefix)), context(rhs.context) {} 00059 resource& operator=(const resource& rhs); 00060 00062 const char* operator[](const char *name) const; 00064 double getNumber(const char* name) const; 00066 bool isTrue(const char *name) const; 00067 00069 void add(const char *name, const char *value); 00072 void read(const char* fn=0); 00077 void write(const char* fn=0) const; 00078 00080 bool empty() const {return (values.empty() && groups.empty());} 00081 gList::const_iterator gBegin() const {return groups.begin();} 00082 gList::const_iterator gEnd() const {return groups.end();} 00083 vList::const_iterator vBegin() const {return values.begin();} 00084 vList::const_iterator vEnd() const {return values.end();} 00085 00089 inline const resource* getGroup(const char* name) const; 00093 inline const char* getValue(const char *name) const; 00095 inline std::string getPrefix() const; 00096 00098 static void clear(vList &vl); 00100 static void parseNameValuePairs(const char* in, vList& lst); 00101 static bool isStringTrue(const char *val); 00102 00103 private: 00104 static const char* delimiters; 00105 gList groups; 00106 vList values; 00107 const char *prefix; 00108 const resource* context; 00109 00110 void clear(); // clear the memory occupied by the strings 00111 void write(std::ostream& out, const char* ctx=0) const; 00112 }; 00113 00114 // only search the top level level 00115 inline const ibis::resource* ibis::resource::getGroup(const char* name) const { 00116 const ibis::resource* group = 0; 00117 if (name==0) return group; 00118 if (*name==static_cast<char>(0)) return group; 00119 00120 gList::const_iterator it = groups.find(name); 00121 if (it != groups.end()) 00122 group = (*it).second; 00123 return group; 00124 } // ibis::resource::getGroup 00125 00126 // only search the top level 00127 inline const char* ibis::resource::getValue(const char* name) const { 00128 const char* value = 0; 00129 if (name==0) return value; 00130 if (*name==static_cast<char>(0)) return value; 00131 00132 vList::const_iterator it = values.find(name); 00133 if (it != values.end()) 00134 value = (*it).second; 00135 return value; 00136 } // ibis::resource::getValue 00137 00138 // get the full prefix of the resource 00139 inline std::string ibis::resource::getPrefix() const { 00140 std::string ret; 00141 if (context != 0) 00142 ret = context->getPrefix(); 00143 if (prefix != 0) { 00144 if (ret.empty()) { 00145 ret = prefix; 00146 } 00147 else { 00148 ret += '.'; 00149 ret += prefix; 00150 } 00151 } 00152 return ret; 00153 } // ibis::resource::getPrefix 00154 00159 inline bool ibis::resource::isStringTrue(const char *val) { 00160 return(val != 0 && *val != 0 && 00161 ((*val == '1') || (*val == 't') || (*val == 'y') || 00162 (*val == 'T') || (*val == 'Y') || (stricmp(val, "on") == 0))); 00163 } // ibis::resource::isStringTrue 00164 #endif // IBIS_RESOURCE_H
![]() |