80 lines
1.6 KiB
C++
80 lines
1.6 KiB
C++
#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();
|
|
}
|
|
|
|
|
|
PIString PIFile::readLine() {
|
|
char * buff = new char[4096];
|
|
stream.getline(buff, 4096);
|
|
return PIString(buff);
|
|
}
|
|
|
|
|
|
int PIFile::readAll(void * data) {
|
|
int cp = pos(), s = size();
|
|
stream.seekg(0);
|
|
stream.read((char * )data, s);
|
|
seek(cp);
|
|
return s;
|
|
}
|
|
|
|
|
|
PIByteArray PIFile::readAll() {
|
|
int s = size();
|
|
if (s < 0) return PIByteArray();
|
|
PIByteArray a(s);
|
|
s = readAll(a.data());
|
|
if (s >= 0) a.resize(s);
|
|
return a;
|
|
}
|
|
|
|
|
|
int PIFile::size() {
|
|
if (!stream.is_open()) return -1;
|
|
int s, cp = stream.tellg();
|
|
stream.seekg(0, fstream::end);
|
|
s = stream.tellg();
|
|
stream.seekg(cp, fstream::beg);
|
|
return s;
|
|
}
|
|
|
|
|
|
void PIFile::resize(int new_size, char fill_) {
|
|
int 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;
|
|
}
|
|
|
|
|
|
int PIFile::pos() {
|
|
if (cmode[Read]) return stream.tellg();
|
|
if (cmode[Write]) return stream.tellp();
|
|
return -1;
|
|
}
|
|
|
|
|
|
PIFile PIFile::openTemporary(PIFlags<PIFile::Mode> mode) {
|
|
char * rc;
|
|
rc = tmpnam(0);
|
|
return PIFile(PIString(rc), mode);
|
|
}
|