CMS 3D CMS Logo

IgConfigElement Class Reference

#include <Iguana/Studio/interface/IgConfigElement.h>

Inheritance diagram for IgConfigElement:

IgExtension IgStateElement

List of all members.

Public Types

typedef std::map< std::string,
std::string > 
KeyMap

Public Member Functions

const KeyMapgetKeyMap () const
bool getValue (const std::string &key, std::string &value) const
template<class T>
bool getValue (const std::string &key, T &value) const
bool getValues (const std::string &key, std::vector< std::string > &values) const
template<class T>
bool getValues (const std::string &key, std::vector< T > &values) const
 IgConfigElement (IgState *state)
const std::string & prefix (void) const
void putValue (const std::string &key, const std::string &value, bool override=true)
template<class T>
void putValue (const std::string &key, const T &value, bool override=true)
void putValues (const std::string &key, const std::vector< std::string > &values, bool override=true)
template<class T>
void putValues (const std::string &key, const std::vector< T > &values, bool override=true)
void save (const std::string &filename="")
void setPrefix (const std::string &prefix)
 ~IgConfigElement ()

Private Member Functions

template<class T>
std::string convert (const T &value) const
void convert (const std::string &str, bool &value) const
template<class T>
void convert (const std::string &str, T &value) const
 IG_DECLARE_STATE_ELEMENT (IgConfigElement)
void initConfiguration ()
void processNode (const QDomNode &node, const std::string &path)
const std::string & substituteValue (std::string &value) const

Private Attributes

KeyMap m_config
bool m_dirty
std::string m_file
std::string m_prefix
IgStatem_state


Detailed Description

Definition at line 23 of file IgConfigElement.h.


Member Typedef Documentation

typedef std::map<std::string, std::string> IgConfigElement::KeyMap

Definition at line 27 of file IgConfigElement.h.


Constructor & Destructor Documentation

IgConfigElement::IgConfigElement ( IgState state  ) 

Definition at line 49 of file IgConfigElement.cc.

References ASSERT, initConfiguration(), m_state, and IgState::put().

00050     : m_state (state),
00051       m_prefix (""),
00052       m_file (""),
00053       m_dirty (true)
00054 {
00055     ASSERT (m_state);
00056     m_state->put (s_key, this);
00057     
00058     initConfiguration ();
00059 }

IgConfigElement::~IgConfigElement (  ) 

Definition at line 61 of file IgConfigElement.cc.

References IgState::detach(), and m_state.

00062 { m_state->detach (s_key); }


Member Function Documentation

template<class T>
std::string IgConfigElement::convert ( const T &  value  )  const [inline, private]

Definition at line 92 of file IgConfigElement.h.

References p.

00093 {
00094     std::string converted;
00095     std::stringstream ios;
00096     int p = ios.precision();
00097     ios.setf(std::ios::fixed);
00098     ios.precision(20);
00099     ios << value;
00100     ios >> converted;
00101     ios.precision(p);
00102     return converted;
00103 }

void IgConfigElement::convert ( const std::string &  str,
bool value 
) const [private]

Definition at line 311 of file IgConfigElement.cc.

00312 {
00313     if (str.size () > 1)
00314     {
00315         if ((str == "true") || (str == "TRUE") || (str == "True"))
00316             value = true;
00317         else
00318             value = false;
00319     }
00320     else if ((str == "t") || (str == "T") || (str == "1"))
00321         value = true;
00322     else
00323         value = false;
00324 }

template<class T>
void IgConfigElement::convert ( const std::string &  str,
T &  value 
) const [inline, private]

Definition at line 80 of file IgConfigElement.h.

References p.

Referenced by getValue(), getValues(), putValue(), and putValues().

00081 {
00082     std::stringstream ios;
00083     int p = ios.precision();
00084     ios.setf(std::ios::fixed);
00085     ios.precision(20);
00086     ios << str.c_str();
00087     ios >> value;
00088     ios.precision(p);
00089 }

const IgConfigElement::KeyMap & IgConfigElement::getKeyMap (  )  const

Definition at line 64 of file IgConfigElement.cc.

References m_config.

00065 { return m_config; }

bool IgConfigElement::getValue ( const std::string &  key,
std::string &  value 
) const

Definition at line 223 of file IgConfigElement.cc.

References m_config, m_prefix, and substituteValue().

00224 {
00225     std::string fullkey = m_prefix + "/" + key;
00226     int sindex = -1;
00227     while ((sindex = fullkey.find ("//")) != -1)
00228         fullkey = fullkey.replace (sindex, 2, "/");
00229     std::map<std::string, std::string>::const_iterator itr = 
00230         m_config.find (fullkey);
00231     if (itr != m_config.end ())
00232     {
00233         value = itr->second;
00234         substituteValue (value);
00235         return true;
00236     }
00237     return false;
00238 }

