59 lines
1.2 KiB
C++
59 lines
1.2 KiB
C++
#include "pifile.h"
|
|
|
|
|
|
bool PIFile::open(const PIString & path_, Flags<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 string(buff);
|
|
}
|
|
|
|
|
|
int PIFile::size() {
|
|
int s, cp = stream.tellg();
|
|
stream.seekg(0, fstream::end);
|
|
s = stream.tellg();
|
|
stream.seekg(cp);
|
|
return s;
|
|
}
|
|
|
|
|
|
void PIFile::seek(int position) {
|
|
if (cmode[Read]) stream.seekg(position);
|
|
if (cmode[Write]) stream.seekp(position);
|
|
}
|
|
|
|
|
|
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;
|
|
}
|