00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Jeff Lait 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_FileUtil.h ( UT Library, C++) 00015 * 00016 * COMMENTS: This verb class provides a wide assortment of 00017 * file utility functions. 00018 */ 00019 00020 #ifndef __UT_FileUtil__ 00021 #define __UT_FileUtil__ 00022 00023 #include "UT_API.h" 00024 #include <iostream.h> 00025 #include <stdio.h> 00026 #include <SYS/SYS_Types.h> 00027 #include "UT_NTUtil.h" // for mode_t 00028 00029 class UT_String; 00030 class UT_WorkBuffer; 00031 00032 // Here are flags used the by the file utility functions: 00033 // If this is set, the showPrompt will be called before any destructive 00034 // action. 00035 #define UT_FILEUTIL_PROMPT 1 00036 // If this is set, the original directory will be included in the 00037 // sweep. 00038 #define UT_FILEUTIL_INCLUSIVE 2 00039 00040 // The standard return code is 0 for success, negative for failure. 00041 // An appropriate error is passed to the error method which can 00042 // be overridden to do something more interesting than dumping to 00043 // the console. 00044 class UT_API UT_FileUtil 00045 { 00046 public: 00047 UT_FileUtil() {} 00048 virtual ~UT_FileUtil() {} 00049 00050 /// Returns the file size of that file in the given path. Returns -1 on 00051 /// error. 00052 static int64 getFileSize(const char *path); 00053 00054 /// Follow the specified symbolic link to the contents, recursing if the 00055 /// contents are themselves another symbolic link. This method will fail 00056 /// if the specified path is not to a symbolic link, or if the link does 00057 /// not point to an existing file when allow_dangling_link is false. 00058 static int resolveSymbolicLinks(const char *path, 00059 UT_WorkBuffer &contents, 00060 bool allow_dangling_link = false); 00061 00062 // Copies a file on disk from the source to the dest. 00063 // This is only good for smallish files as we allocate a file size 00064 // memory buffer. 00065 static int copyFile(const char *srcname, const char *dstname); 00066 // Copies a file from a disk into a stream. This is good for 00067 // building a .cpio archive. 00068 static int copyFileToStream(const char *srcname, ostream &os); 00069 00070 // Move a file, returns 0 on success. 00071 static int moveFile(const char *srcname, const char *dstname); 00072 // Delete a file 00073 static int removeFile(const char *fname); 00074 00075 /// Make a single directory. All leading path components must exist. 00076 /// The function returns true if the path already exists or if the path was 00077 /// made. 00078 /// @param path @n The path to create 00079 /// @param mode @n The unix file mode 00080 /// @param ignore_mask @n 00081 /// By default, the users umask will be used in conjunction with the 00082 /// mode. This parameter will @b force the mode to be the given value. 00083 static bool makeDir(const char *path, mode_t mode=0777, 00084 bool ignore_umask = false); 00085 00086 /// Make a directory and all the parent directories needed 00087 /// @param path @n The path to create 00088 /// @param mode @n The unix file mode 00089 /// @param ignore_mask @n 00090 /// By default, the users umask will be used in conjunction with the 00091 /// mode. This parameter will @b force the mode to be the given value. 00092 static bool makeDirs(const char *path, mode_t mode=0777, 00093 bool ignore_umask = false); 00094 00095 /// Locks a file for exclusive access. Will not return until the file lock 00096 /// is achiveved or an error occurs. 00097 /// Note: On Linux, if any file descriptor referring to this file is 00098 /// closed, the lock will be automatically be closed. Be very careful! 00099 /// Returns true if the file lock succeeded, and false otherwise. 00100 static bool lockFile(int fd); 00101 00102 /// Unlocks a file that was locked for exclusive access with lockFile, 00103 /// above. 00104 /// Returns true if the unlock succeeded, and false otherwise. 00105 static bool unlockFile(int fd); 00106 00107 // This navigates up the given path the specified number of levels. 00108 // It adds a \0 at the correct slash, leaving path to be the 00109 // proper path name. 00110 // eg: upDirectory("foo/bar.vex", 1) = "foo" 00111 // upDriectory("foo/bar/", 1) = "foo" 00112 static void upDirectory(char *path, int levels); 00113 00114 // This scans the (ascii) file fname for any line which starts 00115 // with the prefix. If readonly is not true, it writes out a new 00116 // version of the file lacking those lines. The return is the 00117 // number of lines that would be removed, or negative if error. 00118 static int removeLinesFromFile(const char *fname, 00119 const char *prefix, int readonly = 0); 00120 00121 // This is of the form 00122 // removeOverrideFiles("d:/hsite/houdini5", 00123 // "subnet/Sop", 00124 // "foo.ds", "Dialog Script", UT_FILEUTIL_PROMPT); 00125 // If the stripinfo is specified, it will do a removeLinesFromFile 00126 // with stripinfo rather than delete. 00127 int removeOverrideFiles(const char *newbasepath, 00128 const char *relpath, const char *fname, 00129 const char *english, int flags, 00130 const char *stripinfo = 0); 00131 00132 /// Parses the file in search of the given XML element and outputs 00133 /// its contents into the string argument. Returns true uppon success 00134 /// or false on failure. 00135 static bool readXMLElementFromFile( const char * xml_file_path, 00136 const char * element_name, 00137 UT_String & element_data ); 00138 00139 // These are UI methods you can override to gain different behaviours 00140 // for your instance: 00141 // Severity is the same as UI_Manager::UI_ErrorType. 00142 virtual void showError(const char *error, int severity = 2); 00143 // 0 means Cancel, 1 OK. 00144 virtual int showPrompt(const char *prompt); 00145 }; 00146 00147 /// A class that allows you to create and lock a file for exclusive access. 00148 /// When instances of this class go out of scope, the locked file is 00149 /// automatically unlocked and closed. 00150 class UT_API UT_AutoFileLock 00151 { 00152 public: 00153 /// Open and lock a file for exclusive reading and writing. 00154 UT_AutoFileLock(const char *filename) 00155 { 00156 // Try opening it as if it exists first, then try creating it. 00157 myFile = fopen(filename, "rb+"); 00158 if (!myFile) 00159 myFile = fopen(filename, "wb+"); 00160 00161 if (myFile) 00162 myLockSucceeded = UT_FileUtil::lockFile(fileno(myFile)); 00163 else 00164 myLockSucceeded = false; 00165 } 00166 00167 ~UT_AutoFileLock() 00168 { 00169 if (myFile) 00170 { 00171 if (myLockSucceeded) 00172 UT_FileUtil::unlockFile(fileno(myFile)); 00173 00174 fclose(myFile); 00175 } 00176 } 00177 00178 /// Get the file pointer for this locked file. 00179 FILE *getFile() const 00180 { 00181 return myFile; 00182 } 00183 00184 /// Return whether the file is actually locked. 00185 bool isLocked() const 00186 { 00187 return myLockSucceeded; 00188 } 00189 00190 private: 00191 FILE *myFile; 00192 bool myLockSucceeded; 00193 }; 00194 00195 #endif 00196
1.5.9