Browse Source

Fixed compilation for GCC

master
Alexander Memer 4 years ago
parent
commit
b67a69187d
  1. 7
      cmake-mingw.sh
  2. 125
      include/File.hpp
  3. 122
      src/File.cpp

7
cmake-mingw.sh

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
mkdir build
cd build
cmake -DVK_INCLUDE_DIR="/d/Coding/Vulkan/1.0.68.0/Include" \
-DVK_LIBRARY="/d/Coding/C++/vulkanengine/vulkan-1.dll" \
-DGLM_INCLUDE_DIR="/d/Coding/C++/Libraries/glm" \
-G "MinGW Makefiles" ..

125
include/File.hpp

@ -0,0 +1,125 @@ @@ -0,0 +1,125 @@
#pragma once
#include "PCH.hpp"
class File
{
private:
std::fstream file;
public:
File() { meta.size = 0; meta.fileMode = Mode(binary | in | out); }
~File() { close(); }
enum Mode
{
in = std::ios_base::in,
out = std::ios_base::out,
ate = std::ios_base::ate,
app = std::ios_base::app,
trunc = std::ios_base::trunc,
binary = std::ios_base::binary
};
struct FileMeta
{
std::string path;
std::string status;
U64 size;
Mode fileMode;
} meta;
std::fstream& fstream() { return file; }
bool create(std::string&& pPath, Mode pFileMode = (File::Mode)(File::binary | File::in | File::out));
bool create(Mode pFileMode = (File::Mode)(File::binary | File::in | File::out));
bool open(std::string&& pPath, Mode pFileMode = (File::Mode)(File::binary | File::in | File::out));
bool open(std::string& pPath, Mode pFileMode = (File::Mode)(File::binary | File::in | File::out));
bool open(Mode pFileMode = (File::Mode)(File::binary | File::in | File::out));
bool atEOF() {
return file.eof();
}
bool isOpen() {
return file.is_open();
}
void close() {
file.close();
}
bool checkWritable() {
return (meta.fileMode & out) != 0;
}
bool checkReadable() {
return (meta.fileMode & in) != 0;
}
S64 getSize() {
return meta.size;
}
void setPath(std::string pPath) {
meta.path = pPath;
}
// Writing functions
void write(void* data, U32 size);
template<class T>
void write(T val);
template<class T>
void writeArray(T* val, U32 length = 1);
void writeStr(std::string& string, bool includeTerminator = true, bool writeCapacity = true);
void writeStr(std::string& string, bool includeTerminator = true);
// Reading functions
void read(void* data, U32 size);
template<class T>
void read(T& val);
template<class T>
void readArray(T* val, U32 length = 1);
void read(std::string& string, U32 length);
void readFile(void* buffer);
void readStr(std::string& string, char delim = '\n', int size = -1);
char peekChar();
char pullChar();
};
template<class T>
void File::read(T & val)
{
file.read((char*)&val, sizeof(T));
}
template<class T>
void File::readArray(T * val, U32 length)
{
file.read((char*)val, sizeof(T)*length);
}
template<class T>
inline void File::write(T val)
{
file.write((char*)&val, sizeof(T));
}
template<class T>
void File::writeArray(T * val, U32 length)
{
file.write((char*)val, sizeof(T)*length);
}

122
src/File.cpp

@ -0,0 +1,122 @@ @@ -0,0 +1,122 @@
#include "PCH.hpp"
#include "File.hpp"
bool File::create(std::string && pPath, Mode pFileMode)
{
if (pPath.length() > 127)
return false;
meta.path = pPath;
meta.fileMode = pFileMode;
return create(meta.fileMode);
}
bool File::create(Mode pFileMode)
{
if (meta.path.length() == 0)
return false;
meta.fileMode = pFileMode;
file.open(meta.path.c_str(), (std::_Ios_Openmode)meta.fileMode);
}
bool File::open(std::string && pPath, Mode pFileMode)
{
return open(pPath, pFileMode);
}
bool File::open(std::string & pPath, Mode pFileMode)
{
if (pPath.length() > 127)
return false;
meta.path = pPath;
meta.fileMode = pFileMode;
return open(meta.fileMode);
}
bool File::open(Mode pFileMode)
{
if (meta.path.length() == 0)
return false;
meta.fileMode = pFileMode;
file.open(meta.path.c_str(), (std::ios_base::openmode)meta.fileMode);
if (file.good() && file.is_open() && !file.bad())
{
file.seekg(0, std::ios_base::end);
meta.size = (size_t)file.tellg();
file.seekg(0);
return true;
}
else
return false;
}
void File::write(void * data, U32 size)
{
file.write((char*)data, size);
}
void File::writeStr(std::string & string, bool includeTerminator, bool writeCapacity)
{
if (!writeCapacity)
file.write(string.c_str(), string.length() + (includeTerminator ? 1 : 0));
else
file.write(string.c_str(), string.size() + (includeTerminator ? 1 : 0));
}
void File::writeStr(std::string & string, bool includeTerminator)
{
file.write(string.c_str(), string.length() + (includeTerminator ? 1 : 0));
}
void File::read(void * data, U32 size)
{
file.read((char*)data, size);
}
void File::read(std::string & string, U32 length)
{
string.resize(length);
file.read((char*)string.c_str(), length);
}
void File::readStr(std::string & string, char delim, int size)
{
if (size == -1)
{
char lastChar[2];
lastChar[1] = 0;
file.read(lastChar, 1);
while (*lastChar != delim && *lastChar != '\0' && !file.eof())
{
string.append(lastChar);
file.read(lastChar, 1);
}
}
else
{
string.resize(size);
file.read(const_cast<char*>(string.c_str()), size);
}
}
void File::readFile(void * buffer)
{
readArray((char*)buffer, meta.size);
}
char File::peekChar()
{
return file.peek();
}
char File::pullChar()
{
char pull;
file.read(&pull, 1);
return pull;
}
Loading…
Cancel
Save