From: Christian P. <cp...@us...> - 2005-04-28 10:21:53
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29121/src/System Modified Files: PathFinder.cpp Log Message: - Use StringList implementation Index: PathFinder.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/PathFinder.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- PathFinder.cpp 25 Apr 2005 10:57:31 -0000 1.5 +++ PathFinder.cpp 28 Apr 2005 10:21:44 -0000 1.6 @@ -14,136 +14,85 @@ # include <unistd.h> #endif +#include "pclasses/Trace.h" #include "pclasses/System/PathFinder.h" -// CERR is a drop-in replacement for std::cerr, but slightly more -// decorative. - -#ifndef CERR -#define CERR std::cerr << __FILE__ << ":" << std::dec << __LINE__ << " : " -#endif - -using std::string; -using std::ostream; - namespace P { namespace System { - -PathFinder::~PathFinder() +PathFinder::PathFinder() { } -PathFinder::PathFinder(const string & p, const string & e) +PathFinder::PathFinder(const Unicode::String& p, const Unicode::String& e) { - this->path( p ); - this->extensions( e ); + path(p); + extensions(e); } -/** - 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 ) +PathFinder::~PathFinder() { - std::ostringstream ret; - - typename StringList::const_iterator it = list.begin(); - while(it != list.end()) - { - ret << (*it); - ++it; - if(it != list.end()) - ret << separator; - } - - return ret.str(); } -string PathFinder::pathString() const +Unicode::String PathFinder::pathString() const { - return joinList( this->paths, pathSeparator() ); + return _paths.join(pathSeparator()); } -const PathFinder::string_list & PathFinder::path() const +const PathFinder::string_list& PathFinder::path() const { - return this->paths; + return _paths; } -string PathFinder::extensionsString() const +Unicode::String PathFinder::extensionsString() const { - return joinList( this->exts, pathSeparator()); + return _exts.join(pathSeparator()); } -const PathFinder::string_list & PathFinder::extensions() const +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 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; + return _exts; } -size_t PathFinder::path( const string & p ) +size_t PathFinder::path(const Unicode::String& p) { - this->paths.erase( this->paths.begin(), this->paths.end() ); - return tokenize_to_list( p, this->paths, pathSeparator() ); + _paths = string_list(p, pathSeparator()); + return _paths.size(); } -size_t PathFinder::path( const PathFinder::string_list & p ) +size_t PathFinder::path(const PathFinder::string_list& p) { - this->paths = p; - return this->paths.size(); + _paths = p; + return _paths.size(); } -void PathFinder::addPath( const string & p ) +void PathFinder::addPath(const Unicode::String& p) { - tokenize_to_list( p, this->paths, pathSeparator() ); + string_list np = string_list(p, pathSeparator()); + _paths.insert(_paths.end(), np.begin(), np.end()); } -size_t PathFinder::extensions( const string & p ) +size_t PathFinder::extensions(const Unicode::String& p) { - this->exts.erase( this->exts.begin(), this->exts.end() ); - return tokenize_to_list( p, this->exts, pathSeparator() ); + _exts = string_list(p, pathSeparator()); + return _exts.size(); } -size_t PathFinder::extensions( const PathFinder::string_list & e ) +size_t PathFinder::extensions(const PathFinder::string_list& e) { - this->exts = e; - return this->exts.size(); + _exts = e; + return _exts.size(); } -void PathFinder::addExtension( const string & p ) +void PathFinder::addExtension(const Unicode::String& e) { - tokenize_to_list( p, this->exts, pathSeparator() ); + string_list ne = string_list(e, pathSeparator()); + _exts.insert(_exts.end(), ne.begin(), ne.end()); } // static -bool PathFinder::isAccessible( const string & path ) +bool PathFinder::isAccessible(const Unicode::String & path) { #if WIN32 # define CHECKACCESS _access @@ -153,73 +102,88 @@ # define CHECKRIGHTS F_OK #endif - return 0 == CHECKACCESS( path.c_str(), CHECKRIGHTS ); + return 0 == CHECKACCESS(path.utf8().c_str(), CHECKRIGHTS); #undef CHECKACCESS #undef CHECKRIGHTS } -string PathFinder::basename( const std::string & name ) +Unicode::String PathFinder::basename(const Unicode::String& name) { - string::size_type slashat = name.find_last_of( PathFinder::dirSeparator() ); - if ( slashat == string::npos ) + Unicode::String::size_type slashat = + name.find_last_of(PathFinder::dirSeparator()); + + if(slashat == Unicode::String::npos) return name; - return name.substr( slashat + 1 ); + + return name.substr(slashat + 1); } -std::string PathFinder::dirSeparator() +Unicode::String PathFinder::dirSeparator() { #ifdef WIN32 - return std::string( "\\" ); + return Unicode::String( "\\" ); #else - return std::string( "/" ); + return Unicode::String( "/" ); #endif } -std::string PathFinder::pathSeparator() +Unicode::String PathFinder::pathSeparator() { #ifdef WIN32 - return std::string( ";" ); + return Unicode::String( ";" ); #else - return std::string( ":" ); + return Unicode::String( ":" ); #endif } - -string PathFinder::find( const string & resource, bool check_cache ) const +const Unicode::String& PathFinder::find(const Unicode::String& resource, + bool check_cache) const { - //static const std::string NOT_FOUND = "PathFinder::find() : no findie"; - if( resource.empty() ) return resource; + static Unicode::String emptyStr; + + P_TRACE(PathFinder) << "find(" << resource << ")"; + + if(resource.empty()) + return emptyStr; #define CHECKPATH(CHECKAT) \ + P_TRACE(PathFinder) << "testing '" << CHECKAT << "'"; \ if( ! CHECKAT.empty() && PathFinder::isAccessible( CHECKAT ) ) \ - { this->hitcache[resource] = CHECKAT; return CHECKAT; } + { \ + P_TRACE(PathFinder) << "found '" << CHECKAT << "'"; \ + return _hitcache.insert(std::make_pair(resource, CHECKAT)).first->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; + P_TRACE(PathFinder) << "lookup in cache '" << resource << "'"; + StringStringMap::iterator mi = _hitcache.find(resource); + if(mi != _hitcache.end()) + { + P_TRACE(PathFinder) << "found (cached) '" << (*mi).first + << "' -> '" << (*mi).second << "'"; + return (*mi).second; + } } - CHECKPATH( resource ); - - string_list::const_iterator piter = this->paths.begin(); - string_list::const_iterator eiter = this->exts.begin(); + CHECKPATH(resource); - string path; - string ext; + string_list::const_iterator piter = _paths.begin(); + string_list::const_iterator eiter = _exts.begin(); - if ( PathFinder::isAccessible( resource ) ) + if(PathFinder::isAccessible(resource)) + { + P_TRACE(PathFinder) << "found '" << resource << "'"; return resource; + } - piter = this->paths.begin(); - string checkhere; + Unicode::String path, ext; + Unicode::String checkhere; - while ( piter != this->paths.end() ) + while(piter != _paths.end()) { - path = ( *piter ); - if ( !path.empty() ) + path = (*piter); + if(!path.empty()) { path += PathFinder::dirSeparator(); } @@ -227,29 +191,23 @@ ++piter; checkhere = path + resource; - //CERR << "find( " << resource << " ) checking " << checkhere << std::endl; - CHECKPATH( checkhere ); + CHECKPATH(checkhere); - eiter = this->exts.begin(); - while ( eiter != this->exts.end() ) + eiter = _exts.begin(); + while(eiter != _exts.end()) { - ext = ( *eiter ); - ++eiter; + ext = (*eiter++); checkhere = path + resource + ext; - //CERR << "find( " << resource << " ) checking " << checkhere << std::endl; - CHECKPATH( checkhere ); + CHECKPATH(checkhere); } } - //CERR << "find( "<<resource<<" ): not found :(" << std::endl; - // so arguable: - // this->hitcache[resource] = ""; - return string(); + return emptyStr; } void PathFinder::clearCache() { - this->hitcache.clear(); + _hitcache.clear(); } // /** |