00001 #ifndef CDB_PATH_NAME_HH 00002 #define CDB_PATH_NAME_HH 00003 00004 // File and Version Information: 00005 // $Id: CdbPathName.hh,v 1.4 2005/08/23 18:54:59 gapon Exp $ 00006 00007 #include "CdbBase/CdbCommon.hh" 00008 00009 #include <vector> 00010 #include <string> 00011 #include <iostream> 00012 00013 template< class T > class CdbIItr; 00014 00015 /// This class facilitates path name operations 00016 /** 00017 * Details to come... 00018 */ 00019 class CdbPathName { 00020 00021 friend class CdbPathNameItr; 00022 00023 public: 00024 00025 /// The separator of path elements 00026 /** 00027 * This separator is also used as the name of the "root" path. 00028 */ 00029 static const char* separator( ); 00030 00031 /// Produce a name ending with the folder separator 00032 /** 00033 * The method would ensure that the output string produced out of 00034 * the input one has a path name separator in its end. 00035 * 00036 * EXAMPLE: 00037 * 00038 * Input Output 00039 * ------- ------ 00040 * <empty> ** ERROR ** 00041 * "" ** ERROR ** 00042 * "/" "/" 00043 * "abc" "abc/" 00044 * "efg/" "efg/" 00045 * "xyz//" "xyz//" 00046 * 00047 * The method won't attempt to sqeeze multiple separators as in case of 00048 * the "xyz//" example shown above into a single one. 00049 * 00050 * Also, the input string should not be empty (non-initialized) and its length 00051 * must be at least 1. 00052 * 00053 * IMPORTANT NOTE: 00054 * 00055 * If the input string is empty or it has zero characters then the method would 00056 * crash the application. That's because the method doesn't have a clean way 00057 * to tell a caller abouta wrong value of the parameter. In the end passing 00058 * an empty string into this method is most likely a result of either a serious 00059 * bug in an application's logic, or a sign of a memory corruption. 00060 */ 00061 static std::string separatorTerminated( const std::string& theName ); 00062 00063 public: 00064 00065 /// The normal & default constructor 00066 /** 00067 * Loads the path from specified string. This constructor expects 00068 * a string pointer to be passed as value of the parameter. If this 00069 * is a null pointer or if it points onto a non-translatable string 00070 * then the new object will remain in the invalid state. 00071 * 00072 * @see CdbPathName::isValid() 00073 */ 00074 explicit CdbPathName( const char* theStr = 0 ); 00075 00076 /// Construct an object from a string 00077 /** 00078 * A non-empty string is expected. Otherwise the object will be put into 00079 * a non-valid state. 00080 * 00081 * @see CdbPathName::isValid() 00082 */ 00083 explicit CdbPathName( const std::string& theStr ); 00084 00085 /// The copy constructor 00086 00087 CdbPathName( const CdbPathName& thePath ); 00088 00089 /// The destructor 00090 00091 ~CdbPathName( ); 00092 00093 /// The assignment operator 00094 00095 CdbPathName& operator=( const CdbPathName& thePath ); 00096 00097 /// Check if the path has the valid state 00098 /** 00099 * Any other operations with a path object can only make sense 00100 * if this method will return true. 00101 */ 00102 bool isValid( ) const; 00103 00104 /// Check if the path is an ABSOLUTE one 00105 /** 00106 * Every absolute path begins with the "root" path. The "root" path 00107 * itself is just a case of an absolute path. 00108 */ 00109 bool isAbsolute( ) const; 00110 00111 /// Check if the path corresponds to the "root" path 00112 /** 00113 * More details... 00114 */ 00115 bool isRoot( ) const; 00116 00117 /// Check if the path is a COMPOSITE one 00118 /** 00119 * A composite path is made from more than one simple token. 00120 */ 00121 bool isComposite( ) const; 00122 00123 /// Get the first element in the path 00124 /** 00125 * More details... 00126 */ 00127 CdbPathName first( ) const; 00128 00129 /// Get the rest of the path after (excluding) the first element 00130 /** 00131 * More details... 00132 */ 00133 CdbPathName afterFirst( ) const; 00134 00135 /// Get the last element in the path 00136 /** 00137 * More details... 00138 */ 00139 CdbPathName last( ) const; 00140 00141 /// Get the beginning of the path before (excluding) the last element 00142 /** 00143 * More details... 00144 */ 00145 CdbPathName beforeLast( ) const; 00146 00147 /// The explicit conversion 00148 00149 std::string toString( ) const; 00150 00151 /// Add one path to the other one 00152 00153 CdbPathName operator+( const CdbPathName& thePath ) const; 00154 00155 /// Add a C-style string to a path 00156 00157 CdbPathName operator+( const char* theStr ) const; 00158 00159 /// Add a C++ string to a path 00160 00161 CdbPathName operator+( const std::string& theStr ) const; 00162 00163 /// Return an iterator to iterate over the elements of a path 00164 /** 00165 * The iterator will return elements of the path in a form 00166 * of path objects. 00167 */ 00168 CdbIItr<CdbPathName>* iterator( ) const; 00169 00170 /// Dump the internal state of the object 00171 00172 void dump( std::ostream& theStream, 00173 const std::string& thePrefix = "" ) const; 00174 00175 private: 00176 00177 /// Special constructor 00178 /** 00179 * The constructor will take a copy of the passed passed list of tokens. 00180 * The constructor will also recalculate the attributes of the object given 00181 * its new value. 00182 * 00183 * NOTE: 00184 * 00185 * The constructor does not check for the correctness of the parameters' 00186 * values. 00187 */ 00188 CdbPathName( const std::vector< std::string >& theTokens /**< a list of tokens to load. */ 00189 ); 00190 00191 /// Translate specified string into tokens 00192 /** 00193 * This method will break (if successful) the specified input string into tokens, 00194 * update a list of tokens, and it will also try to build a full path. 00195 * 00196 * A successful completion of the method indicated by the "true" means that the path 00197 * object can be assigned a valid state. 00198 * 00199 * NOTE: The method doesn't change the path descriptors! 00200 */ 00201 bool translateStringIntoTokens( const char* theStr ); 00202 00203 /// Rebuild the full path 00204 /** 00205 * The method will reconstruct a full path name of the current list of tokens. 00206 * 00207 * The operation assumes that a non-empty list of tokens has already been loaded. The correct 00208 * contents of the list of tokens is not assumed though. 00209 * 00210 * @see CdbPathName::isValid() 00211 */ 00212 bool rebuildFullPath( ); 00213 00214 /// Recalculate descriptor flags: isAbsolute, isRoot and isComposite 00215 /** 00216 * NOTE: The object must be in a valid state! 00217 * 00218 * @see CdbPathName::isValid() 00219 */ 00220 void recalculateDescriptorFlags( ); 00221 00222 private: 00223 00224 // The current state of the object 00225 00226 bool _isValid; 00227 00228 // The path descriptors 00229 00230 bool _isAbsolute; 00231 bool _isRoot; 00232 bool _isComposite; 00233 00234 // The path as a single string 00235 00236 std::string _name; 00237 00238 // The path as a list of tokens 00239 00240 std::vector< std::string > _tokens; 00241 }; 00242 00243 inline 00244 std::ostream& 00245 operator<<( std::ostream& o, 00246 const CdbPathName& thePathName ) 00247 { 00248 o << thePathName.toString( ); 00249 return o; 00250 } 00251 00252 inline 00253 CdbPathName 00254 operator+( const char* theStr, 00255 const CdbPathName& thePath ) 00256 { 00257 return CdbPathName( theStr ) + thePath; 00258 } 00259 00260 inline 00261 CdbPathName 00262 operator+( const std::string& theStr, 00263 const CdbPathName& thePath ) 00264 { 00265 return CdbPathName( theStr ) + thePath; 00266 } 00267 00268 #endif // CDB_PATH_NAME_HH
1.3-rc3