29.07.2011 - fundamental new
This commit is contained in:
58
pifile.h
58
pifile.h
@@ -1,7 +1,6 @@
|
||||
#ifndef PIFILE_H
|
||||
#define PIFILE_H
|
||||
|
||||
#include "piincludes.h"
|
||||
#include "pistring.h"
|
||||
#include <fstream>
|
||||
|
||||
@@ -11,33 +10,46 @@ class PIFile
|
||||
{
|
||||
public:
|
||||
PIFile() {;}
|
||||
|
||||
|
||||
enum Mode {Read = fstream::in, Write = fstream::out, Truncate = fstream::trunc, New = fstream::app};
|
||||
|
||||
PIFile(const PIString & path, Flags<Mode> mode = Read | Write) {open(path, mode);}
|
||||
|
||||
PIFile(const PIString & path, PIFlags<Mode> mode = Read | Write) {open(path, mode);}
|
||||
PIFile(const PIFile & file) {cpath = file.cpath; cmode = file.cmode;}
|
||||
~PIFile() {if (isOpened()) close();}
|
||||
|
||||
bool open(const PIString & path, Flags<Mode> mode = Read | Write);
|
||||
inline void close() {stream.close();}
|
||||
inline void clear() {string st = cpath.stdString(); close(); stream.open(st.c_str(), fstream::trunc | fstream::binary | (fstream::openmode)(int)cmode);}
|
||||
void seek(int position);
|
||||
inline void seekToEnd() {stream.seekg(0, fstream::end);}
|
||||
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);}
|
||||
void seek(int 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(int line) {stream.clear(); seekToBegin(); piForTimes (line) readLine();} // line 0 - begin of file
|
||||
void resize(int new_size, char fill = 0);
|
||||
inline void fill(char c) {stream.fill(c);}
|
||||
inline void flush() {stream.flush();}
|
||||
void fill(char c) {stream.fill(c);}
|
||||
void flush() {stream.flush();}
|
||||
PIString readLine();
|
||||
|
||||
int readAll(void * data);
|
||||
PIByteArray readAll();
|
||||
void remove() {if (isOpened()) close(); std::remove(cpath.data());}
|
||||
|
||||
PIString path() const {return cpath;}
|
||||
Flags<Mode> mode() const {return cmode;}
|
||||
void setPath(const PIString & path) {cpath = path;}
|
||||
PIFlags<Mode> mode() const {return cmode;}
|
||||
int size();
|
||||
int pos();
|
||||
bool isOpened() const {return stream.is_open();}
|
||||
bool isEnd() const {return stream.eof();}
|
||||
|
||||
bool isOpened() {return stream.is_open();}
|
||||
bool isEnd() {return stream.eof();}
|
||||
bool isEmpty() {return (size() == 0);}
|
||||
|
||||
PIFile & writeData(const void * data, int size_) {stream.write((char * )data, size_); return *this;}
|
||||
PIFile & readData(void * data, int size_) {stream.read((char * )data, size_); return *this;}
|
||||
|
||||
PIFile & writeToBinLog(ushort id, const void * data, int size) {writeBinary(id).writeBinary((ushort)size).writeData(data, size); flush(); return *this;}
|
||||
|
||||
PIFile & writeBinary(const char v) {stream.write((char * )&v, sizeof(v)); return *this;}
|
||||
PIFile & writeBinary(const short v) {stream.write((char * )&v, sizeof(v)); return *this;}
|
||||
PIFile & writeBinary(const int v) {stream.write((char * )&v, sizeof(v)); return *this;}
|
||||
@@ -48,10 +60,10 @@ public:
|
||||
PIFile & writeBinary(const ulong v) {stream.write((char * )&v, sizeof(v)); return *this;}
|
||||
PIFile & writeBinary(const float v) {stream.write((char * )&v, sizeof(v)); return *this;}
|
||||
PIFile & writeBinary(const double v) {stream.write((char * )&v, sizeof(v)); return *this;}
|
||||
|
||||
|
||||
PIFile & operator <<(const char & v) {stream.write(&v, 1); return *this;}
|
||||
//PIFile & operator <<(const string & v) {stream.write(v.c_str(), v.size()); return *this;}
|
||||
PIFile & operator <<(const PIString & v) {stream.write(v.data(), v.size()); return *this;}
|
||||
PIFile & operator <<(const PIString & v) {string s = v.stdString(); stream.write(s.c_str(), s.size()); return *this;}
|
||||
PIFile & operator <<(const PIByteArray & v) {stream.write((char * )v.data(), v.size()); return *this;}
|
||||
PIFile & operator <<(const short & v) {stream << v; return *this;}
|
||||
PIFile & operator <<(const int & v) {stream << v; return *this;}
|
||||
@@ -62,7 +74,7 @@ public:
|
||||
PIFile & operator <<(const ulong & v) {stream << v; return *this;}
|
||||
PIFile & operator <<(const float & v) {stream << v; return *this;}
|
||||
PIFile & operator <<(const double & v) {stream << v; return *this;}
|
||||
|
||||
|
||||
PIFile & operator >>(char & v) {stream >> v; return *this;}
|
||||
PIFile & operator >>(short & v) {stream >> v; return *this;}
|
||||
PIFile & operator >>(int & v) {stream >> v; return *this;}
|
||||
@@ -74,11 +86,13 @@ 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);
|
||||
|
||||
private:
|
||||
fstream stream;
|
||||
PIString cpath;
|
||||
Flags<Mode> cmode;
|
||||
|
||||
PIFlags<Mode> cmode;
|
||||
|
||||
};
|
||||
|
||||
#endif // PIFILE_H
|
||||
|
||||
Reference in New Issue
Block a user