template<class T>
bool IgConfigElement::getValue ( const std::string &  key,
T &  value 
) const [inline]

Definition at line 107 of file IgConfigElement.h.

References convert().

Referenced by getValues(), putValues(), IgStudioDriver::run(), IgStudioDriver::setupMainWindow(), and substituteValue().

00108 {
00109     std::string val;
00110     if (getValue(key, val))
00111     {
00112         convert (val, value);
00113         return true;
00114     }
00115     return false;
00116 }

bool IgConfigElement::getValues ( const std::string &  key,
std::vector< std::string > &  values 
) const

Definition at line 241 of file IgConfigElement.cc.

References getValue(), i, and size.

00243 {
00244     std::string val;
00245     if (getValue (key, val))
00246     {
00247         std::string item ("");
00248         std::string separator (",");
00249         getValue ("separator", separator);
00250         size_t size = val.size ();
00251         for (size_t i = 0; i < size; i++)
00252         {
00253             if (val [i] == separator [0])
00254             {
00255                 while (*item.begin () == ' ')
00256                     item.erase (item.begin ());
00257                 while (*(--item.end ()) == ' ')
00258                     item.erase (--item.end ());
00259                 values.push_back (item);
00260                 item = "";
00261             }
00262             else
00263                 item.push_back (val [i]);
00264         }
00265     
00266         while (*item.begin () == ' ')
00267             item.erase (item.begin ());
00268         while (*(--item.end ()) == ' ')
00269             item.erase (--item.end ());
00270         values.push_back (item);
00271         return true;
00272     }
00273     return false;
00274 }

template<class T>
bool IgConfigElement::getValues ( const std::string &  key,
std::vector< T > &  values 
) const [inline]

Definition at line 120 of file IgConfigElement.h.

References convert(), and i.

00122 {
00123     std::vector<std::string> val;
00124     if (getValues(key, val))
00125     {
00126         T val1;
00127         for(size_t i=0; i<val.size(); i++)
00128         {
00129             convert(val[i], val1);
00130             values.push_back(val1);
00131         }
00132         return true;
00133     }
00134     return false;
00135 }

IgConfigElement::IG_DECLARE_STATE_ELEMENT ( IgConfigElement   )  [private]

void IgConfigElement::initConfiguration (  )  [private]

Definition at line 68 of file IgConfigElement.cc.

References ASSERT, lat::File::close(), dir, doc, lat::Filename::exists(), f, file, IgArgsElement::find(), DBSPlugin::get(), lat::UserInfo::homedir(), lat::UserInfo::id(), lat::Filename::isDirectory(), lat::Filename::isExecutable(), lat::Filename::isReadable(), lat::Filename::isWritable(), m_dirty, m_file, m_state, lat::Filename::makedir(), lat::UserInfo::name(), lat::File::open(), lat::IOFlags::OpenCreate, lat::IOFlags::OpenWrite, processNode(), putValue(), lat::UserInfo::self(), lat::UserInfo::shell(), lat::UserInfo::uid(), and cmsBenchmark::user.

Referenced by IgConfigElement().

00069 {
00070     IgArgsElement *args = IgArgsElement::get (m_state);
00071     ASSERT (args);
00072     
00073     lat::UserInfo *user = lat::UserInfo::self ();
00074     if (args->find ("--ig-config", m_file, true))
00075     {
00076         lat::Filename f (m_file);
00077         if (f.isDirectory () || !f.isReadable ())
00078         {
00079             ASSERT ("ERROR: IGUANA configuration file does not exist or not a regular file" == 0);
00080             m_file = "";
00081         }
00082     }
00083     else
00084     {
00085         std::string home (user->homedir ());
00086         m_file = home + "/.iguana";
00087         lat::Filename dir (m_file);
00088         if(!dir.isDirectory ())
00089             dir.makedir (m_file, 0755, false, true);
00090         else if (!dir.isWritable () || !dir.isExecutable ())
00091             ASSERT ("ERROR: IGUANA configuration directory ~/.iguana is not writable. Please make sure that directory permission are 0755 and if it exists on an AFS area then you should have a valid AFS token." == 0);
00092 
00093         m_file = m_file + "/iguana.conf";
00094         lat::Filename file (m_file);
00095         if (!file.exists ())
00096         {
00097             lat::File f;
00098             f.open (m_file, lat::IOFlags::OpenWrite | lat::IOFlags::OpenCreate);
00099             f.close();
00100         }
00101     }
00102     putValue ("/IgConfigElementInternal/PID",           getpid ());
00103     putValue ("/IgConfigElementInternal/PPID",          getppid ());
00104     putValue ("/IgConfigElementInternal/UID",           user->uid ());
00105     putValue ("/IgConfigElementInternal/GID",           user->uid ());
00106     putValue ("/IgConfigElementInternal/ID",            user->id ());
00107     putValue ("/IgConfigElementInternal/USER",          user->name ());
00108     putValue ("/IgConfigElementInternal/HOMEDIR",       user->homedir ());
00109     putValue ("/IgConfigElementInternal/SHELL",         user->shell ());
00110     
00111     if (!m_file.empty ())
00112     {
00113         QDomDocument doc ("IGUANA_CONFIG");
00114         std::ifstream file;
00115         file.open (m_file.c_str ());
00116         ASSERT (file.good ());
00117         
00118         QBuffer buffer;
00119         buffer.open (IO_ReadWrite);
00120         buffer.writeBlock ("<C>", 3);
00121         
00122         char ch;
00123         file.get (ch);
00124         while (!file.eof ())
00125         {
00126             buffer.writeBlock (&ch, 1);
00127             file.get (ch);
00128         }
00129         
00130         buffer.writeBlock ("</C>", 4);
00131         file.close ();
00132         buffer.reset ();
00133         
00134         if (!doc.setContent (&buffer))
00135             ASSERT ("ERROR: Reading IGUANA configuration file" == 0);
00136         else
00137             processNode (doc.firstChild (), "");
00138     }
00139     m_dirty = false;
00140 }

