#include "pifile.h" bool PIFile::open(const PIString & path_, PIFlags 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(); } PIString PIFile::readLine() { char * buff = new char[4096]; stream.getline(buff, 4096); return PIString(buff); } llong PIFile::readAll(void * data) { llong cp = pos(), s = size(); stream.seekg(0); stream.read((char * )data, s); seek(cp); return s; } PIByteArray PIFile::readAll() { llong s = size(); if (s < 0) return PIByteArray(); PIByteArray a(s); s = readAll(a.data()); if (s >= 0) a.resize(s); return a; } 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); return s; } void PIFile::resize(llong new_size, char fill_) { llong ds = new_size - size(); if (ds == 0) return; if (ds > 0) { char * buff = new char[ds]; memset(buff, fill_, ds); stream.write(buff, ds); delete buff; return; } cout << "[PIFile] Downsize is not support yet :-(" << endl; } llong PIFile::pos() { if (cmode[Read]) return stream.tellg(); if (cmode[Write]) return stream.tellp(); return -1; } PIFile PIFile::openTemporary(PIFlags mode) { char * rc; rc = tmpnam(0); return PIFile(PIString(rc), mode); }