#ifndef HELPERS_H #define HELPERS_H #include #include #include using namespace std; //file reading things inline string ReadTextFromFile(const char* fileName, string buffer) { std::ifstream file(fileName); unsigned char temporary; if (!file.is_open()) { std::cerr << "Could not open file for text input: " << fileName; return buffer; } while (getline(file, buffer)) buffer += buffer; file.close(); return buffer; } inline vector ReadFileAsBinary(const char* fileName, vector buffer) { try { //open the fstream in input mode, with binary mode std::fstream file(fileName, std::ios::in | std::ios::binary); unsigned char temporary; if (!file.is_open()) { std::cerr << "Could not open file for binary input: " << fileName; return buffer; } while (file >> temporary) { buffer.push_back(temporary); } file.close(); return buffer; } catch (const std::exception& ex) { std::cerr << "Exception during binary file input." << fileName << "was not successfully streamed to binary." << ex.what(); return buffer; } } //file writing things inline bool WriteTextToFile(const char* fileName, const string fileContents) { std::ofstream file(fileName); if(!file.is_open()) { std::cerr << "Could not open file: " << fileName; return false; } file << fileContents; file.close(); return true; } inline bool WriteFileFromBinary(const char* fileName, vector buffer) { std::ofstream file(fileName); int count = 0; if (!file.is_open()) { std::cerr << "Could not open file: " << fileName; return false; } for (auto const& x : buffer) { file << x; count = count + 1; if (count == 99) { file << "\n"; count = 0; } } file.close(); return true; } #endif