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:
88
pifile.cpp
Normal file → Executable file
88
pifile.cpp
Normal file → Executable 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,35 +20,46 @@
|
||||
#include "pifile.h"
|
||||
|
||||
|
||||
bool PIFile::open(const PIString & path_, PIFlags<Mode> mode_) {
|
||||
cpath = path_;
|
||||
cmode = mode_;
|
||||
string st = cpath.stdString();
|
||||
if (cmode[New]) {
|
||||
stream.open(st.c_str(), fstream::in | fstream::out | fstream::trunc | fstream::binary);
|
||||
stream.close();
|
||||
cmode &= ~New;
|
||||
stream.open(st.c_str(), (fstream::openmode)(int)cmode | fstream::binary);
|
||||
} else stream.open(st.c_str(), (fstream::openmode)(int)cmode | fstream::binary);
|
||||
return isOpened();
|
||||
bool PIFile::openDevice() {
|
||||
if (opened_) close();
|
||||
if (path_.isEmpty()) return false;
|
||||
//cout << "fopen " << path_.data() << ": " << strType(mode_).data() << endl;
|
||||
fd = fopen(path_.data(), strType(mode_).data());
|
||||
opened_ = (fd != 0);
|
||||
seekToBegin();
|
||||
return opened_;
|
||||
}
|
||||
|
||||
|
||||
bool PIFile::closeDevice() {
|
||||
if (!opened_) return true;
|
||||
return (fclose(fd) != 0);
|
||||
}
|
||||
|
||||
|
||||
PIString PIFile::readLine() {
|
||||
char * buff = new char[4096];
|
||||
stream.getline(buff, 4096);
|
||||
return PIString(buff);
|
||||
PIString str;
|
||||
if (!opened_) return str;
|
||||
char cc;
|
||||
int cp = 0;
|
||||
while (!isEnd() && cp < 4095) {
|
||||
cc = char(fgetc(fd));
|
||||
if (cc == '\n') break;
|
||||
str.push_back(cc);
|
||||
}
|
||||
//cout << "readline: " << buff << endl;
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
llong PIFile::readAll(void * data) {
|
||||
llong cp = pos(), s = size(), i = -1;
|
||||
stream.seekg(0);
|
||||
seekToBegin();
|
||||
if (s < 0) {
|
||||
while (!stream.eof())
|
||||
stream.read(&(((char*)data)[++i]), 1);
|
||||
while (!isEnd())
|
||||
read(&(((char*)data)[++i]), 1);
|
||||
} else
|
||||
stream.read((char * )data, s);
|
||||
read((char * )data, s);
|
||||
seek(cp);
|
||||
return s;
|
||||
}
|
||||
@@ -56,14 +67,11 @@ llong PIFile::readAll(void * data) {
|
||||
|
||||
PIByteArray PIFile::readAll(bool forceRead) {
|
||||
llong cp = pos(), s = size();
|
||||
char c;
|
||||
PIByteArray a;
|
||||
if (s < 0) {
|
||||
if (!forceRead) return a;
|
||||
while (!stream.eof()) {
|
||||
stream.read(&c, 1);
|
||||
a.push_back(c);
|
||||
}
|
||||
while (!isEnd())
|
||||
a.push_back(readChar());
|
||||
seek(cp);
|
||||
return a;
|
||||
}
|
||||
@@ -75,12 +83,11 @@ PIByteArray PIFile::readAll(bool forceRead) {
|
||||
|
||||
|
||||
llong PIFile::size() {
|
||||
if (!stream.is_open()) return -1;
|
||||
llong s, cp = stream.tellg();
|
||||
stream.seekg(0, fstream::end);
|
||||
s = stream.tellg();
|
||||
stream.seekg(cp, fstream::beg);
|
||||
stream.clear();
|
||||
if (!opened_) return -1;
|
||||
llong s, cp = pos();
|
||||
seekToEnd();
|
||||
s = pos();
|
||||
seek(cp);
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -91,23 +98,18 @@ void PIFile::resize(llong new_size, char fill_) {
|
||||
if (ds > 0) {
|
||||
char * buff = new char[ds];
|
||||
memset(buff, fill_, ds);
|
||||
stream.write(buff, ds);
|
||||
delete buff;
|
||||
write(buff, ds);
|
||||
delete[] buff;
|
||||
return;
|
||||
}
|
||||
cout << "[PIFile] Downsize is not support yet :-(" << endl;
|
||||
piCout << "[PIFile] Downsize is not support yet :-(" << endl;
|
||||
}
|
||||
|
||||
|
||||
llong PIFile::pos() {
|
||||
if (cmode[Write]) return stream.tellp();
|
||||
if (cmode[Read]) return stream.tellg();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
PIFile PIFile::openTemporary(PIFlags<PIFile::Mode> mode) {
|
||||
char * rc;
|
||||
rc = tmpnam(0);
|
||||
return PIFile(PIString(rc), mode);
|
||||
bool PIFile::isExists(const PIString & path) {
|
||||
FILE * f = fopen(PIString(path).data(), "r");
|
||||
bool ok = (f != 0);
|
||||
if (ok) fclose(f);
|
||||
return ok;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user