]> marstr Code Repo - FileIO/commitdiff
Initial commit with recently added functions
authorMarStr <marcus@marstr.online>
Fri, 5 Jul 2024 13:23:48 +0000 (15:23 +0200)
committerMarStr <marcus@marstr.online>
Fri, 5 Jul 2024 13:23:48 +0000 (15:23 +0200)
fileio.cpp [new file with mode: 0644]
fileio.h [new file with mode: 0644]

diff --git a/fileio.cpp b/fileio.cpp
new file mode 100644 (file)
index 0000000..73f5db9
--- /dev/null
@@ -0,0 +1,202 @@
+
+#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(); }
diff --git a/fileio.h b/fileio.h
new file mode 100644 (file)
index 0000000..0cc6698
--- /dev/null
+++ b/fileio.h
@@ -0,0 +1,95 @@
+
+// A utilitarian class to handle file operations in my C++ projects.
+
+#ifndef MARSTR_FILEIO
+#define MARSTR_FILEIO
+
+#include <cstdlib>
+#include <stdlib.h>
+#include <stdio.h>
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <vector>
+#include <zlib.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+namespace marstr
+{
+       class fileio
+       {
+       public:
+               // The file to write out to
+               std::ofstream out;
+               // The file to read in from
+               std::ifstream in;
+
+               // Check if a file exists prior to opening it or writing to it
+               bool doesFileExist(std::string filename);
+               // Check if a folder exists
+               bool doesFolderExist(std::string fldname);
+               // Create a folder
+               void mkdir(std::string fldname);
+
+               // Compresses a file
+               void compressFile(std::string source, std::string dest);
+               // Decompresses a file
+               void decompressFile(std::string source, std::string dest);
+
+               // Open a binary file for writing
+               void openFileForWriting(std::string fn);
+
+               // Open a binary file for reading
+               void openFileForReading(std::string fn);
+               
+               // Reads a plain text file back to back and provides everything in a
+               // std::string object
+               std::string readPlainText(std::string fn);
+
+               // Jumps to the specified position in the file opened for reading. We mostly
+               // use this to quickly jump to positions in an octree file and read only
+               // what is needed for rendering.
+               void jumpToPositionInFile(int seekpos);
+
+               // Jumps back to the very beginning of the file
+               void jumpToBeginningOfFile();
+
+               // Close the file to write
+               void closeFileForWriting();
+               // And for reading
+               void closeFileForReading();
+
+               // Writes all kinds of things we need
+               void writeShort(short s);
+               void writeInt(int i);
+               void writeUInt(unsigned int i);
+               void writeLong(long l);
+               void writeULong(unsigned long l);
+               void writeFloat(float f);
+               void writeDouble(double d);
+               void writeString(std::string str);
+               void writeStringAsPlainText(std::string str);
+               void writeBinary(unsigned char c);
+
+               // Reads back the same data types
+               short readShort();
+               unsigned short readUShort();
+               int readInt();
+               unsigned int readUInt();
+               long readLong();
+               unsigned long readULong();
+               float readFloat();
+               double readDouble();
+               std::string readString();
+
+               // Deletes the specified file
+               void deleteFile(std::string filename);
+
+               // Get current position in file stream
+               int CurrentStreamPosition();
+       };
+}
+#endif