const std::string & IgConfigElement::prefix ( void   )  const

Definition at line 192 of file IgConfigElement.cc.

References m_prefix.

00193 { return m_prefix; }

void IgConfigElement::processNode ( const QDomNode &  node,
const std::string &  path 
) [private]

Definition at line 143 of file IgConfigElement.cc.

References putValue(), and value.

Referenced by initConfiguration().

00144 {
00145     if (node.isElement ())
00146     {
00147         if (node.nodeName () == "IgConfigElementInternal") return;
00148         std::string npath = path + "/" + node.nodeName ().latin1 ();
00149         if(node.hasChildNodes ())
00150         {
00151             QDomNode child = node.firstChild ();
00152             while (!child.isNull ())
00153             {
00154                 processNode (child, npath);
00155                 child = child.nextSibling ();
00156             }
00157         }
00158         else
00159             putValue (npath.substr (2), "", true);
00160     }
00161     else if (node.isText ())
00162     {
00163         std::string value (node.nodeValue ().latin1 ());
00164         std::string::iterator chr = value.begin ();
00165 
00166         while (chr != value.end ())
00167         {
00168             if (*chr == '\n') value.erase (chr);
00169             else chr++;
00170         }
00171 
00172         while (*value.begin () == ' ')
00173             value.erase (value.begin ());
00174 
00175         while (*(--value.end ()) == ' ')
00176             value.erase (--value.end ());
00177 
00178         putValue (path.substr (2), value, true);
00179     }
00180 }

void IgConfigElement::putValue ( const std::string &  key,
const std::string &  value,
bool  override = true 
)

Definition at line 277 of file IgConfigElement.cc.

References m_config, m_dirty, and m_prefix.

00280 {
00281     std::string fullkey = m_prefix + "/" + key;
00282     int sindex = -1;
00283     while ((sindex = fullkey.find ("//")) != -1)
00284         fullkey = fullkey.replace (sindex, 2, "/");
00285 
00286     if (override || (m_config.find (fullkey) == m_config.end ()))
00287     {
00288         m_config [fullkey] = value;
00289         m_dirty = true;
00290     }
00291 }

template<class T>
void IgConfigElement::putValue ( const std::string &  key,
const T &  value,
bool  override = true 
) [inline]

Definition at line 139 of file IgConfigElement.h.

References convert().

Referenced by initConfiguration(), processNode(), and putValues().

00142 { putValue (key, convert(value), override); }

void IgConfigElement::putValues ( const std::string &  key,
const std::vector< std::string > &  values,
bool  override = true 
)

Definition at line 294 of file IgConfigElement.cc.

References getValue(), i, and putValue().

00297 {
00298     std::string nvalue ("");
00299     if (values.size () > 0)
00300     {
00301         nvalue = values [0];
00302         std::string separator (",");
00303         getValue ("separator", separator);
00304         for (size_t i = 1; i < values.size (); i++)
00305             nvalue += separator + values [i];
00306     }
00307     putValue (key, nvalue, override);
00308 }

template<class T>
void IgConfigElement::putValues ( const std::string &  key,
const std::vector< T > &  values,
bool  override = true 
) [inline]

Definition at line 146 of file IgConfigElement.h.

References convert(), and i.

