Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef WAVEPACKET_UTIL_FILE_H__
00035 #define WAVEPACKET_UTIL_FILE_H__
00036
00037
00038
00039 #include "common/common.h"
00040
00041 #ifdef WIN32
00042
00043
00044 #include <io.h>
00045
00046 #else // WIN32
00047
00048
00049 #include <dirent.h>
00050 #include <unistd.h>
00051 #include <sys/errno.h>
00052 #include <sys/file.h>
00053
00054 #endif // WIN32
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 const char * GetExtension(IN const char * filename) throw();
00082
00083
00084
00085
00086
00087 bool hasExtension(IN const char * filename, IN const char * extension) throw();
00088
00089
00090
00091
00092 void GetFileRoot(IN const char * filename,
00093 OUT std::string& file_root);
00094
00095
00096
00097 void GetParentDirectory(IN const char * filename,
00098 OUT std::string& directory) throw();
00099
00100
00101
00102
00103 const char * getTopDirectory(IN const char * filename,
00104 OUT std::string& topDir);
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 std::string getPathRelativeTo(IN const char * startFilename,
00115 IN const char * relFilename);
00116
00117
00118
00119 void appendPath(IN const char * parentDirectory,
00120 IN const char * nextPath,
00121 OUT std::string& path);
00122
00123
00124
00125 const char * GetFilename(IN const char * path) throw();
00126
00127
00128 void getTempfile(IN const char * path,
00129 OUT std::string& tempfile) throw();
00130
00131
00132 void getUserHomePath(OUT std::string& path);
00133
00134
00135 bool ContainsWhitespace(IN const char * test) throw();
00136
00137
00138 bool doesPathExist(IN const char * path) throw();
00139
00140
00141 void createEmptyFileIfDoesNotExist(IN const char * path) throw();
00142
00143
00144
00145 void walkDirectoryTree(IN const char * rootDirectory,
00146 IN const char * matchExtension,
00147 OUT VecString& paths);
00148
00149 #ifdef WIN32
00150
00151 struct DIR {
00152 DIR(void) throw() { this->clear(); }
00153 void clear(void) throw() {
00154 path = "";
00155 h = NULL;
00156 }
00157
00158 std::string path;
00159 HANDLE h;
00160 };
00161
00162 DIR * opendir(IN const char * path);
00163 struct dirent {
00164 char d_name[MAX_PATH];
00165 };
00166 struct dirent * readdir(IO DIR * dir);
00167 int readdir_r(IO DIR * dir, IN struct dirent * entry,
00168 OUT struct dirent ** result);
00169 int closedir(IN DIR * dir);
00170 char * strerror_r(IN int error, IN char * buf, IN int bufsize);
00171 #endif // WIN32
00172
00173
00174
00175
00176 #define THROW_ERROR( error , args ) \
00177 { \
00178 if (error) { \
00179 char buffer[512]; \
00180 strerror_r(error, buffer, sizeof(buffer)); \
00181 WAVE_EX(wex); \
00182 wex << "Encountered an error (" << error; \
00183 wex << "): " << buffer; \
00184 wex << "\n" << args ; \
00185 } \
00186 }
00187
00188
00189
00190
00191
00192 class smart_fd {
00193 public:
00194
00195 smart_fd(void) throw() : m_fd(-1) { }
00196 smart_fd(IN int fd) throw() : m_fd(fd) { }
00197 ~smart_fd(void) throw() { this->clear(); }
00198
00199
00200 void clear(void) throw() {
00201 if (m_fd > -1) {
00202 #ifdef WIN32
00203 if (_close(m_fd)) {
00204 #else // WIN32
00205 if (close(m_fd)) {
00206 #endif // WIN32
00207 DPRINTF("Failed to close descriptor");
00208 }
00209 m_fd = -1;
00210 }
00211 }
00212
00213 bool open(IN const char * path, IN int flags) {
00214 this->clear();
00215 #ifdef WIN32
00216 m_fd = _open(path, flags);
00217 #else // WIN32
00218 m_fd = ::open(path, flags);
00219 #endif // WIN32
00220 return (m_fd > -1);
00221 }
00222
00223 operator int(void) const throw() { return m_fd; }
00224
00225 operator bool(void) const throw() { return (m_fd > -1); }
00226 bool operator !(void) const throw() { return (m_fd < 0); }
00227
00228 private:
00229
00230 smart_fd(IN const smart_fd&);
00231
00232
00233 int m_fd;
00234 };
00235
00236
00237
00238
00239
00240
00241
00242 class smart_dir {
00243 public:
00244
00245 smart_dir(void) throw() : m_dir(NULL) { }
00246 ~smart_dir(void) throw() { this->clear(); }
00247
00248
00249 void clear(void) throw() {
00250 if (m_dir) {
00251 closedir(m_dir);
00252 m_dir = NULL;
00253 }
00254 }
00255
00256 operator bool(void) const throw() { return !!m_dir; }
00257
00258 void open(IN const char * path) {
00259 this->clear();
00260 m_dir = opendir(path);
00261 if (!m_dir) {
00262 THROW_ERROR(errno,
00263 "Failed to open directory: " << path);
00264 }
00265 }
00266
00267 void close(void) throw() { this->clear(); }
00268
00269 bool getNextEntry(OUT struct dirent& entry) {
00270 ASSERT(m_dir, "need to open directory first!");
00271 struct dirent * result = NULL;
00272 THROW_ERROR(readdir_r(m_dir, &entry, &result),
00273 "Failed to read next directory entry");
00274 return (result != NULL);
00275 }
00276
00277 private:
00278
00279 smart_dir(IN const smart_dir&);
00280
00281
00282 DIR * m_dir;
00283 };
00284
00285
00286
00287
00288
00289 class AdvisoryLock {
00290 public:
00291 typedef int error_t;
00292
00293 AdvisoryLock(void) throw() : m_fd(-1) { }
00294 ~AdvisoryLock(void) throw() {
00295 this->unlock();
00296 }
00297
00298 bool attemptLock(IN const char * filename,
00299 IN int operation) throw() {
00300 ASSERT(filename, "NULL filename");
00301 ASSERT(-1 == m_fd, "lock object already used");
00302
00303 #ifdef WIN32
00304 operation = operation;
00305 m_fd = 0;
00306 #else // WIN32
00307
00308 m_fd = open(filename, O_RDONLY);
00309 if (-1 == m_fd) {
00310 error_t error = errno;
00311 DPRINTF("Could not open file: %s", filename);
00312 DPRINTF("Open failed with error %d : %s",
00313 error, strerror(error));
00314 return false;
00315 }
00316
00317
00318 int result = flock(m_fd, operation);
00319 if (-1 == result) {
00320 close(m_fd);
00321 m_fd = -1;
00322 error_t error = errno;
00323 DPRINTF("Lock failed with error %d : %s",
00324 error, strerror(error));
00325 return false;
00326 }
00327 #endif // WIN32
00328
00329 return true;
00330 }
00331
00332 void unlock(void) throw() {
00333 #ifndef WIN32
00334 if (-1 != m_fd) {
00335 flock(m_fd, LOCK_UN);
00336 close(m_fd);
00337 m_fd = -1;
00338 }
00339 #endif // WIN32
00340 }
00341
00342 private:
00343 int m_fd;
00344 };
00345
00346
00347
00348
00349 #endif // WAVEPACKET_UTIL_FILE_H__
00350