05.11.2011 - stable version, 0.1.0, self-test program, work at GCC 2.95 - 4.5, VC 2010, MinGW, Linux, Windows, QNX
This commit is contained in:
37
pifile.h
37
pifile.h
@@ -1,41 +1,42 @@
|
||||
#ifndef PIFILE_H
|
||||
#define PIFILE_H
|
||||
|
||||
#include "pistring.h"
|
||||
#include "piobject.h"
|
||||
#include <fstream>
|
||||
|
||||
using std::fstream;
|
||||
|
||||
class PIFile
|
||||
class PIFile: public PIObject
|
||||
{
|
||||
public:
|
||||
PIFile() {;}
|
||||
PIFile(): PIObject() {;}
|
||||
|
||||
enum Mode {Read = fstream::in, Write = fstream::out, Truncate = fstream::trunc, New = fstream::app};
|
||||
enum Mode {Read = fstream::in, Write = fstream::out, Truncate = fstream::trunc, New = fstream::app, ReadWrite = Read | Write};
|
||||
|
||||
PIFile(const PIString & path, PIFlags<Mode> mode = Read | Write) {open(path, mode);}
|
||||
PIFile(const PIFile & file) {cpath = file.cpath; cmode = file.cmode;}
|
||||
PIFile(const PIString & path, PIFlags<Mode> mode = ReadWrite): PIObject(path) {open(path, mode);}
|
||||
PIFile(const PIFile & file): PIObject(file.cpath) {cpath = file.cpath; cmode = file.cmode;}
|
||||
~PIFile() {if (isOpened()) close();}
|
||||
|
||||
PIFile & operator =(const PIFile & f) {cpath = f.cpath; cmode = f.cmode; return *this;}
|
||||
|
||||
bool open(const PIString & path, PIFlags<Mode> mode);
|
||||
bool open(PIFlags<Mode> mode) {return open(cpath, mode);}
|
||||
bool open(const PIString & path) {return open(path, cmode);}
|
||||
bool open() {return open(cpath, cmode);}
|
||||
void close() {stream.close();}
|
||||
void clear() {string st = cpath.stdString(); close(); stream.open(st.c_str(), fstream::trunc | fstream::binary | (fstream::openmode)(int)cmode);}
|
||||
EVENT_HANDLER2(PIFile, bool, open, const PIString & , path, PIFlags<Mode>, mode);
|
||||
EVENT_HANDLER1(PIFile, bool, open, PIFlags<Mode>, mode) {return open(cpath, mode);}
|
||||
EVENT_HANDLER1(PIFile, bool, open, const PIString & , path) {return open(path, cmode);}
|
||||
EVENT_HANDLER0(PIFile, bool, open) {return open(cpath, cmode);}
|
||||
EVENT_HANDLER0(PIFile, void, close) {stream.close();}
|
||||
EVENT_HANDLER0(PIFile, void, clear) {string st = cpath.stdString(); close(); stream.open(st.c_str(), fstream::trunc | fstream::binary | (fstream::openmode)(int)cmode);}
|
||||
void seek(llong position) {stream.clear(); stream.seekg(position); stream.seekp(position);}
|
||||
void seekToBegin() {stream.clear(); stream.seekg(0, fstream::beg); stream.seekp(0, fstream::beg);}
|
||||
void seekToEnd() {stream.clear(); stream.seekg(0, fstream::end); stream.seekp(0, fstream::end);}
|
||||
void seekToLine(llong line) {stream.clear(); seekToBegin(); piForTimes (line) readLine();} // line 0 - begin of file
|
||||
void resize(llong new_size, char fill = 0);
|
||||
EVENT_HANDLER1(PIFile, void, resize, llong, new_size) {resize(new_size, 0);}
|
||||
EVENT_HANDLER2(PIFile, void, resize, llong, new_size, char, fill);
|
||||
void fill(char c) {stream.fill(c);}
|
||||
void flush() {stream.flush();}
|
||||
EVENT_HANDLER0(PIFile, void, flush) {stream.flush();}
|
||||
PIString readLine();
|
||||
llong readAll(void * data);
|
||||
PIByteArray readAll();
|
||||
void remove() {if (isOpened()) close(); std::remove(cpath.data());}
|
||||
PIByteArray readAll(bool forceRead = false);
|
||||
EVENT_HANDLER0(PIFile, void, remove) {if (isOpened()) close(); std::remove(cpath.data());}
|
||||
|
||||
PIString path() const {return cpath;}
|
||||
void setPath(const PIString & path) {cpath = path;}
|
||||
@@ -86,7 +87,9 @@ public:
|
||||
PIFile & operator >>(float & v) {stream >> v; return *this;}
|
||||
PIFile & operator >>(double & v) {stream >> v; return *this;}
|
||||
|
||||
static PIFile openTemporary(PIFlags<PIFile::Mode> mode = PIFile::Read | PIFile::Write);
|
||||
static PIFile openTemporary(PIFlags<PIFile::Mode> mode = PIFile::ReadWrite);
|
||||
static bool isExists(const PIString & path) {return std::ifstream(path.stdString().c_str()).good();}
|
||||
static bool remove(const PIString & path) {return std::remove(path.stdString().c_str()) == 0;}
|
||||
|
||||
private:
|
||||
fstream stream;
|
||||
|
||||
Reference in New Issue
Block a user