From: Christian P. <cp...@us...> - 2005-04-23 15:19:45
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20134/include/pclasses/System Modified Files: PathFinder.h Log Message: - Readability and some code cleanup - Removed variable pathSeparator cause it's system specific Index: PathFinder.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/System/PathFinder.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- PathFinder.h 25 Dec 2004 20:48:13 -0000 1.5 +++ PathFinder.h 23 Apr 2005 15:19:31 -0000 1.6 @@ -1,181 +1,183 @@ -#ifndef p_system_PATHFINDER_H -#define p_system_PATHFINDER_H +#ifndef P_System_PathFinder_h +#define P_System_PathFinder_h + // Author: stephan beal <st...@s1...> // License: Public Domain +// Readability cleanup and code P-ificaton by Christian Prochnow <cp...@se...> +// TODO: get rid of string_list and joinList and tokenize_to_list and provide a common +// implementation as it may be useful for other too. -- cproch +#include <pclasses/Export.h> #include <string> #include <list> #include <map> -#include <iostream> - -// #include <cl/StringList.h> // todo: get rid of this in the public interface. -namespace P { namespace System { - /** - PathFinder searches for keys using a set of prefixes - (paths) and suffixes (file extensions). - - Example: +namespace P { - <pre> - PathFinder p; - p.path( "/lib/lib:/usr/lib/lib" ); - p.extensions( ".a:.so" ); - cout << p.find( "z" ); - </pre> +namespace System { - That would print an empty string if it finds nothing, or a - string if it finds any of the following: +/*! + PathFinder searches for keys using a set of prefixes + (paths) and suffixes (file extensions). + + Example: - - z - - /lib/libz - - /lib/libz.a - - /lib/libz.so - - /usr/lib/libz - - /usr/lib/libz.a - - /usr/lib/libz.so + <pre> + PathFinder p; + p.path( "/lib/lib:/usr/lib/lib" ); + p.extensions( ".a:.so" ); + cout << p.find( "z" ); + </pre> - */ - class PathFinder - { - public: + That would print an empty string if it finds nothing, or a + string if it finds any of the following: - /** - A list type returned by some functions. - */ - typedef std::list<std::string> string_list; + - z + - /lib/libz + - /lib/libz.a + - /lib/libz.so + - /usr/lib/libz + - /usr/lib/libz.a + - /usr/lib/libz.so +*/ +class PSYSTEM_EXPORT PathFinder { + public: /** - Creates object with the given path/extension list. - */ - PathFinder( const std::string & path = std::string(), const std::string & ext = std::string(), const std::string & pathsep = ":" ); + A list type returned by some functions. + */ + typedef std::list<std::string> string_list; - virtual ~PathFinder(); - /** - Returns a ":"-separated string of all paths added via add/path(). - */ - std::string pathString() const; + Creates object with the given path/extension list. + */ + PathFinder(const std::string & path = std::string(), + const std::string & ext = std::string()); - /** - Sets the string used as a separator for the - string-based variants of path(), extentions(), etc. - */ - void pathSeparator( const std::string & sep ); + virtual ~PathFinder(); - /** - Returns the path separator string. Default is ":"; - */ - std::string pathSeparator() const; + /** + Returns a pathSeparator()-delimited string of all paths + added via add/path(). + */ + std::string pathString() const; /** - Sets the path to p, which should be a path_separtor()-delimited string. - Returns the number of path elements parsed from p. - */ + Sets the path to p, which should be a pathSeparator()-delimited string. + Returns the number of path elements parsed from p. + */ virtual size_t path( const std::string & p ); - /** - Sets the path to the given list of directories. - Returns the number of elements in the list. - */ + /** + Sets the path to the given list of directories. + Returns the number of elements in the list. + */ virtual size_t path( const string_list & p ); /** - Adds p to the path. May be path_separtor()-delimited. - */ + Adds p to the path. May be path_separtor()-delimited. + */ virtual void addPath( const std::string & p ); /** - Adds a "search extension." Sample: - finder.extension( ".txt:.TXT" ); Will now try all - path combinations with the rightmost characters - matching ".txt" or ".TXT" (in that order). Be sure - to include a period if you want that searched - - that is so this class can be used to find non-files - and those with non-traditional extensions, like - "foo_EXT". - */ + Adds a "search extension." Sample: + finder.extensions( ".txt:.TXT" ); Will now try all + path combinations with the rightmost characters + matching ".txt" or ".TXT" (in that order). Be sure + to include a period if you want that searched - + that is so this class can be used to find non-files + and those with non-traditional extensions, like + "foo_EXT". + */ virtual void addExtension( const std::string & ext = std::string() ); + /** - like addExtension(), but overwrites extension list. - Returns the number of entries parsed from the string. - */ + like addExtension(), but overwrites extension list. + Returns the number of entries parsed from the string. + */ virtual size_t extensions( const std::string & ext ); - /** - Sets the extensions list to the given list. - Returns the number of entries in p. - */ + + /** + Sets the extensions list to the given list. + Returns the number of entries in p. + */ virtual size_t extensions( const string_list & p ); /** - Returns the pathSeparator()-delimited listed of file - suffixes to use when searching for a path. - */ - std::string extensionsString() const; - /** - Returns this object's extensions list. - */ + Returns the pathSeparator()-delimited listed of file + suffixes to use when searching for a path. + */ + std::string extensionsString() const; + + /** + Returns this object's extensions list. + */ const string_list & extensions() const; - /** - Returns true if path is readable. - */ + /** + Returns true if path is readable. + */ static bool isAccessible( const std::string & path ); /** - Returns the "base name" of the given string: any part - following the final directory separator character. - */ + Returns the "base name" of the given string: any part + following the final directory separator character. + */ static std::string basename( const std::string & ); - /** - Returns a platform-dependent path separator. This - is set when the class is compiled. - */ + /** + Returns a platform-dependent directory separator. This + is set when the class is compiled. + */ static std::string dirSeparator(); /** - Returns the full path of the given resource, provided it could be found - in all paths or with all extensions added and provided it is readable. - Note that this might return a relative path, especially if the - resourcename passed to it immediately resolves to an existing resource. - It returns an empty string if the resourcename cannot be found in - the filesystem tree (or is otherwise unaccessible). + Returns a platform-dependent path-list separator string. + This is set when the class is compiled. + */ + static std::string pathSeparator(); - If check_cache is false then this function ignores - it's lookup cache and searches again, otherwise it - uses a cache. When caching it will always return - the same result for any given resourcename. - */ - std::string find( const std::string & resourcename, bool check_cache = true ) const; + /** + Returns the full path of the given resource, provided it could be found + in all paths or with all extensions added and provided it is readable. + Note that this might return a relative path, especially if the + resourcename passed to it immediately resolves to an existing resource. + It returns an empty string if the resourcename cannot be found in + the filesystem tree (or is otherwise unaccessible). - /** - Empties the hit-cache used by find(). + If check_cache is false then this function ignores + it's lookup cache and searches again, otherwise it + uses a cache. When caching it will always return + the same result for any given resourcename. + */ + std::string find( const std::string & resourcename, bool check_cache = true ) const; - Design note: because the cache is mutable data, we - *could* legally make this function const. Something - to consider. - */ - void clearCache(); + /** + Empties the hit-cache used by find(). + Design note: because the cache is mutable data, we + *could* legally make this function const. Something + to consider. + */ + void clearCache(); /** - Returns a list of all items added via addPath() and path(). - */ + Returns a list of all items added via addPath() and path(). + */ const string_list & path() const; - - private: + private: string_list paths; string_list exts; - std::string pathseparator; typedef std::map < std::string, std::string > StringStringMap; typedef StringStringMap::iterator StringStringIterator; mutable StringStringMap hitcache; - }; +}; +} // !namespace System -} } // namespaces -#endif // p_system_PATHFINDER_H +} // !namespace P + +#endif // !P_System_PathFinder_h |