00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _pfile_h_
00021 #define _pfile_h_
00022
00023 #include <pclasses/config.h>
00024 #include <pclasses/pexport.h>
00025 #include <pclasses/pfileinfo.h>
00026 #include <pclasses/piodevice.h>
00027 #include <pclasses/piostream.h>
00028 #include <string>
00029
00030 namespace P {
00031
00033
00041 class PCORE_EXPORT File: public IODevice {
00042 public:
00043
00045
00049 enum openMode_t {
00050 Normal,
00052 Append,
00054 Truncate
00056 };
00057
00059
00064 enum createMode_t {
00065 OpenCreate,
00066 OpenExisting,
00067 CreateFail
00068 };
00069
00071 File() throw();
00072
00074 File(const File& f) throw(IOError);
00075
00077
00086 File(const char* path, accessMode_t access, openMode_t open = Normal,
00087 createMode_t create = OpenCreate, shareMode_t share = AllowNone) throw(IOError);
00088
00090 ~File() throw();
00091
00093
00104 void open(const char* path, accessMode_t access, openMode_t open = Normal,
00105 createMode_t create = OpenCreate, shareMode_t share = AllowNone) throw(IOError);
00106
00108
00116 size_t peek(char* buffer, size_t count) throw(IOError);
00117
00118 #ifndef HAVE_LARGEFILE64
00119
00121
00128 void truncate(off_t size) throw(IOError);
00129
00131 off_t size() throw(IOError);
00132
00133 #else
00134
00136
00143 void truncate(off64_t size) throw(IOError);
00144
00146 off64_t size() throw(IOError);
00147
00148 #endif
00149
00151 void reopen() throw(IOError);
00152
00154 inline const std::string& path() const throw()
00155 { return m_path; }
00156
00157 File& operator=(const File& f) throw(IOError);
00158
00160 static void unlink(const char* path) throw(IOError);
00161
00163 static bool exists(const char* path) throw();
00164
00166 static FileInfo stat(const char* path) throw(IOError);
00167
00169 static File mktemp(const char* prefix) throw(IOError);
00170
00171 private:
00172 std::string m_path;
00173 accessMode_t m_accessMode;
00174 openMode_t m_openMode;
00175 createMode_t m_createMode;
00176 shareMode_t m_shareMode;
00177 };
00178
00179
00181
00185 class PCORE_EXPORT FileStream: public File, public IOStream {
00186 public:
00187 FileStream()
00188 : File(), IOStream(*this, 4096) {}
00189
00190 FileStream(const char* path, accessMode_t access, openMode_t open = Normal,
00191 createMode_t create = OpenCreate, shareMode_t share = AllowNone) throw(IOError)
00192 : File(path, access, open, create), IOStream(*this, 4096) {}
00193
00194 FileStream(const FileStream& fs) throw(IOError)
00195 : File(fs), IOStream(*this, 4096) {}
00196
00197 FileStream(const File& f) throw(IOError)
00198 : File(f), IOStream(*this, 4096) {}
00199
00200 ~FileStream() throw()
00201 {
00202 try
00203 {
00204 close();
00205 }
00206 catch(...)
00207 {
00208 }
00209 }
00210
00211 void close() throw(IOError)
00212 {
00213 commit();
00214 File::close();
00215 }
00216
00217 void commit() throw(IOError)
00218 {
00219 sync();
00220 File::commit();
00221 }
00222
00223 FileStream& operator=(const FileStream& fs) throw(IOError)
00224 {
00225 sync(); flush();
00226 File::operator=(fs);
00227 return *this;
00228 }
00229
00230 };
00231
00232 }
00233
00234 #endif