00149 {
00150     std::vector<std::string> vec;
00151     for(size_t i=0; i<values.size(); i++)
00152         vec.push_back(convert(values[i]));
00153     putValues (key, vec, override);
00154 }

void IgConfigElement::save ( const std::string &  filename = ""  ) 

Definition at line 327 of file IgConfigElement.cc.

References ASSERT, IgConfigElementNode::childs, file, IgConfigElementNode::find(), IgConfigElementNode::hasValue, i, getDQMSummary::key, m_config, m_dirty, m_file, IgConfigElementNode::name, out, IgConfigElementNode::print(), size, and IgConfigElementNode::value.

00328 {
00329     std::string file (filename);
00330     if (filename.empty ())
00331         file = m_file;
00332     if (!file.empty ())
00333     {
00334         if ((file == m_file) && !m_dirty) return;
00335         IgConfigElementNode head;
00336         head.name = "IgConfigElementHeadNodeDummy";
00337         std::map<std::string, std::string>::const_iterator itr = 
00338             m_config.begin ();
00339         while (itr != m_config.end ())
00340         {
00341             if (itr->first.find ("/IgConfigElementInternal/") == 0)
00342             {
00343                 itr++;
00344                 continue;
00345             }
00346             IgConfigElementNode* cur = &head;
00347             std::string key = itr->first;
00348             std::string part ("");
00349             size_t size = key.size ();
00350             for (size_t i = 0; i < size; i++)
00351             {
00352                 char ch = key [i];
00353                 if (ch == '/')
00354                 {
00355                     if (part.empty ()) continue;
00356                     cur = IgConfigElementNode::find (cur, part);
00357                     part = "";
00358                 }
00359                 else
00360                     part.push_back (ch);
00361             }
00362             if (!part.empty ())
00363                 cur = IgConfigElementNode::find (cur, part);
00364             cur->value = itr->second;
00365             cur->hasValue = true;
00366             itr++;
00367         }
00368         
00369         std::ofstream out;
00370         out.open (file.c_str ());
00371         ASSERT (out.good ());
00372         for (size_t i = 0; i < head.childs.size (); i++)
00373             IgConfigElementNode::print (&head.childs [i], out);
00374         out.close ();
00375     }
00376 }

void IgConfigElement::setPrefix ( const std::string &  prefix  ) 

Definition at line 183 of file IgConfigElement.cc.

References index, and m_prefix.

Referenced by IgStudioDriver::setupMainWindow().

00184 {
00185     m_prefix = "/" + prefix;
00186     int index = -1;
00187     while ((index = m_prefix.find ("//")) != -1)
00188         m_prefix = m_prefix.replace (index, 2, "/");
00189 }

const std::string & IgConfigElement::substituteValue ( std::string &  value  )  const [private]

Definition at line 196 of file IgConfigElement.cc.

References DBSPlugin::get(), IgEnvsElement::getEnv(), getValue(), and m_state.

Referenced by getValue().

00197 {
00198     IgEnvsElement* envs = IgEnvsElement::get (m_state);
00199     int spos = 0;
00200     while ((spos = value.rfind ("$", spos - 1)) != -1)
00201     {
00202         if (((int)value.size () > spos + 1) && (value[spos + 1] == '{'))
00203         {
00204             int epos = value.find ("}", spos);
00205             if (epos != -1)
00206             {
00207                 std::string nval ("");
00208                 if (epos > spos + 2)
00209                 {
00210                     std::string var (value.substr (spos + 2, epos - spos - 2));
00211                     if (!getValue (var, nval) && !getValue ("/IgConfigElementInternal/" + var, nval))
00212                         envs->getEnv (var, nval);
00213                 }
00214                 value = value.substr (0, spos) + nval + value.substr (epos + 1);
00215             }
00216         }
00217         if (spos == 0) break;
00218     }
00219     return value;
00220 }


Member Data Documentation

KeyMap IgConfigElement::m_config [private]

Definition at line 73 of file IgConfigElement.h.

Referenced by getKeyMap(), getValue(), putValue(), and save().

bool IgConfigElement::m_dirty [private]

Definition at line 76 of file IgConfigElement.h.

Referenced by initConfiguration(), putValue(), and save().

std::string IgConfigElement::m_file [private]

Definition at line 75 of file IgConfigElement.h.

Referenced by initConfiguration(), and save().

std::string IgConfigElement::m_prefix [private]

Definition at line 74 of file IgConfigElement.h.

Referenced by getValue(), prefix(), putValue(), and setPrefix().

IgState* IgConfigElement::m_state [private]

Definition at line 72 of file IgConfigElement.h.

Referenced by IgConfigElement(), initConfiguration(), substituteValue(), and ~IgConfigElement().


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:25:08 2009 for CMSSW by  doxygen 1.5.4