1 //--- 2 // 3 // License: MIT 4 // 5 // Author: Ken Melero 6 // 7 // Description: This class provides manipulation of filenames. 8 // 9 //--- 10 // $Id$ 11 12 #ifndef ossimFilename_HEADER 13 #define ossimFilename_HEADER 1 14 15 #include <ossim/base/ossimConstants.h> 16 #include <ossim/base/ossimString.h> 17 18 class ossimLocalTm; 19 20 class OSSIM_DLL ossimFilename : public ossimString 21 { 22 public: 23 enum AccessModes 24 { 25 OSSIM_READ_WRITE = 6, 26 OSSIM_READ = 4, 27 OSSIM_WRITE = 2, 28 OSSIM_EXE = 1, // The PC docs say that this is ignored 29 OSSIM_EXIST = 0 30 }; 31 ossimFilename(); 32 ossimFilename(const ossimFilename& src); 33 ossimFilename(const ossimString& src); 34 ossimFilename(const std::string& src); 35 ossimFilename(const char* src); 36 const ossimFilename& operator=(const ossimFilename& f); 37 38 template <class Iter> ossimFilename(Iter s, Iter e); 39 40 static const ossimFilename NIL; 41 42 bool operator == (const ossimFilename& rhs)const; 43 44 bool operator == (const ossimString& rhs)const; 45 bool operator == (const char* rhs)const; 46 47 /** @brief Writes f to the output stream os. */ 48 // friend std::ostream& operator<<(std::ostream& os, 49 // const ossimFilename& s); 50 51 // void convertBackToForwardSlashes(); 52 // void convertForwardToBackSlashes(); 53 54 bool setTimes(ossimLocalTm* accessTime, 55 ossimLocalTm* modTime, 56 ossimLocalTm* createTime)const; 57 bool getTimes(ossimLocalTm* accessTime, 58 ossimLocalTm* modTime, 59 ossimLocalTm* createTime)const; 60 /** 61 * @brief Time in seconds since last accessed. 62 * 63 * @return The number of seconds since last accessed or -1 if file does not 64 * exist. 65 */ 66 ossim_int64 lastAccessed() const; 67 68 bool touch()const; 69 70 /** 71 * Method to do file name expansion. 72 * 73 * Like: ~/docs will be expanded to /home/user/docs 74 * 75 * @return The expanded file name. 76 * 77 * @note METHOD IS NOT COMPLETE YET. 78 */ 79 ossimFilename expand() const; 80 81 // Methods to test ossimFilename for various states. 82 bool exists() const; 83 bool isFile() const; 84 bool isDir() const; 85 bool isReadable() const; 86 bool isUrl() const; 87 bool isWriteable() const; 88 bool isExecutable() const; 89 90 ossim_int64 fileSize() const; 91 92 // Methods to access parts of the ossimFilename. 93 94 /** 95 * @return "tif" if file is "/data/images/t1.tif". 96 * 97 * @note The '.' (dot) is not returned. 98 */ 99 ossimString ext() const; 100 101 /** 102 * @return @return "/data/images" if file is "/data/images/t1.tif". 103 */ 104 ossimFilename path() const; 105 106 /** 107 * @return @return "c:" if file is "c:\data\images\t1.tif". 108 */ 109 ossimFilename drive() const; 110 111 /** 112 * @return @return "t1.tif" if file is "/data/images/t1.tif". 113 */ 114 ossimFilename file() const; 115 116 /** 117 * @return @return "t1" if file is "/data/images/t1.tif". 118 */ 119 ossimFilename fileNoExtension() const; 120 121 /** 122 * @return @return "/data/images/t1" if file is "/data/images/t1.tif". 123 * 124 * @note The '.' (dot) is not returned. 125 */ 126 ossimFilename noExtension() const; 127 128 /** 129 * Sets the extension of a file name. 130 * 131 * Given: 132 * ossimFilename f = "foo"; 133 * f.setExtension("tif"); 134 * f now equals "foo.tif" 135 * 136 * Given: 137 * ossimFilename f = "foo.jpg"; 138 * f.setExtension("tif"); 139 * f now equals "foo.tif" 140 * 141 * Given: 142 * ossimFilename f = "foo.jpg"; 143 * f.setExtension(".tif"); 144 * f now equals "foo.tif" 145 * 146 * Given: 147 * ossimFilename f = "foo."; 148 * ossimFilename f2; 149 * f2 = f.setExtension("tif"); 150 * f now equals "foo.tif" 151 * f2 now equals "foo.tif" 152 * 153 * @param e Extension to add or replace. 154 * 155 * @returns a reference to this. 156 */ 157 ossimFilename& setExtension(const ossimString& e); 158 159 /** 160 * Sets the file path and drive. 161 * Input: the drive to be set (this should come from the user's preferences) 162 * Example: "x:" 163 * Given: "/filepath/file.ext" 164 * @return @return "c:\filepath\file.ext". 165 */ 166 ossimFilename& setDrive(const ossimString& d); 167 ossimFilename& setPath(const ossimString& p); 168 ossimFilename& setFile(const ossimString& f); 169 170 void split(ossimString& drivePart, 171 ossimString& pathPart, 172 ossimString& filePart, 173 ossimString& extPart)const; 174 175 void merge(const ossimString& drivePart, 176 const ossimString& pathPart, 177 const ossimString& filePart, 178 const ossimString& extPart); 179 /*! 180 * Returns file appended onto this string. Path seperator is always placed 181 * between this and file. Returns file if this string is empty. 182 * Given: this = /foo and file = bar output = /foo/bar 183 * Given: this = /foo/ and file = bar output = /foo/bar 184 */ 185 ossimFilename dirCat(const ossimFilename& file) const; 186 187 /*! 188 */ 189 bool createDirectory(bool recurseFlag=true, 190 int perm=0775)const; 191 192 /*! 193 * @brief Removes pathname from filesystem if supported by platform. 194 * 195 * If pathname is a directory it will only be removed if empty. If supported 196 * will set errno and output equivalent string if one occurs. 197 * 198 * @return true on success, false on error. 199 */ 200 static bool remove(const ossimFilename& pathname); 201 202 /** 203 * Will use a wildcard remove. NOTE: This is in 204 * a regular expression format so if you want all 205 * files with prefix myfile to be erased then you would give 206 * it <path>/myfile.* 207 * 208 * The . here is a regular expression '.' character which says any character 209 * and the * "is any number of" 210 * 211 */ 212 static bool wildcardRemove(const ossimFilename& pathname); 213 214 bool rename(const ossimFilename& destFile, bool overwriteDestinationFlag=true)const; 215 216 bool remove()const; 217 bool wildcardRemove()const; 218 219 /** 220 * @brief Copies this file to output file. 221 * 222 * Copies this to output file. If output file is a directory, this->file() is 223 * appended to output file. 224 * 225 * @param outputFile File name to copy to. 226 * 227 * @return True on success, false on error. 228 */ 229 bool copyFileTo(const ossimFilename& outputFile) const; 230 231 /** 232 * @brief Checks whether file name is relative or absolute. 233 * 234 * @return true if file name has a relative path, false if absolute. 235 * 236 * @note This will return true if file name is empty. 237 */ 238 bool isRelative() const; 239 240 /** 241 * @brief Method to check if expansion is needed. 242 * 243 * This checks if file isRelative() first if isRelative is false (has 244 * absolute path) it then walks the file and looks for '$'. 245 * 246 * @return true if file name is relative or has environment vars. 247 * 248 * @note This will return false if file name is empty. 249 */ 250 bool needsExpansion() const; 251 252 /** @return The path separator. */ 253 char getPathSeparator() const; 254 255 /** 256 * Convenience method to append a generic timestamp to the base-name portion of the filename. 257 * This is useful for establishing rolling names for temporary files and logs. Returns this. 258 */ 259 ossimFilename& appendTimestamp(); 260 261 /** 262 * Convenience method to append a string to the base-name portion of the filename. Returns this. 263 */ 264 ossimFilename& append(const ossimString& append_this_to_filename); 265 266 /** 267 * @return This as a string converted to native slashes. 268 */ 269 std::string native() const; 270 271 protected: 272 273 /** 274 * @brief Converts slashes of this to internal '/' format. 275 */ 276 void converPathSeparator(); 277 278 // void convertToNative(); 279 280 /*! 281 * since windows uses \ for path separation 282 * and unix / we need to be consistent. 283 */ 284 static const char OSSIM_NATIVE_PATH_SEPARATOR; 285 static const char OSSIM_FILENAME_PATH_SEPARATOR; 286 }; 287 288 // inline std::ostream& operator<<(std::ostream& os, const ossimFilename& f) 289 // { 290 // return os << f.string().c_str(); 291 // } 292 293 #endif 294