1 /******************************************************************************** 2 * * 3 * F i l e I n f o r m a t i o n a n d M a n i p u l a t i o n * 4 * * 5 ********************************************************************************* 6 * Copyright (C) 2000,2005 by Jeroen van der Zijp. All Rights Reserved. * 7 ********************************************************************************* 8 * This library is free software; you can redistribute it and/or * 9 * modify it under the terms of the GNU Lesser General Public * 10 * License as published by the Free Software Foundation; either * 11 * version 2.1 of the License, or (at your option) any later version. * 12 * * 13 * This library is distributed in the hope that it will be useful, * 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 16 * Lesser General Public License for more details. * 17 * * 18 * You should have received a copy of the GNU Lesser General Public * 19 * License along with this library; if not, write to the Free Software * 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * 21 ********************************************************************************* 22 * $Id: FXFile.h,v 1.69 2005/01/16 16:06:06 fox Exp $ * 23 ********************************************************************************/ 24 #ifndef FXFILE_H 25 #define FXFILE_H 26 27 28 /// Declared as "C" so as to not clash tag-names 29 extern "C" { struct stat; } 30 31 namespace FX { 32 33 34 /// Options for listing files 35 enum { 36 LIST_MATCH_ALL = 0, /// Matching files and directories 37 LIST_NO_FILES = 1, /// Don't list any files 38 LIST_NO_DIRS = 2, /// Don't list any directories 39 LIST_ALL_FILES = 4, /// List all files 40 LIST_ALL_DIRS = 8, /// List all directories 41 LIST_HIDDEN_FILES = 16, /// List hidden files also 42 LIST_HIDDEN_DIRS = 32, /// List hidden directories also 43 LIST_NO_PARENT = 64, /// Don't include '..' in the listing 44 LIST_CASEFOLD = 128 /// Matching is case-insensitive 45 }; 46 47 48 namespace FXFile { 49 50 51 /// Get current user name 52 FXString FXAPI getCurrentUserName(); 53 54 /// Return value of environment variable name 55 FXString FXAPI getEnvironment(const FXString& name); 56 57 /// Return the home directory for the current user. 58 FXString FXAPI getHomeDirectory(); 59 60 /// Return the home directory for a given user. 61 FXString FXAPI getUserDirectory(const FXString& user); 62 63 /// Return temporary directory. 64 FXString FXAPI getTempDirectory(); 65 66 /// Set the current working directory 67 FXbool FXAPI setCurrentDirectory(const FXString& path); 68 69 /// Get the current working directory 70 FXString FXAPI getCurrentDirectory(); 71 72 /// Set the current drive (for Win32 systems) 73 FXbool FXAPI setCurrentDrive(const FXString& prefix); 74 75 /// Return the current drive (for Win32 systems) 76 FXString FXAPI getCurrentDrive(); 77 78 /// Get executable path 79 FXString FXAPI getExecPath(); 80 81 /** 82 * Return the directory part of the path name. 83 * Note that directory("/bla/bla/") is "/bla/bla" and NOT "/bla". 84 * However, directory("/bla/bla") is "/bla" as we expect! 85 */ 86 FXString FXAPI directory(const FXString& file); 87 88 /** 89 * Return name and extension part of the path name. 90 * Note that name("/bla/bla/") is "" and NOT "bla". 91 * However, name("/bla/bla") is "bla" as we expect! 92 */ 93 FXString FXAPI name(const FXString& file); 94 95 /// Return file title, i.e. document name only 96 FXString FXAPI title(const FXString& file); 97 98 /// Return extension part of the file name 99 FXString FXAPI extension(const FXString& file); 100 101 /// Return file name less the extension 102 FXString FXAPI stripExtension(const FXString& file); 103 104 /// Return the drive letter prefixing this file name (if any). 105 FXString FXAPI drive(const FXString& file); 106 107 /// Perform tilde or environment variable expansion 108 FXString FXAPI expand(const FXString& file); 109 110 /** 111 * Simplify a file path; the path will remain relative if it was relative, 112 * or absolute if it was absolute. Also, a trailing "/" will be preserved 113 * as this is important in other functions. 114 * For example, simplify("..//aaa/./bbb//../c/") becomes "../aaa/c/". 115 */ 116 FXString FXAPI simplify(const FXString& file); 117 118 /// Return absolute path from current directory and file name 119 FXString FXAPI absolute(const FXString& file); 120 121 /// Return absolute path from base directory and file name 122 FXString FXAPI absolute(const FXString& base,const FXString& file); 123 124 /// Return relative path of file to the current directory 125 FXString FXAPI relative(const FXString& file); 126 127 /// Return relative path of file to given base directory 128 FXString FXAPI relative(const FXString& base,const FXString& file); 129 130 /** 131 * Return root of absolute path; on Unix, this is just "/". On 132 * Windows, this is "\\" or "C:\". Returns the empty string 133 * if the given path is not absolute. 134 */ 135 FXString FXAPI root(const FXString& file); 136 137 /// Enquote filename to make safe for shell 138 FXString FXAPI enquote(const FXString& file,FXbool forcequotes=FALSE); 139 140 /// Dequote filename to get original again 141 FXString FXAPI dequote(const FXString& file); 142 143 /** 144 * Generate unique filename of the form pathnameXXX.ext, where 145 * pathname.ext is the original input file, and XXX is a number, 146 * possibly empty, that makes the file unique. 147 */ 148 FXString FXAPI unique(const FXString& file); 149 150 /// Search path list for this file, return full path name for first occurrence 151 FXString FXAPI search(const FXString& pathlist,const FXString& file); 152 153 /// Return path to directory above input directory name 154 FXString FXAPI upLevel(const FXString& file); 155 156 /// Return true if file name is absolute 157 FXbool FXAPI isAbsolute(const FXString& file); 158 159 /// Return true if input directory is a top-level directory 160 FXbool FXAPI isTopDirectory(const FXString& file); 161 162 /// Return true if input path is a file name 163 FXbool FXAPI isFile(const FXString& file); 164 165 /// Return true if input path is a link 166 FXbool FXAPI isLink(const FXString& file); 167 168 /// Return true if input path is a directory 169 FXbool FXAPI isDirectory(const FXString& file); 170 171 /// Return true if input path is a file share 172 FXbool FXAPI isShare(const FXString& file); 173 174 /// Return true if file is readable 175 FXbool FXAPI isReadable(const FXString& file); 176 177 /// Return true if file is writable 178 FXbool FXAPI isWritable(const FXString& file); 179 180 /// Return true if file is executable 181 FXbool FXAPI isExecutable(const FXString& file); 182 183 /// Return true if owner has read-write-execute permissions 184 FXbool FXAPI isOwnerReadWriteExecute(const FXString& file); 185 186 /// Return true if owner has read permissions 187 FXbool FXAPI isOwnerReadable(const FXString& file); 188 189 /// Return true if owner has write permissions 190 FXbool FXAPI isOwnerWritable(const FXString& file); 191 192 /// Return true if owner has execute permissions 193 FXbool FXAPI isOwnerExecutable(const FXString& file); 194 195 /// Return true if group has read-write-execute permissions 196 FXbool FXAPI isGroupReadWriteExecute(const FXString& file); 197 198 /// Return true if group has read permissions 199 FXbool FXAPI isGroupReadable(const FXString& file); 200 201 /// Return true if group has write permissions 202 FXbool FXAPI isGroupWritable(const FXString& file); 203 204 /// Return true if group has execute permissions 205 FXbool FXAPI isGroupExecutable(const FXString& file); 206 207 /// Return true if others have read-write-execute permissions 208 FXbool FXAPI isOtherReadWriteExecute(const FXString& file); 209 210 /// Return true if others have read permissions 211 FXbool FXAPI isOtherReadable(const FXString& file); 212 213 /// Return true if others have write permissions 214 FXbool FXAPI isOtherWritable(const FXString& file); 215 216 /// Return true if others have execute permissions 217 FXbool FXAPI isOtherExecutable(const FXString& file); 218 219 /// Return true if the file sets the user id on execution 220 FXbool FXAPI isSetUid(const FXString& file); 221 222 /// Return true if the file sets the group id on execution 223 FXbool FXAPI isSetGid(const FXString& file); 224 225 /// Return true if the file has the sticky bit set 226 FXbool FXAPI isSetSticky(const FXString& file); 227 228 /// Return owner name from uid if available 229 FXString FXAPI owner(FXuint uid); 230 231 /// Return owner name of file if available 232 FXString FXAPI owner(const FXString& file); 233 234 /// Return group name from gid if available 235 FXString FXAPI group(FXuint gid); 236 237 /// Return group name of file if available 238 FXString FXAPI group(const FXString& file); 239 240 /// Return permissions string 241 FXString FXAPI permissions(FXuint mode); 242 243 /// Return file size in bytes 244 FXlong FXAPI size(const FXString& file); 245 246 /** 247 * Return last modified time for this file, on filesystems 248 * where this is supported. This is the time when any data 249 * in the file was last modified. 250 */ 251 FXTime FXAPI modified(const FXString& file); 252 253 /** 254 * Return last accessed time for this file, on filesystems 255 * where this is supported. 256 */ 257 FXTime FXAPI accessed(const FXString& file); 258 259 /** 260 * Return created time for this file, on filesystems 261 * where this is supported. This is also the time when 262 * ownership, permissions, links, and other meta-data may 263 * have changed. 264 */ 265 FXTime FXAPI created(const FXString& file); 266 267 /** 268 * Return touched time for this file, on filesystems 269 * where this is supported. This is the time when anything 270 * at all, either contents or meta-data, about the file was 271 * changed. 272 */ 273 FXTime FXAPI touched(const FXString& file); 274 275 /// Match filenames using *, ?, [^a-z], and so on 276 FXbool FXAPI match(const FXString& pattern,const FXString& file,FXuint flags=(FILEMATCH_NOESCAPE|FILEMATCH_FILE_NAME)); 277 278 /** 279 * List files in a given directory. 280 * Returns the number of files in the string-array list which matched the 281 * pattern or satisfied the flag conditions. 282 */ 283 FXint FXAPI listFiles(FXString*& filelist,const FXString& path,const FXString& pattern="*",FXuint flags=LIST_MATCH_ALL); 284 285 /// Return current time 286 FXTime FXAPI now(); 287 288 /// Convert file time to date-string 289 FXString FXAPI time(FXTime filetime); 290 291 /** 292 * Convert file time to date-string as per strftime. 293 * Format characters supported by most systems are: 294 * 295 * %a %A %b %B %c %d %H %I %j %m %M %p %S %U %w %W %x %X %y %Y %Z %% 296 * 297 * Some systems support additional conversions. 298 */ 299 FXString FXAPI time(const FXchar *format,FXTime filetime); 300 301 /// Return file info as reported by system stat() function 302 FXbool FXAPI info(const FXString& file,struct stat& inf); 303 304 /// Return file info as reported by system lstat() function 305 FXbool FXAPI linkinfo(const FXString& file,struct stat& inf); 306 307 /// Return true if file exists 308 FXbool FXAPI exists(const FXString& file); 309 310 /// Return true if files are identical 311 FXbool FXAPI identical(const FXString& file1,const FXString& file2); 312 313 /// Return the mode flags for this file 314 FXuint FXAPI mode(const FXString& file); 315 316 /// Change the mode flags for this file 317 FXbool FXAPI mode(const FXString& file,FXuint mode); 318 319 /// Create new directory 320 FXbool FXAPI createDirectory(const FXString& path,FXuint mode); 321 322 /// Create new (empty) file 323 FXbool FXAPI createFile(const FXString& file,FXuint mode); 324 325 /** 326 * Concatenate srcfile1 and srcfile2 to a dstfile. 327 * If overwrite is true, then the operation fails if dstfile already exists. 328 * srcfile1 and srcfile2 should not be the same as dstfile. 329 */ 330 FXbool FXAPI concatenate(const FXString& srcfile1,const FXString& srcfile2,const FXString& dstfile,FXbool overwrite=FALSE); 331 332 /// Remove file or directory, recursively. 333 FXbool FXAPI remove(const FXString& file); 334 335 /// Copy file or directory, recursively 336 FXbool FXAPI copy(const FXString& srcfile,const FXString& dstfile,FXbool overwrite=FALSE); 337 338 /// Rename or move file or directory 339 FXbool FXAPI move(const FXString& srcfile,const FXString& dstfile,FXbool overwrite=FALSE); 340 341 /// Link file 342 FXbool FXAPI link(const FXString& srcfile,const FXString& dstfile,FXbool overwrite=FALSE); 343 344 /// Symbolic link file 345 FXbool FXAPI symlink(const FXString& srcfile,const FXString& dstfile,FXbool overwrite=FALSE); 346 347 /// Read symbolic link 348 FXString FXAPI symlink(const FXString& file); 349 350 } 351 352 } 353 354 #endif 355