// Environment.cxx #include "dataset_util/Environment.h" #include #include "dataset_util/XmlElement.h" #include "dataset_util/Path.h" #include "dataset_util/Text.h" using std::string; using std::vector; using std::ostream; using std::endl; extern const char** environ; //********************************************************************** // Static member functions. //********************************************************************** // Return the initial system environment. const Environment& Environment::initial() { static Environment env; static bool init = false; if ( ! init ) { env.sysfill(); init = true; } return env; } //********************************************************************** // Return the current system environment. Environment Environment::current() { Environment env; env.sysfill(); return env; } //********************************************************************** // Return the posix environment. const Environment& Environment::posix() { static Environment env; static bool init = false; if ( ! init ) { // RHE3 requires this to export PATH. env["PATH"] = "/usr/bin:/bin"; } return env; } //********************************************************************** // DTD. const Text& Environment::dtd() { static Text txt; if ( txt.size() == 0 ) { txt.append(""); txt.append(""); txt.append(""); txt.append(""); } return txt; } //********************************************************************** // Member functions. //********************************************************************** // Default constructor. Environment::Environment() { } //********************************************************************** // XML constructor. Environment::Environment(const XmlElement& ele) { if ( ele.name() != xml_name() ) return; XmlElement::ElementList xvars = ele.children("EnvironmentalVariable"); for ( XmlElement::ElementList::const_iterator ixvar=xvars.begin(); ixvar!=xvars.end(); ++ixvar ) { XmlElement xvar = **ixvar; string name = xvar.attribute("name"); string value = xvar.attribute("value"); if ( name.size() > 0 ) { insert(make_pair(name, value)); } } } //********************************************************************** // Fill from character array. void Environment::fill(const char *const * lines) { erase( begin(), end() ); for ( int i=0; i<1000000; ++i ) { const char* cline = lines[i]; if ( cline == 0 ) break; string line(cline); string::size_type epos = line.find('='); assert( epos != string::npos ); if ( epos == string::npos ) continue; string name = line.substr(0,epos); string value = line.substr(epos+1, line.size()-epos-1); assert( find(name) == end() ); insert(make_pair(name, value)); } } //********************************************************************** // Fill from system table. void Environment::sysfill() { assert( environ != 0 ); fill(environ); } //********************************************************************** // Return if a value is defined. bool Environment::has(string name) const { const_iterator ival = find(name); return ival != end(); } //********************************************************************** // Return a value. string Environment::value(string name) const { const_iterator ival = find(name); if ( ival == end() ) return ""; return ival->second; } //********************************************************************** // Return table. const char *const * Environment::env() const { vector lines; vector clines; for ( const_iterator ival=begin(); ival!=end(); ++ival ) { string name = ival->first; string value = ival->second; string line = name + "=" + value + '\0'; lines.push_back(line); } for ( vector::const_iterator ilin=lines.begin(); ilin!=lines.end(); ++ilin ) { const string& line = *ilin; const char* cline = &line.at(0); clines.push_back(cline); } assert( lines.size() == clines.size() ); clines.push_back(0); m_lines.push_back(lines); m_clines.push_back(clines); return &m_clines.back().front(); } //********************************************************************** // Return unsafe table. char *const * Environment::unsafe_env() const { return const_cast(env()); } //********************************************************************** // Write to XML. const XmlElement* Environment::xml() const { XmlElement* pele = new XmlElement(xml_name()); for ( const_iterator ival=begin(); ival!=end(); ++ival ) { string name = ival->first; string value = ival->second; XmlElement* pxvar = new XmlElement("EnvironmentalVariable"); pxvar->add_attribute("name", name); pxvar->add_attribute("value", value); pele->add_child(pxvar); } return pele; } //********************************************************************** // Free functions. //********************************************************************** // Output stream. ostream& operator<<(ostream& lhs, const Environment& rhs) { Environment::size_type nval = rhs.size(); lhs << "Environment has " << nval << " entr"; if ( nval == 1 ) { lhs << "y"; } else { lhs << "ies"; } if ( nval > 0 ) lhs << ":"; for ( Environment::const_iterator ival=rhs.begin(); ival!=rhs.end(); ++ival ) { string name = ival->first; string value = ival->second; lhs << "\n " << name << " = "; if ( name.find("PATH") != string::npos ) { Path pth(value); print(lhs, pth, " "); } else { lhs << value; } } return lhs; } //**********************************************************************