File.cpp
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 #include "adchpp.h"
00020
00021 #include "File.h"
00022
00023 namespace adchpp {
00024
00025 using namespace std;
00026
00027 string File::read(uint32_t len) throw(FileException) {
00028 string tmp;
00029 tmp.resize(len);
00030 uint32_t x = read(&tmp[0], len);
00031 tmp.resize(x);
00032 return tmp;
00033 }
00034
00035 #ifdef _WIN32
00036
00037 File::File(const string& aFileName, int access, int mode) throw(FileException) {
00038 dcassert(access == WRITE || access == READ || access == (READ | WRITE));
00039
00040 int m = 0;
00041 if(mode & OPEN) {
00042 if(mode & CREATE) {
00043 m = (mode & TRUNCATE) ? CREATE_ALWAYS : OPEN_ALWAYS;
00044 } else {
00045 m = (mode & TRUNCATE) ? TRUNCATE_EXISTING : OPEN_EXISTING;
00046 }
00047 } else {
00048 if(mode & CREATE) {
00049 m = (mode & TRUNCATE) ? CREATE_ALWAYS : CREATE_NEW;
00050 } else {
00051 dcassert(0);
00052 }
00053 }
00054 int a = 0;
00055 if(access & READ)
00056 a |= GENERIC_READ;
00057 if(access & WRITE)
00058 a |= GENERIC_WRITE;
00059
00060 h = ::CreateFile(aFileName.c_str(), a, FILE_SHARE_READ, NULL, m, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
00061
00062 if(h == INVALID_HANDLE_VALUE) {
00063 throw FileException(Util::translateError(GetLastError()));
00064 }
00065
00066 }
00067
00068 int64_t File::getSize() {
00069 DWORD x;
00070 DWORD l = ::GetFileSize(h, &x);
00071
00072 if( (l == INVALID_FILE_SIZE) && (GetLastError() != NO_ERROR))
00073 return -1;
00074
00075 return (int64_t)l | ((int64_t)x)<<32;
00076 }
00077
00078 int64_t File::getSize(const string& aFileName) {
00079 WIN32_FIND_DATA fd;
00080 HANDLE hFind;
00081
00082 hFind = FindFirstFile(aFileName.c_str(), &fd);
00083
00084 if (hFind == INVALID_HANDLE_VALUE) {
00085 return -1;
00086 } else {
00087 FindClose(hFind);
00088 return ((int64_t)fd.nFileSizeHigh << 32 | (int64_t)fd.nFileSizeLow);
00089 }
00090 }
00091
00092 string File::getFilePath(const string& path) throw() {
00093 string::size_type i = path.find_last_of("\\/");
00094 return (i != string::npos) ? path.substr(0, i) : path;
00095 }
00096
00097 string File::getFileName(const string& path) throw() {
00098 string::size_type i = path.find_last_of("\\/");
00099 return (i != string::npos) ? path.substr(i + 1) : path;
00100 }
00101
00102 bool File::isAbsolutePath(const string& path) throw() {
00103 return (path.length() >= 3 && path[1] == ':' && (path[2] == '\\' || path[2] == '/')) ||
00104 (path.length() >= 1 && (path[0] == '\\' || path[0] == '/'));
00105 }
00106
00107 void File::ensureDirectory(const string& aFile) throw() {
00108 string::size_type start = 0;
00109
00110 while( (start = aFile.find_first_of("\\/", start)) != string::npos) {
00111 ::CreateDirectory(aFile.substr(0, start+1).c_str(), NULL);
00112 start++;
00113 }
00114 }
00115
00116 #else // _WIN32
00117
00118 File::File(const string& aFileName, int access, int mode) throw(FileException) {
00119 dcassert(access == WRITE || access == READ || access == (READ | WRITE));
00120
00121 int m = 0;
00122 if(access == READ)
00123 m |= O_RDONLY;
00124 else if(access == WRITE)
00125 m |= O_WRONLY;
00126 else
00127 m |= O_RDWR;
00128
00129 if(mode & CREATE) {
00130 m |= O_CREAT;
00131 }
00132 if(mode & TRUNCATE) {
00133 m |= O_TRUNC;
00134 }
00135 h = open(aFileName.c_str(), m, S_IRUSR | S_IWUSR);
00136 if(h == -1)
00137 throw FileException("Could not open file");
00138 }
00139
00140 int64_t File::getSize() {
00141 struct stat s;
00142 if(fstat(h, &s) == -1)
00143 return -1;
00144
00145 return (int64_t)s.st_size;
00146 }
00147
00148 int64_t File::getSize(const string& aFileName) {
00149 struct stat s;
00150 if(stat(aFileName.c_str(), &s) == -1)
00151 return -1;
00152
00153 return s.st_size;
00154 }
00155
00156 string File::getFilePath(const string& path) throw() {
00157 string::size_type i = path.rfind('/');
00158 return (i != string::npos) ? path.substr(0, i) : path;
00159 }
00160
00161 string File::getFileName(const string& path) throw() {
00162 string::size_type i = path.rfind('/');
00163 return (i != string::npos) ? path.substr(i + 1) : path;
00164 }
00165
00166 bool File::isAbsolutePath(const string& path) throw() {
00167 return path.length() >= 1 && path[0] == '/';
00168 }
00169
00170 void File::ensureDirectory(const string& aFile) throw() {
00171 string::size_type start = 0;
00172
00173 while( (start = aFile.find('/', start)) != string::npos) {
00174 ::mkdir(aFile.substr(0, start+1).c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
00175 start++;
00176 }
00177 }
00178 #endif
00179
00180 std::string File::makeAbsolutePath(const std::string& filename) {
00181 return makeAbsolutePath(Util::getAppPath() + PATH_SEPARATOR, filename);
00182 }
00183
00184 std::string File::makeAbsolutePath(const std::string& path, const std::string& filename) {
00185 return isAbsolutePath(filename) ? filename : path + filename;
00186 }
00187
00188 }