// Path.cxx #include "dataset_util/Path.h" #include #include #include "dataset_util/FileStatus.h" using std::string; using std::ostream; //********************************************************************** // Member functions. //********************************************************************** // Constructor. Path::Path(string value) { get(value); } //********************************************************************** // Get. void Path::get(string senv) { // Clear the original contents. erase(begin(), end()); // Extract the path. string::size_type pos = 0; while ( pos != string::npos ) { // Skip colons. pos = senv.find_first_not_of(':', pos); if ( pos == string::npos ) break; // Find the next colon. string::size_type endpos = senv.find(':', pos); if ( endpos == string::npos ) { endpos = senv.size(); } string name = senv.substr(pos,endpos-pos); assert( name.size() > 0 ); push_back(name); pos = endpos + 1; } } //********************************************************************** // Get from environment. void Path::getenv(string env) { // Fetch the value from the environment. char* pcenv = ::getenv(env.c_str()); if ( pcenv == 0 ) return; string senv = pcenv; get(senv); } //********************************************************************** // Set. void Path::setenv(string env) { string senv = ""; for ( const_iterator inam=begin(); inam!=end(); ++inam ) { if ( inam != begin() ) senv += ":"; senv += *inam; } ::setenv(env.c_str(), senv.c_str(), 1); } //********************************************************************** // Find. Path Path::find(string filename) { Path pth; if ( filename.size() == 0 ) return pth; string senv = ""; for ( const_iterator idir=begin(); idir!=end(); ++idir ) { string dir = *idir; while ( dir.size()>0 && dir[dir.size()-1]=='/' ) { dir = dir.substr(0, dir.size()-1); } if ( dir.size() == 0 ) continue; string fullname = dir + "/" + filename; FileStatus fstat(fullname); if ( fstat.exists() ) pth.push_back(*idir); } return pth; } //********************************************************************** // Write as string. string Path::to_string() const { string sout; for ( Path::const_iterator inam=begin(); inam!=end(); ++inam ) { if ( inam != begin() ) sout += ":"; sout += *inam; } return sout; } //********************************************************************** // Free functions. //********************************************************************** // Output stream. ostream& operator<<(ostream& lhs, const Path& rhs) { return print(lhs, rhs, " "); return lhs; } //********************************************************************** // Formatted output stream. ostream& print(ostream& lhs, const Path& rhs, string indent) { Path::size_type nnam = rhs.size(); lhs << "Path has " << nnam << " entr"; if ( nnam == 1 ) { lhs << "y"; } else { lhs << "ies"; } if ( nnam > 0 ) lhs << ":"; for ( Path::const_iterator inam=rhs.begin(); inam!=rhs.end(); ++inam ) { lhs << '\n' << indent << *inam; } return lhs; } //**********************************************************************