Files
pip/pifile.h

80 lines
3.6 KiB
C++

#ifndef PIFILE_H
#define PIFILE_H
#include <fstream>
#include "piincludes.h"
#include "pistring.h"
using std::fstream;
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() {if (isOpened()) close();}
bool open(const PIString & path, Flags<Mode> mode = Read | Write);
void close() {stream.close();}
void clear() {close(); stream.open(cpath.stdString().c_str(), fstream::trunc | (fstream::openmode)(int)cmode);}
void seek(int position);
void resize(int new_size, char fill = 0);
void fill(char c) {stream.fill(c);}
void flush() {stream.flush();}
PIString readLine();
PIString path() const {return cpath;}
Flags<Mode> mode() const {return cmode;}
int size();
int pos();
bool isOpened() const {return stream.is_open();}
bool isEnd() const {return stream.eof();}
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 & 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;}
PIFile & writeBinary(const long v) {stream.write((char * )&v, sizeof(v)); return *this;}
PIFile & writeBinary(const uchar v) {stream.write((char * )&v, sizeof(v)); return *this;}
PIFile & writeBinary(const ushort v) {stream.write((char * )&v, sizeof(v)); return *this;}
PIFile & writeBinary(const uint v) {stream.write((char * )&v, sizeof(v)); return *this;}
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 short & v) {stream << v; return *this;}
PIFile & operator <<(const int & v) {stream << v; return *this;}
PIFile & operator <<(const long & v) {stream << v; return *this;}
PIFile & operator <<(const uchar & v) {stream << v; return *this;}
PIFile & operator <<(const ushort & v) {stream << v; return *this;}
PIFile & operator <<(const uint & v) {stream << v; return *this;}
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;}
PIFile & operator >>(long & v) {stream >> v; return *this;}
PIFile & operator >>(uchar & v) {stream >> v; return *this;}
PIFile & operator >>(ushort & v) {stream >> v; return *this;}
PIFile & operator >>(uint & v) {stream >> v; return *this;}
PIFile & operator >>(ulong & v) {stream >> v; return *this;}
PIFile & operator >>(float & v) {stream >> v; return *this;}
PIFile & operator >>(double & v) {stream >> v; return *this;}
private:
fstream stream;
PIString cpath;
Flags<Mode> cmode;
};
#endif // PIFILE_H