From: Christian P. <cp...@us...> - 2005-04-23 15:19:44
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20134/src/System Modified Files: PathFinder.cpp Log Message: - Readability and some code cleanup - Removed variable pathSeparator cause it's system specific Index: PathFinder.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/PathFinder.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- PathFinder.cpp 25 Dec 2004 20:48:13 -0000 1.3 +++ PathFinder.cpp 23 Apr 2005 15:19:32 -0000 1.4 @@ -1,7 +1,9 @@ // Author: stephan beal <st...@s1...> // License: Public Domain +// Readability cleanup and code P-ificaton by Christian Prochnow <cp...@se...> #include <iostream> +#include <sstream> #include <stdlib.h> // getenv() @@ -21,139 +23,130 @@ #define CERR std::cerr << __FILE__ << ":" << std::dec << __LINE__ << " : " #endif - - using std::string; using std::ostream; -namespace P { namespace System { +namespace P { +namespace System { - PathFinder::~PathFinder() - { - } - PathFinder::PathFinder( const string & p, const string & e, const string & pathsep ) - { - this->pathSeparator( pathsep ); - this->path( p ); - this->extensions( e ); - } +PathFinder::~PathFinder() +{ +} +PathFinder::PathFinder(const string & p, const string & e) +{ + this->path( p ); + this->extensions( e ); +} - string PathFinder::pathSeparator() const - { - return this->pathseparator; - } +/** + Internal helper function to collapse a list into a string. + cproch: Modified code to use a ostringstream. Appending to strings is really slow. +*/ +template <typename StringList> +std::string joinList( const StringList & list, const std::string & separator ) +{ + std::ostringstream ret; + unsigned long count = list.size(); + unsigned long at = 0; - void PathFinder::pathSeparator( const string & sep ) + typename StringList::const_iterator it = list.begin(); + while(it != list.end()) { - this->pathseparator = sep; + ret << (*it); + ++it; + if(it != list.end()) + ret << separator; } - /** - Internal helper function to collapse a list into a string. - */ - template <typename StringList> - std::string joinList( const StringList & list, const std::string & separator ) - { - std::string ret; - unsigned long count = list.size(); - unsigned long at = 0; - typename StringList::const_iterator it = list.begin(); - typename StringList::const_iterator et = list.end(); - for(; it != et; ++it ) - { - - ret += (*it); - if( ++at != count ) ret += separator; - } - return ret; - } - - string PathFinder::pathString() const - { - return joinList( this->paths, this->pathseparator ); - } + return ret.str(); +} - const PathFinder::string_list & PathFinder::path() const - { - return this->paths; - } +string PathFinder::pathString() const +{ + return joinList( this->paths, pathSeparator() ); +} - string PathFinder::extensionsString() const - { - return joinList( this->exts, this->pathseparator ); - } +const PathFinder::string_list & PathFinder::path() const +{ + return this->paths; +} - const PathFinder::string_list & PathFinder::extensions() const - { - return this->exts; - } +string PathFinder::extensionsString() const +{ + return joinList( this->exts, pathSeparator()); +} +const PathFinder::string_list & PathFinder::extensions() const +{ + return this->exts; +} - size_t tokenize_to_list( const std::string & str, std::list<std::string> & li, const std::string & sep ) - { // internal helper function - if( str.empty() ) return 0; +size_t tokenize_to_list( const std::string & str, std::list<std::string> & li, const std::string & sep ) +{ // internal helper function + if( str.empty() ) return 0; - size_t c = 0; + size_t c = 0; - std::string token; - std::string::size_type sz = str.size(); - for( std::string::size_type i = 0; i < sz; i++ ) - { - if( sz-1 == i ) token += str[i]; - if( str.find( sep, i ) == i || (sz-1 == i) ) - { - //CERR << "token="<<token<<std::endl; - li.push_back( token ); - token = ""; - i += sep.size() - 1; - continue; - } - token += str[i]; - } - return c; - } + std::string token; + std::string::size_type sz = str.size(); - size_t PathFinder::path( const string & p ) + for( std::string::size_type i = 0; i < sz; i++ ) { - this->paths.erase( this->paths.begin(), this->paths.end() ); - return tokenize_to_list( p, this->paths, this->pathseparator ); + if( sz-1 == i ) token += str[i]; + if( str.find( sep, i ) == i || (sz-1 == i) ) + { + //CERR << "token="<<token<<std::endl; + li.push_back( token ); + token = ""; + i += sep.size() - 1; + continue; + } + token += str[i]; } - size_t PathFinder::path( const PathFinder::string_list & p ) - { - this->paths = p; - return this->paths.size(); - } + return c; +} - void PathFinder::addPath( const string & p ) - { - tokenize_to_list( p, this->paths, this->pathseparator ); - } +size_t PathFinder::path( const string & p ) +{ + this->paths.erase( this->paths.begin(), this->paths.end() ); + return tokenize_to_list( p, this->paths, pathSeparator() ); +} +size_t PathFinder::path( const PathFinder::string_list & p ) +{ + this->paths = p; + return this->paths.size(); +} - size_t PathFinder::extensions( const string & p ) - { - this->exts.erase( this->exts.begin(), this->exts.end() ); - return tokenize_to_list( p, this->exts, this->pathseparator ); - } +void PathFinder::addPath( const string & p ) +{ + tokenize_to_list( p, this->paths, pathSeparator() ); +} - size_t PathFinder::extensions( const PathFinder::string_list & e ) - { - this->exts = e; - return this->exts.size(); - } +size_t PathFinder::extensions( const string & p ) +{ + this->exts.erase( this->exts.begin(), this->exts.end() ); + return tokenize_to_list( p, this->exts, pathSeparator() ); +} - void PathFinder::addExtension( const string & p ) - { - tokenize_to_list( p, this->exts, this->pathseparator ); - } +size_t PathFinder::extensions( const PathFinder::string_list & e ) +{ + this->exts = e; + return this->exts.size(); +} - // static - bool PathFinder::isAccessible( const string & path ) - { +void PathFinder::addExtension( const string & p ) +{ + tokenize_to_list( p, this->exts, pathSeparator() ); +} + +// static +bool PathFinder::isAccessible( const string & path ) +{ #if WIN32 # define CHECKACCESS _access # define CHECKRIGHTS 0 @@ -162,91 +155,105 @@ # define CHECKRIGHTS F_OK #endif - return 0 == CHECKACCESS( path.c_str(), CHECKRIGHTS ); + return 0 == CHECKACCESS( path.c_str(), CHECKRIGHTS ); #undef CHECKACCESS #undef CHECKRIGHTS - } +} - string PathFinder::basename( const std::string & name ) - { - string::size_type slashat = name.find_last_of( PathFinder::dirSeparator() ); - if ( slashat == string::npos ) - return name; - return name.substr( slashat + 1 ); - } +string PathFinder::basename( const std::string & name ) +{ + string::size_type slashat = name.find_last_of( PathFinder::dirSeparator() ); + if ( slashat == string::npos ) + return name; + return name.substr( slashat + 1 ); +} +std::string PathFinder::dirSeparator() +{ +#ifdef WIN32 + return std::string( "\\" ); +#else + return std::string( "/" ); +#endif +} - std::string PathFinder::dirSeparator() - { -#if WIN32 - return std::string( "\\" ); +std::string PathFinder::pathSeparator() +{ +#ifdef WIN32 + return std::string( ";" ); #else - return std::string( "/" ); + return std::string( ":" ); #endif - } +} - string PathFinder::find( const string & resource, bool check_cache ) const - { - //static const std::string NOT_FOUND = "PathFinder::find() : no findie"; - if( resource.empty() ) return resource; +string PathFinder::find( const string & resource, bool check_cache ) const +{ + //static const std::string NOT_FOUND = "PathFinder::find() : no findie"; + if( resource.empty() ) return resource; #define CHECKPATH(CHECKAT) \ - if( ! CHECKAT.empty() && PathFinder::isAccessible( CHECKAT ) ) \ - { this->hitcache[resource] = CHECKAT; return CHECKAT; } + if( ! CHECKAT.empty() && PathFinder::isAccessible( CHECKAT ) ) \ + { this->hitcache[resource] = CHECKAT; return CHECKAT; } - //CERR << "find( " << resource << " )" << std::endl; - if( check_cache ) - { - std::map <std::string,std::string>::iterator mapiter; - mapiter = this->hitcache.find( resource ); - if( this->hitcache.end() != mapiter ) return (*mapiter).second; - } + //CERR << "find( " << resource << " )" << std::endl; + if(check_cache) + { + std::map <std::string,std::string>::iterator mapiter; + mapiter = this->hitcache.find( resource ); + if( this->hitcache.end() != mapiter ) return (*mapiter).second; + } - CHECKPATH( resource ); + CHECKPATH( resource ); - string_list::const_iterator piter = this->paths.begin(); - string_list::const_iterator eiter = this->exts.begin(); + string_list::const_iterator piter = this->paths.begin(); + string_list::const_iterator eiter = this->exts.begin(); - string path; - string ext; + string path; + string ext; - if ( PathFinder::isAccessible( resource ) ) - return resource; + if ( PathFinder::isAccessible( resource ) ) + return resource; - piter = this->paths.begin(); - string checkhere; - while ( piter != this->paths.end() ) + piter = this->paths.begin(); + string checkhere; + + while ( piter != this->paths.end() ) + { + path = ( *piter ); + if ( !path.empty() ) { - path = ( *piter ); - if ( !path.empty() ) - { - path += PathFinder::dirSeparator(); - } - ++piter; - checkhere = path + resource; + path += PathFinder::dirSeparator(); + } + + ++piter; + checkhere = path + resource; + + //CERR << "find( " << resource << " ) checking " << checkhere << std::endl; + CHECKPATH( checkhere ); + + eiter = this->exts.begin(); + while ( eiter != this->exts.end() ) + { + ext = ( *eiter ); + ++eiter; + checkhere = path + resource + ext; //CERR << "find( " << resource << " ) checking " << checkhere << std::endl; CHECKPATH( checkhere ); - eiter = this->exts.begin(); - while ( eiter != this->exts.end() ) - { - ext = ( *eiter ); - ++eiter; - checkhere = path + resource + ext; - //CERR << "find( " << resource << " ) checking " << checkhere << std::endl; - CHECKPATH( checkhere ); - } } - //CERR << "find( "<<resource<<" ): not found :(" << std::endl; - // so arguable: - // this->hitcache[resource] = ""; - return string(); } - void PathFinder::clearCache() - { - this->hitcache.clear(); - } + //CERR << "find( "<<resource<<" ): not found :(" << std::endl; + // so arguable: + // this->hitcache[resource] = ""; + return string(); +} + +void PathFinder::clearCache() +{ + this->hitcache.clear(); +} + // /** // bin_PathFinder is a PathFinder which uses the environment's PATH by default. // */ @@ -269,4 +276,6 @@ // } -}} // namespaces +} // !namespace System + +} // !namespace P |