diff --git a/File/File.hpp b/File/File.hpp index 1541f7a..40fb7ab 100644 --- a/File/File.hpp +++ b/File/File.hpp @@ -30,10 +30,21 @@ namespace evt { class File { - + + public: + + enum Mode { + binary, + normal, + both + }; + + private: + std::fstream fileStream; std::ios_base::openmode inputOutputMode; std::string fileName; + Mode mode = Mode::both; void open(const std::ios_base::openmode inputOutputMode) { @@ -44,7 +55,7 @@ namespace evt { fileStream.open(fileName, inputOutputMode); if (fileStream.fail()) { - std::cout << "File couldn't be open" << std::endl; + std::cerr << "File couldn't be open" << std::endl; } } } @@ -55,19 +66,27 @@ namespace evt { } } + void incompatibleMode() { + std::cerr << "Error: Incompatible Mode" << std::endl; + } + public: // Only for binary files bool writeAtEnd = true; - File(const std::string& fileName) { + File(const std::string& fileName, const Mode mode = Mode::both) { this->fileName = fileName; + this->mode = mode; } /* BINARY */ template ::value>::type> void writeInBinary(const Type& text) { + + if (mode == Mode::normal) { incompatibleMode(); return; } + if (writeAtEnd) { open(std::ios::binary | std::ios::out | std::ios::in | std::ios::app); } else { open(std::ios::binary | std::ios::out | std::ios::in); } fileStream.write(text.c_str(), text.length()); @@ -75,6 +94,9 @@ namespace evt { template ::value>::type> void writeInBinary(Type contentToWrite) { + + if (mode == Mode::normal) { incompatibleMode(); return; } + if (writeAtEnd) { open(std::ios::binary | std::ios::out | std::ios::in | std::ios::app); } else { open(std::ios::binary | std::ios::out | std::ios::in); } fileStream.write(reinterpret_cast(&contentToWrite), sizeof(contentToWrite)); @@ -82,6 +104,9 @@ namespace evt { template ::value>::type> Type readFromBinary() { + + if (mode == Mode::normal) { incompatibleMode(); return Type{}; } + Type readInput {}; open(std::ios::in | std::ios::binary); fileStream.read(reinterpret_cast(&readInput), sizeof(readInput)); @@ -90,6 +115,9 @@ namespace evt { template ::value>::type> Type readFromBinary(std::size_t size) { + + if (mode == Mode::normal) { incompatibleMode(); return Type{}; } + open(std::ios::in | std::ios::binary); std::unique_ptr readTextChar(new char[size]); @@ -99,6 +127,9 @@ namespace evt { } void seekPosition(std::size_t offsetPosition, std::ios_base::seekdir position = std::ios::beg) { + + if (mode == Mode::normal) { incompatibleMode(); return; } + writeAtEnd = false; fileStream.seekp(offsetPosition, position); } @@ -107,6 +138,9 @@ namespace evt { template void write(const Type& contentToWrite) { + + if (mode == Mode::binary) { incompatibleMode(); return; } + open(std::ios::out | std::ios::in | std::ios::app); fileStream << contentToWrite; } @@ -114,6 +148,8 @@ namespace evt { // Reads text content word by word (?) std::string read() { + if (mode == Mode::binary) { incompatibleMode(); return std::string{}; } + std::string readContent; open(std::ios::in); fileStream >> readContent; @@ -123,6 +159,8 @@ namespace evt { std::string getline() { + if (mode == Mode::binary) { incompatibleMode(); return std::string{}; } + std::string s; open(std::ios::in); @@ -155,10 +193,15 @@ namespace evt { fileToWrite.write(text); } - /* EXTRA */ + /* OTHER */ + + void seekInputPosition(std::size_t offsetPosition, std::ios_base::seekdir position = std::ios::beg) { + fileStream.seekg(offsetPosition, position); + } - void open(const std::string& fileName) { + void open(const std::string& fileName, const Mode mode = Mode::both) { this->fileName = fileName; + this->mode = mode; this->close(); } diff --git a/File/main.cpp b/File/main.cpp index a295c29..fe8b3bd 100644 --- a/File/main.cpp +++ b/File/main.cpp @@ -13,6 +13,10 @@ int main() { cout << test2.getline() << endl; cout << test2.read() << endl; + test2.seekInputPosition(0); + + cout << test2.getline() << endl; + test2.open("222.bin"); test2.writeInBinary(2000); //test2.seekPosition(1); // now it writes at position 1, but also changes the property "writeAtEnd"! @@ -23,6 +27,9 @@ int main() { cout << test2.readFromBinary(4) << endl; cout << test2.readFromBinary() << endl; + test2.seekInputPosition(0); + cout << test2.readFromBinary() << endl; + File::saveTextTo("3333.txt", "Hi, this is a teasdfst!"); cout << File::toString("3333.txt") << endl; diff --git a/README.md b/README.md index f4cc362..78e80a1 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # File -[![Version](https://img.shields.io/badge/version-v2.1-green.svg)](https://github.com/illescasDaniel/File/releases) +[![Version](https://img.shields.io/badge/version-v2.2-green.svg)](https://github.com/illescasDaniel/File/releases) -File class using fstream to easily manage files in C++ +A wrapper around std::fstream to easily read and write content from/to files in C++. Main features: * Open files (read or write) without having to close them manually