resource.h
Go to the documentation of this file.
1 // $Id$
2 // Author: John Wu <John.Wu at ACM.org>
3 // Lawrence Berkeley National Laboratory
4 // Copyright (c) 2000-2016 the Regents of the University of California
5 #ifndef IBIS_RESOURCE_H
6 #define IBIS_RESOURCE_H
7 #include "util.h" // ibis::util::strnewdup(), std::less<const char*>
10 #include <fstream> // std::ofstream
11 #include <map> // std::map
12 
38 class FASTBIT_CXX_DLLSPEC ibis::resource {
39 public:
43  typedef std::map< const char*, resource*, ibis::lessi > gList;
44  typedef std::map< const char*, char*, ibis::lessi > vList;
45 
46  ~resource() {clear();};
48  resource() : prefix(0), context(0) {};
50  explicit resource(const char *fn) : prefix(0), context(0) {(void)read(fn);}
52  resource(const resource* ctx, const char* pfx) :
53  prefix(ibis::util::strnewdup(pfx)), context(ctx) {}
55  resource(const resource& rhs) :
56  groups(rhs.groups), values(rhs.values),
57  prefix(ibis::util::strnewdup(rhs.prefix)), context(rhs.context) {}
58  resource& operator=(const resource& rhs);
59 
61  const char* operator[](const char *name) const;
63  double getNumber(const char* name) const;
65  bool isTrue(const char *name) const;
66 
67  int read(const char* fn=0);
68  void write(const char* fn=0) const;
69  void add(const char *name, const char *value);
70 
72  bool empty() const {return (values.empty() && groups.empty());}
73  gList::const_iterator gBegin() const {return groups.begin();}
74  gList::const_iterator gEnd() const {return groups.end();}
75  vList::const_iterator vBegin() const {return values.begin();}
76  vList::const_iterator vEnd() const {return values.end();}
77 
81  inline const resource* getGroup(const char* name) const;
85  inline const char* getValue(const char *name) const;
87  inline std::string getPrefix() const;
88 
90  static void clear(vList &vl);
92  static void parseNameValuePairs(const char* in, vList& lst);
93  static bool isStringTrue(const char *val);
94 
95 private:
96  static const char* delimiters;
97  gList groups;
98  vList values;
99  const char *prefix;
100  const resource* context;
101 
102  void clear(); // clear the memory occupied by the strings
103  void write(std::ostream& out, const char* ctx=0) const;
104 };
105 
106 // only search the top level level
107 inline const ibis::resource* ibis::resource::getGroup(const char* name) const {
108  const ibis::resource* group = 0;
109  if (name==0) return group;
110  if (*name==static_cast<char>(0)) return group;
111 
112  gList::const_iterator it = groups.find(name);
113  if (it != groups.end())
114  group = (*it).second;
115  return group;
116 } // ibis::resource::getGroup
117 
118 // only search the top level
119 inline const char* ibis::resource::getValue(const char* name) const {
120  const char* value = 0;
121  if (name==0) return value;
122  if (*name==static_cast<char>(0)) return value;
123 
124  vList::const_iterator it = values.find(name);
125  if (it != values.end())
126  value = (*it).second;
127  return value;
128 } // ibis::resource::getValue
129 
130 // get the full prefix of the resource
131 inline std::string ibis::resource::getPrefix() const {
132  std::string ret;
133  if (context != 0)
134  ret = context->getPrefix();
135  if (prefix != 0) {
136  if (ret.empty()) {
137  ret = prefix;
138  }
139  else {
140  ret += '.';
141  ret += prefix;
142  }
143  }
144  return ret;
145 } // ibis::resource::getPrefix
146 
151 inline bool ibis::resource::isStringTrue(const char *val) {
152  return(val != 0 && *val != 0 &&
153  ((*val == '1') || (*val == 't') || (*val == 'y') ||
154  (*val == 'T') || (*val == 'Y') || (stricmp(val, "on") == 0)));
155 } // ibis::resource::isStringTrue
156 #endif // IBIS_RESOURCE_H
const char * operator[](const char *name) const
Locate the named parameter and return its value in raw string form.
Definition: resource.cpp:233
int read(const char *fn=0)
Read a configuration file.
Definition: resource.cpp:44
void add(const char *name, const char *value)
Add a name-value pair to the resource list.
Definition: resource.cpp:169
const char * getValue(const char *name) const
Find a named parameter.
Definition: resource.h:119
resource & operator=(const resource &rhs)
The assignment operator.
Definition: resource.cpp:444
char * strnewdup(const char *s)
Duplicate string content with C++ default new operator.
Definition: util.cpp:1420
A container for name-value pairs.
Definition: resource.h:38
resource(const resource *ctx, const char *pfx)
Create an empty object with the specified prefix and context.
Definition: resource.h:52
The current implementation of FastBit is code named IBIS; most data structures and functions are in t...
Definition: bord.h:16
resource(const char *fn)
Read the content of a parameter file.
Definition: resource.h:50
bool isTrue(const char *name) const
Locate the named parameter and return its value as true or false.
Definition: resource.cpp:290
void write(const char *fn=0) const
Write the content to a file.
Definition: resource.cpp:513
bool empty() const
Returns true if there is no name-value pair on record.
Definition: resource.h:72
double getNumber(const char *name) const
Locate the named parameter and return its value as a number.
Definition: resource.cpp:273
Defines minor utility functions and common classes used by FastBit.
std::map< const char *, resource *, ibis::lessi > gList
The name-value pairs are categorized into two types, names that map to simple values (vList) and name...
Definition: resource.h:43
static bool isStringTrue(const char *val)
Returns true is the string value should be interpreted as logical truth.
Definition: resource.h:151
static void parseNameValuePairs(const char *in, vList &lst)
Parse a string into a name-value list.
Definition: resource.cpp:347
void clear(ibis::array_t< ibis::bitvector * > &bv)
Clear an array of bit vectors.
Definition: bitvector.cpp:4662
const char * delimiters
Delimiters used to separate a string of names.
Definition: util.cpp:71
resource()
Default constructor. Creates an empty object.
Definition: resource.h:48
std::string getPrefix() const
Return the name of the full prefix of the resource.
Definition: resource.h:131
const resource * getGroup(const char *name) const
Find a group with the given name.
Definition: resource.h:107
resource(const resource &rhs)
Copy constructor. Deep copy.
Definition: resource.h:55

Make It A Bit Faster
Contact us
Disclaimers
FastBit source code
FastBit mailing list archive