File.h

Go to the documentation of this file.
00001 /* 
00002  * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #ifndef ADCHPP_FILE_H
00020 #define ADCHPP_FILE_H
00021 
00022 #include "Exception.h"
00023 #include "Util.h"
00024 
00025 #ifndef _WIN32
00026 #include <sys/stat.h>
00027 #include <fcntl.h>
00028 #endif
00029 
00030 namespace adchpp {
00031 
00032 STANDARD_EXCEPTION(FileException);
00033 
00034 class File
00035 {
00036 public:
00037     enum {
00038         READ = 0x01,
00039         WRITE = 0x02
00040     };
00041     
00042     enum {
00043         OPEN = 0x01,
00044         CREATE = 0x02,
00045         TRUNCATE = 0x04
00046     };
00047 
00048     ADCHPP_DLL File(const std::string& aFileName, int access, int mode = OPEN) throw(FileException);
00049 
00050     ADCHPP_DLL int64_t getSize();
00051     ADCHPP_DLL static int64_t getSize(const std::string& aFileName);
00052 
00053     ADCHPP_DLL std::string read(uint32_t len) throw(FileException);
00054     
00056     ADCHPP_DLL static std::string getFilePath(const std::string& name) throw();
00058     ADCHPP_DLL static std::string getFileName(const std::string& name) throw();
00059     ADCHPP_DLL static bool isAbsolutePath(const std::string& name) throw();
00060 
00061     ADCHPP_DLL static std::string makeAbsolutePath(const std::string& filename);
00062     ADCHPP_DLL static std::string makeAbsolutePath(const std::string& path, const std::string& filename);
00063 
00064     ADCHPP_DLL static void ensureDirectory(const std::string& aFile) throw();
00065     
00066 #ifdef _WIN32
00067     void close() {
00068         if(h != INVALID_HANDLE_VALUE) {
00069             CloseHandle(h);
00070             h = INVALID_HANDLE_VALUE;
00071         }
00072     }
00073     
00074     int64_t getPos() {
00075         LONG x = 0;
00076         DWORD l = ::SetFilePointer(h, 0, &x, FILE_CURRENT);
00077         
00078         return (int64_t)l | ((int64_t)x)<<32;
00079     }       
00080 
00081     void setPos(int64_t pos) {
00082         LONG x = (LONG) (pos>>32);
00083         ::SetFilePointer(h, (DWORD)(pos & 0xffffffff), &x, FILE_BEGIN);
00084     }       
00085     void setEndPos(int64_t pos) {
00086         LONG x = (LONG) (pos>>32);
00087         ::SetFilePointer(h, (DWORD)(pos & 0xffffffff), &x, FILE_END);
00088     }       
00089 
00090     void movePos(int64_t pos) {
00091         LONG x = (LONG) (pos>>32);
00092         ::SetFilePointer(h, (DWORD)(pos & 0xffffffff), &x, FILE_CURRENT);
00093     }
00094     
00095     uint32_t read(void* buf, uint32_t len) throw(FileException) {
00096         DWORD x;
00097         if(!::ReadFile(h, buf, len, &x, NULL)) {
00098             throw(FileException(Util::translateError(GetLastError())));
00099         }
00100         return x;
00101     }
00102 
00103     void write(const void* buf, size_t len) throw(FileException) {
00104         DWORD x;
00105         if(!::WriteFile(h, buf, (DWORD)len, &x, NULL)) {
00106             throw FileException(Util::translateError(GetLastError()));
00107         }
00108         if(x < len) {
00109             throw FileException("Unable to write, disk full?");
00110         }
00111     }
00112     
00113     void setEOF() throw(FileException) {
00114         dcassert(h != NULL);
00115         if(!SetEndOfFile(h)) {
00116             throw FileException(Util::translateError(GetLastError()));
00117         }
00118     }
00119 
00120     static void deleteFile(const std::string& aFileName) { ::DeleteFile(aFileName.c_str()); };
00121     static void renameFile(const std::string& source, const std::string& target) { ::MoveFile(source.c_str(), target.c_str()); };
00122 
00123 #else // WIN32
00124     
00125     void close() {
00126         if(h != -1) {
00127 			::close(h);
00128             h = -1;
00129         }
00130     }
00131 
00132     int64_t getPos() { return (int64_t) lseek(h, 0, SEEK_CUR); }
00133 
00134     void setPos(int64_t pos) { lseek(h, (off_t)pos, SEEK_SET); };
00135     void setEndPos(int64_t pos) { lseek(h, (off_t)pos, SEEK_END); };
00136     void movePos(int64_t pos) { lseek(h, (off_t)pos, SEEK_CUR); };
00137 
00138     uint32_t read(void* buf, uint32_t len) throw(FileException) {
00139         ssize_t x = ::read(h, buf, (size_t)len);
00140         if(x == -1)
00141             throw FileException(Util::translateError(errno));
00142         return (uint32_t)x;
00143     }
00144     
00145     void write(const void* buf, uint32_t len) throw(FileException) {
00146         ssize_t x;
00147         x = ::write(h, buf, len);
00148         if(x == -1)
00149             throw FileException(Util::translateError(errno));
00150         if(x < (ssize_t)len)
00151             throw FileException("Unable to write, disk full?");
00152     }
00153 
00157     void setEOF() throw(FileException) {
00158     }
00159 
00160     static void deleteFile(const std::string& aFileName) { ::unlink(aFileName.c_str()); };
00161     static void renameFile(const std::string& source, const std::string& target) { ::rename(source.c_str(), target.c_str()); };
00162     
00163 #endif // WIN32
00164 
00165     ~File() {
00166         close();
00167     }
00168 
00169     std::string read() throw(FileException) {
00170         setPos(0);
00171         return read((uint32_t)getSize());
00172     }
00173 
00174     void write(const std::string& aString) throw(FileException) {
00175         write((void*)aString.data(), aString.size());
00176     }
00177         
00178 private:
00179     File(const File&);
00180     File& operator=(const File&);
00181     
00182 #ifdef _WIN32
00183     HANDLE h;
00184 #else
00185     int h;
00186 #endif
00187 
00188 };
00189 
00190 }
00191 
00192 #endif // FILE_H
Generated on Sat Nov 27 23:37:53 2010 for adchpp by  doxygen 1.6.3