FileIO/fileio.cpp

203 lines
4.6 KiB
C++

#include <fileio/fileio.h>
bool marstr::fileio::doesFileExist(std::string filename)
{
std::ifstream f(filename.c_str());
return f.good();
}
bool marstr::fileio::doesFolderExist(std::string fldname)
{
DIR *pDir;
bool bExists = false;
pDir = opendir (fldname.c_str());
if (pDir != NULL)
{
bExists = true;
(void) closedir (pDir);
}
return bExists;
}
void marstr::fileio::mkdir(std::string fldname)
{
std::string t = "mkdir -p " + fldname;
system(t.c_str());
}
void marstr::fileio::compressFile(std::string source, std::string dest)
{
FILE *infile = fopen(source.c_str(), "rb");
gzFile outfile = gzopen(dest.c_str(), "wb");
//if (!infile || !outfile) return -1;
char inbuffer[128];
int num_read = 0;
unsigned long total_read = 0, total_wrote = 0;
while ((num_read = fread(inbuffer, 1, sizeof(inbuffer), infile)) > 0) {
total_read += num_read;
gzwrite(outfile, inbuffer, num_read);
}
fclose(infile);
gzclose(outfile);
}
void marstr::fileio::decompressFile(std::string source, std::string dest)
{
gzFile inFileZ = gzopen(source.c_str(), "rb");
if (inFileZ == NULL) {
printf("Error: Failed to gzopen %s\n", source.c_str());
exit(0);
}
unsigned char unzipBuffer[8192];
unsigned int unzippedBytes;
std::vector<unsigned char> unzippedData;
while (true) {
unzippedBytes = gzread(inFileZ, unzipBuffer, 8192);
if (unzippedBytes > 0) {
unzippedData.insert(unzippedData.end(), unzipBuffer, unzipBuffer + unzippedBytes);
} else {
break;
}
}
gzclose(inFileZ);
// Write the decompressed data
openFileForWriting(dest);
for (int i=0; i<unzippedData.size(); i++)
{ writeBinary(unzippedData[i]); }
closeFileForWriting();
}
void marstr::fileio::openFileForWriting(std::string fn)
{ out.open (fn, std::ofstream::binary | std::ios::out); }
void marstr::fileio::openFileForReading(std::string fn)
{ in.open (fn, std::ifstream::binary | std::ios::in); }
std::string marstr::fileio::readPlainText(std::string fn)
{
in.open(fn);
std::string txt;
while (!in.eof()) { in >> txt; }
closeFileForReading();
return txt;
}
void marstr::fileio::jumpToPositionInFile(int seekpos)
{ in.seekg(0, in.beg+seekpos); }
void marstr::fileio::jumpToBeginningOfFile()
{ in.seekg(0, in.beg); }
void marstr::fileio::closeFileForWriting()
{ out.close(); }
void marstr::fileio::closeFileForReading()
{ in.close(); }
void marstr::fileio::writeShort(short s) { out.write((char *)&s, sizeof(short)); }
void marstr::fileio::writeInt(int i) { out.write ((char *)&i, sizeof(int)); }
void marstr::fileio::writeUInt(unsigned int i) { out.write ((char *)&i, sizeof(unsigned int)); }
void marstr::fileio::writeLong(long l) { out.write ((char *)&l, sizeof(long)); }
void marstr::fileio::writeULong(unsigned long l) { out.write ((char *)&l, sizeof(unsigned long)); }
void marstr::fileio::writeFloat(float f) { out.write ((char *)&f, sizeof(float)); }
void marstr::fileio::writeDouble(double d) { out.write ((char *)&d, sizeof(double)); }
void marstr::fileio::writeString(std::string str)
{
// Length of string
int len = str.length();
// Write length
writeInt(len);
// Write string itself
//out.write( str.c_str(), str.size() );
out.write(str.c_str(), len);
}
void marstr::fileio::writeStringAsPlainText(std::string str)
{ int len = str.length(); out.write(str.c_str(), len); }
void marstr::fileio::writeBinary(unsigned char c)
{ out.write(c, sizeof(unsigned char)); }
short marstr::fileio::readShort()
{
short val;
in.read((char*)&val, sizeof(short));
return val;
}
unsigned short marstr::fileio::readUShort()
{
unsigned short val;
in.read((char*)&val, sizeof(unsigned short));
return val;
}
int marstr::fileio::readInt()
{
int val;
in.read((char*)&val, sizeof(int));
return val;
}
unsigned int marstr::fileio::readUInt()
{
unsigned int val;
in.read((char*)&val, sizeof(unsigned int));
return val;
}
long marstr::fileio::readLong()
{
long val;
in.read((char*)&val, sizeof(long));
return val;
}
unsigned long marstr::fileio::readULong()
{
unsigned long val;
in.read((char*)&val, sizeof(unsigned long));
return val;
}
float marstr::fileio::readFloat()
{
float val;
in.read((char*)&val, sizeof(float));
return val;
}
double marstr::fileio::readDouble()
{
double val;
in.read((char*)&val, sizeof(double));
return val;
}
std::string marstr::fileio::readString()
{
// Read string length:
int len = readInt();
// Create buffer:
char* temp = new char[len];
// Read string:
in.read(temp, len);
std::string res = temp;
// Return it
return res;
}
void marstr::fileio::deleteFile(std::string filename)
{ remove(filename.c_str()); }
int marstr::fileio::CurrentStreamPosition()
{ return in.tellg(); }