00001 #ifndef UTIL_H 00002 #define UTIL_H 00003 00004 #include "Exceptions.h" 00005 #include <string> 00006 #include <sys/stat.h> 00007 #include <sys/types.h> 00008 #include <unistd.h> 00009 #include <fcntl.h> 00010 #include <dirent.h> 00011 #include <vector> 00012 00013 BOREALIS_NAMESPACE_BEGIN; 00014 00015 // If src ends with suffic, this just returns src. Otherwise, this returns 00016 // src + suffix. 00017 string ensureStringEndsWith(string src, char suffix) 00018 throw (exception); 00019 00020 bool dirExists(string dirPath) 00021 throw (std::exception); 00022 00023 // Returns true iff any object exists with the specified pathname. 00024 bool fileExists(string filePath) 00025 throw (std::exception); 00026 00027 // Deletes the specified file. Throws an exception of the file didn't exist or 00028 // if any other problem occured. 00029 void deleteFile(string filePath) 00030 throw (std::exception); 00031 00032 // Returns a vector populated with the simple filename of each matching 00033 // file. A file matches if its filename starts with the string 'prefix'. 00034 // The filenames are sorted alphapetically in the return value, with 00035 // filenames[0] being alphabetically-first result. 00036 // 00037 // After this method returns, 'filenames' will be populated with and only with 00038 // the sorted filename lists of the hits. The filenames don't include the 00039 // pathname of the directory. 00040 void listFilesWithPrefix(string dirpath, string prefix, vector<string> & filenames) 00041 throw (std::exception); 00042 00043 // Like the write(2) function, but automatically handles lseek, partial writes, and 00044 // the EINTR return code. 00045 void writeData(int fd, off_t fileOffset, const char *pBuffer, ssize_t numBytesToWrite) 00046 throw (std::exception); 00047 00048 // Like the read(2) function, but automatically handles lseek, partial writes, and 00049 // the EINTR return code. 00050 void readData(int fd, off_t fileOffset, char *pBuffer, ssize_t numBytesRead) 00051 throw (std::exception); 00052 00053 // Thin wrapper on the a use of the open system call. Returns the handle, or throws 00054 // an exception. 00055 int openFileWithFlags(string filename, int flags) 00056 throw (std::exception); 00057 00058 // Thin wrapper on the a use of the open system call. Returns the handle, or throws 00059 // an exception. 00060 int openFileWithFlagsAndPerms(string filename, int flags, int perms) 00061 throw (std::exception); 00062 00063 // Thin wrapper on the a use of the close system call. 00064 void closeFile(int fd) 00065 throw (std::exception); 00066 00067 // Thin wrapper on the a use of the fstat system call. 00068 off_t getFileLen(int fd) 00069 throw (std::exception); 00070 00071 // Thin wrapper on the a use of the fstat system call. 00072 unsigned long getBlockSize(int fd) 00073 throw (std::exception); 00074 00075 // Returns the current offset in the file, like what's returned by lseek(...). 00076 off_t getFileOffset(int fd) 00077 throw (std::exception); 00078 00079 // Thin wrapper on the lseek system call. 00080 off_t setFileOffset(int fd, off_t offset, int whence) 00081 throw (std::exception); 00082 00083 // Tries to set the file the specified size. If the file needs to grow to meet the 00084 // specified size, but we run out of disk space first, then this reset the file to the size it was 00085 // when this function was invoked, and then throw a SmTooFullException exception. 00086 // 00087 // If 'forceAlloc' is true, then this will explicitely write a '.' character to every 00088 // byte that the file is grown by, to ensure that the space is really given to it 00089 // by the file system. 00090 void setFileLen(int fd, off_t newFileLen, bool forceAlloc) 00091 throw (std::exception); 00092 00093 // As trivial of a function as you're ever likely to find. This exists because 00094 // timeval doesn't have such a constructor. 'seconds' and 'microSeconds' must 00095 // both be non-negative, and 'microSeconds' must be < 1,000,000. 00096 timeval makeTimeval(long seconds, 00097 long microSeconds) 00098 throw (exception); 00099 00100 // Creates a timeval from the specified components. It's OK for 'usec' to have a 00101 // value > 999,999. This function will normalize it appropriately. 00102 // 00103 // Throws an exception if the specified values exceed the capacity of a timeval. 00104 // (E.g., if after normalization, 'sec' > max-long-value.) 00105 // 00106 // *** NOTE: THIS ASSUMES THAT (sec + (usec/1,000,000)) IS LESS THAN OR EQUAL TO 00107 // *** numeric_limits<unsigned long long>::max(). IF THAT'S NOT TRUE, THEN THIS 00108 // *** FUNCTION WILL FAIL TO DETECT OVERFLOW IN ITS CALCULATIONS. 00109 timeval makeNormalTimeval(unsigned long long sec, 00110 unsigned long long usec) 00111 throw (exception); 00112 00113 // Returns a tmeval that's the average of the specified timevals. The timevals 00114 // are encoded as some bunch of seconds, and some bunch of microseconds. 00115 // 'numValues' must be > 0. 00116 timeval getAvgTimevalBySums(unsigned long long summedSeconds, 00117 unsigned long long summedMicroSeconds, 00118 unsigned int numValues) 00119 throw (exception); 00120 00121 // Returns the difference in times between the two moments (t2 - t1). This will 00122 // throw an exception if overflow seems likely, but some overflows may not be 00123 // caught. 00124 // 00125 // If... 00126 // (A) t2 >= t1, and 00127 // (B) both fields in each structure are non-negative, and 00128 // (C) t1.tv_usec < 1,000,000 and t2.tv_usec < 1,000,000 00129 // 00130 // Then the returned timeval will also have properties (B) and (C) (i.e., be 00131 // 'normal'). 00132 timeval getTimevalDiff(const timeval & t1, 00133 const timeval & t2) 00134 throw (exception); 00135 00136 // Just represents the fields of the timeval struct. 00137 string timevalToSimpleString(const timeval & tv); 00138 00139 // Just represents the fields of the tm struct. 00140 string tmToSimpleString(const tm & aTime); 00141 00142 // Renders the timeval into a form suitable for humans to read. 00143 string timevalToHumanString(const timeval & tv); 00144 00145 00146 // Returns true iff t1's members = t2's (corresponding) members. 00147 bool timevalsEqual(const timeval & t1, 00148 const timeval & t2); 00149 00150 // If t1 > t2, returns 1. If t1 = t2, returns 0. If t1 < t2, returns -1. 00151 // If the timevals aren't normalized (tv_usec in [0, 999999]), this throws an 00152 // exception. 00153 int timevalsComp(const timeval & t1, 00154 const timeval & t2); 00155 00156 00157 // Reduces the accumulator by the subtrahend. Will borrow as needed, based on the 00158 // assumption that the numbers involved are actually: 00159 // ((1,000,000 * Seconds) + Micrseconds) microseconds. 00160 // 00161 // No assumption is made about normality of the numbers. If this method does 00162 // happen to notice underflow (which it's not obligated to do), it will throw 00163 // an exception. 00164 // 00165 // Behavior isn't defined when the accumulator will be forced negative by the 00166 // operation. 00167 void subtractTimestampSums(unsigned long long & accumulatorSeconds, 00168 unsigned long long & accumulatorMicroSeconds, 00169 unsigned long long subrahendSeconds, 00170 unsigned long long subrahendMicroSeconds) 00171 throw (exception); 00172 00173 BOREALIS_NAMESPACE_END; 00174 00175 #endif