15.10.2012 - version 0.2.0 - PIIODevice - base of file, ethernet, serial and packets extractor. PIEthernet now support also TCP (client and server). PIConsole now can align labels in each column individually.

This commit is contained in:
peri4
2012-10-15 23:36:18 +04:00
parent 5558add03e
commit cfc5eed75e
64 changed files with 1879 additions and 867 deletions

141
pifile.h Normal file → Executable file
View File

@@ -1,7 +1,7 @@
/*
PIP - Platform Independent Primitives
File
Copyright (C) 2011 Ivan Pelipenko peri4ko@gmail.com
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -20,100 +20,89 @@
#ifndef PIFILE_H
#define PIFILE_H
#include "piobject.h"
#include <fstream>
#include "piiodevice.h"
#include <cstdio>
using std::fstream;
class PIFile: public PIObject
class PIFile: public PIIODevice
{
public:
PIFile(): PIObject() {;}
PIFile(const PIString & path = PIString(), DeviceMode type = ReadWrite): PIIODevice(path, type) {openDevice();}
enum Mode {Read = fstream::in, Write = fstream::out, Truncate = fstream::trunc, New = fstream::app, ReadWrite = Read | Write};
//PIFile & operator =(const PIFile & f) {path_ = f.path_; type_ = f.type_; return *this;}
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() {close();}
PIFile & operator =(const PIFile & f) {cpath = f.cpath; cmode = f.cmode; return *this;}
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.clear(); 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 flush() {fflush(fd);}
EVENT_HANDLER(PIFile, void, clear) {close(); fd = fopen(path_.data(), "w"); close(); open();}
void seek(llong position) {if (!opened_) return; fseek(fd, position, SEEK_SET); clearerr(fd);}
void seekToBegin() {if (!opened_) return; fseek(fd, 0, SEEK_SET); clearerr(fd);}
void seekToEnd() {if (!opened_) return; fseek(fd, 0, SEEK_END); clearerr(fd);}
void seekToLine(llong line) {if (!opened_) return; seekToBegin(); piForTimes (line) readLine(); clearerr(fd);} // line 0 - begin of file
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);}
EVENT_HANDLER0(PIFile, void, flush) {stream.flush();}
//void fill(char c) {stream.fill(c);}
char readChar() {return (char)fgetc(fd);}
PIString readLine();
llong readAll(void * data);
PIByteArray readAll(bool forceRead = false);
EVENT_HANDLER0(PIFile, void, remove) {close(); std::remove(cpath.data());}
EVENT_HANDLER0(PIFile, void, remove) {close(); std::remove(path_.data());}
PIString path() const {return cpath;}
void setPath(const PIString & path) {cpath = path;}
PIFlags<Mode> mode() const {return cmode;}
void setPath(const PIString & path) {path_ = path; if (opened_) openDevice();}
llong size();
llong pos();
bool isOpened() {return stream.is_open();}
bool isEnd() {return stream.eof();}
llong pos() {if (!opened_) return -1; return ftell(fd);}
bool isEnd() {return (feof(fd) || ferror(fd));}
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;}
int read(void * read_to, int max_size) {if (!canRead()) return -1; return fread(read_to, max_size, 1, fd);}
int write(const void * data, int max_size) {if (!canWrite()) return -1; return fwrite(data, max_size, 1, fd);}
PIFile & writeToBinLog(ushort id, const void * data, int size) {if (!isWriteable()) return *this; writeBinary(id).writeBinary((ushort)size); write(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;}
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 & writeBinary(const char v) {write(&v, sizeof(v)); return *this;}
PIFile & writeBinary(const short v) {write(&v, sizeof(v)); return *this;}
PIFile & writeBinary(const int v) {write(&v, sizeof(v)); return *this;}
PIFile & writeBinary(const long v) {write(&v, sizeof(v)); return *this;}
PIFile & writeBinary(const uchar v) {write(&v, sizeof(v)); return *this;}
PIFile & writeBinary(const ushort v) {write(&v, sizeof(v)); return *this;}
PIFile & writeBinary(const uint v) {write(&v, sizeof(v)); return *this;}
PIFile & writeBinary(const ulong v) {write(&v, sizeof(v)); return *this;}
PIFile & writeBinary(const float v) {write(&v, sizeof(v)); return *this;}
PIFile & writeBinary(const double v) {write(&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) {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;}
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 <<(const char & v) {if (!isWriteable()) return *this; write(&v, 1); return *this;}
//PIFile & operator <<(const string & v) {write(v.c_str(), v.size()); return *this;}
PIFile & operator <<(const PIString & v) {if (!isWriteable()) return *this; write(v.data(), v.lengthAscii()); return *this;}
PIFile & operator <<(const PIByteArray & v) {if (!isWriteable()) return *this; write(v.data(), v.size()); return *this;}
PIFile & operator <<(short v) {if (!isWriteable()) return *this; fprintf(fd, "%hd", v); return *this;}
PIFile & operator <<(int v) {if (!isWriteable()) return *this; fprintf(fd, "%d", v); return *this;}
PIFile & operator <<(long v) {if (!isWriteable()) return *this; fprintf(fd, "%ld", v); return *this;}
PIFile & operator <<(uchar v) {if (!isWriteable()) return *this; fprintf(fd, "%c", v); return *this;}
PIFile & operator <<(ushort v) {if (!isWriteable()) return *this; fprintf(fd, "%hd", v); return *this;}
PIFile & operator <<(uint v) {if (!isWriteable()) return *this; fprintf(fd, "%d", v); return *this;}
PIFile & operator <<(ulong v) {if (!isWriteable()) return *this; fprintf(fd, "%ld", v); return *this;}
PIFile & operator <<(float v) {if (!isWriteable()) return *this; fprintf(fd, "%f", v); return *this;}
PIFile & operator <<(double v) {if (!isWriteable()) return *this; fprintf(fd, "%lf", 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;}
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;}
PIFile & operator >>(char & v) {if (!isWriteable()) return *this; fscanf(fd, "%hhn", &v); return *this;}
PIFile & operator >>(short & v) {if (!isWriteable()) return *this; fscanf(fd, "%hn", &v); return *this;}
PIFile & operator >>(int & v) {if (!isWriteable()) return *this; fscanf(fd, "%n", &v); return *this;}
PIFile & operator >>(long & v) {if (!isWriteable()) return *this; fscanf(fd, "%ln", &v); return *this;}
PIFile & operator >>(uchar & v) {if (!isWriteable()) return *this; fscanf(fd, "%hhn", &v); return *this;}
PIFile & operator >>(ushort & v) {if (!isWriteable()) return *this; fscanf(fd, "%hn", &v); return *this;}
PIFile & operator >>(uint & v) {if (!isWriteable()) return *this; fscanf(fd, "%n", &v); return *this;}
PIFile & operator >>(ulong & v) {if (!isWriteable()) return *this; fscanf(fd, "%ln", &v); return *this;}
PIFile & operator >>(float & v) {if (!isWriteable()) return *this; fscanf(fd, "%f", &v); return *this;}
PIFile & operator >>(double & v) {if (!isWriteable()) return *this; fscanf(fd, "%lf", &v); return *this;}
static PIFile openTemporary(PIIODevice::DeviceMode mode = PIIODevice::ReadWrite) {return PIFile(PIString(tmpnam(0)), mode);}
static bool isExists(const PIString & path);
static bool remove(const PIString & path) {return std::remove(path.data()) == 0;}
protected:
bool openDevice();
bool closeDevice();
private:
fstream stream;
PIString cpath;
PIFlags<Mode> cmode;
PIString strType(const PIIODevice::DeviceMode type) {switch (type) {case PIIODevice::ReadOnly: return "rb"; case WriteOnly: return "ab"; case ReadWrite: return "a+b";} return "rb";}
FILE * fd;
};