DeSR Dependency Parser |
00001 /* 00002 ** IXE C++ Library 00003 ** ixe/io/FileHandle.h 00004 ** ---------------------------------------------------------------------- 00005 ** Copyright (c) 2000 Ideare SpA. 00006 ** Copyright (c) 2000 Giuseppe Attardi (attardi@di.unipi.it). 00007 ** ---------------------------------------------------------------------- 00008 ** 00009 ** This file is part of DeSR. 00010 ** 00011 ** DeSR is free software; you can redistribute it and/or modify it 00012 ** under the terms of the GNU General Public License, version 3, 00013 ** as published by the Free Software Foundation. 00014 ** 00015 ** DeSR is distributed in the hope that it will be useful, 00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 ** GNU General Public License for more details. 00019 ** 00020 ** You should have received a copy of the GNU General Public License 00021 ** along with this program. If not, see <http://www.gnu.org/licenses/>. 00022 ** ---------------------------------------------------------------------- 00023 */ 00024 00025 #ifndef IXE_FileHandle_H 00026 #define IXE_FileHandle_H 00027 00028 // Settings 00029 #include "include/ixe.h" 00030 00031 // standard 00032 #include <sys/types.h> /* for off_t */ 00033 #include <fstream> 00034 00035 // local 00036 #include "include/error.h" 00037 00038 #ifdef _WIN32 00039 #include <windows.h> 00040 #endif 00041 00042 using namespace std; 00043 00044 namespace IXE { 00045 00046 namespace io { 00047 00048 //============================================================================= 00052 //============================================================================= 00053 00054 class FileHandle 00055 { 00056 public: 00057 FileHandle() : fd_(0) { } 00058 00059 FileHandle(char const* path, std::ios::openmode mode = std::ios::in) { 00060 if (!open(path, mode)) 00061 throw FileError(std::string("Cannot open file ") + path); 00062 } 00063 00064 ~FileHandle() { close(); } 00065 00066 bool open(char const *path, 00067 std::ios::openmode mode = std::ios::in); 00068 void close(); 00069 off64_t size(); 00070 bool truncate(off64_t size); 00071 bool write(off64_t offset, void const* data, Size length); 00072 bool write(void const* data, Size length); 00073 00074 # ifdef _WIN32 00075 HANDLE fd_; 00076 # else 00077 int fd_; // Unix file descriptor 00078 # endif 00079 }; 00080 00081 } // namespace io 00082 00083 } // namespace IXE 00084 00085 #endif /* IXE_FileHandle_H */