Skip to content

Commit

Permalink
v2.2:
Browse files Browse the repository at this point in the history
- Added optional modes to restrict the mode in which you read/write the files
- Added method to seek an input position in the open file (normal and binary)
  • Loading branch information
illescasDaniel committed Jun 28, 2017
1 parent fec3dd3 commit 33d1b8f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
53 changes: 48 additions & 5 deletions File/File.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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;
}
}
}
Expand All @@ -55,33 +66,47 @@ 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 <typename Type, typename = typename std::enable_if<std::is_same<Type, std::string>::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());
}

template <typename Type, typename = typename std::enable_if<!std::is_same<Type, std::string>::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<char*>(&contentToWrite), sizeof(contentToWrite));
}

template <typename Type, typename = typename std::enable_if<!std::is_same<Type, std::string>::value>::type>
Type readFromBinary() {

if (mode == Mode::normal) { incompatibleMode(); return Type{}; }

Type readInput {};
open(std::ios::in | std::ios::binary);
fileStream.read(reinterpret_cast<char*>(&readInput), sizeof(readInput));
Expand All @@ -90,6 +115,9 @@ namespace evt {

template <typename Type, typename = typename std::enable_if<std::is_same<Type, std::string>::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<char> readTextChar(new char[size]);
Expand All @@ -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);
}
Expand All @@ -107,13 +138,18 @@ namespace evt {

template <typename Type>
void write(const Type& contentToWrite) {

if (mode == Mode::binary) { incompatibleMode(); return; }

open(std::ios::out | std::ios::in | std::ios::app);
fileStream << contentToWrite;
}

// 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;
Expand All @@ -123,6 +159,8 @@ namespace evt {

std::string getline() {

if (mode == Mode::binary) { incompatibleMode(); return std::string{}; }

std::string s;

open(std::ios::in);
Expand Down Expand Up @@ -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();
}

Expand Down
7 changes: 7 additions & 0 deletions File/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(2000);
//test2.seekPosition(1); // now it writes at position 1, but also changes the property "writeAtEnd"!
Expand All @@ -23,6 +27,9 @@ int main() {
cout << test2.readFromBinary<string>(4) << endl;
cout << test2.readFromBinary<float>() << endl;

test2.seekInputPosition(0);
cout << test2.readFromBinary<int>() << endl;

File::saveTextTo("3333.txt", "Hi, this is a teasdfst!");
cout << File::toString("3333.txt") << endl;

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 33d1b8f

Please sign in to comment.