#ifndef HELPERS_HPP #define HELPERS_HPP #include #include // Read text from a file inline char* ReadTextFromFile(const char* fileName, char* buffer) { try { std::ifstream file(fileName); if (!file.is_open()) { std::cerr << "Could not open file for text input: " << fileName << std::endl; return buffer; } file.seekg(0, std::ios::end); std::streamsize size = file.tellg(); file.seekg(0, std::ios::beg); delete[] buffer; buffer = nullptr; buffer = new char[size + 1]; file.read(buffer, size); buffer[size] = '\0'; // Null-terminate the string file.close(); return buffer; } catch (const std::exception& ex) { std::cerr << "Exception during text file input: " << fileName << " was not successfully streamed to text. " << ex.what() << std::endl; return nullptr; } } // Read binary data from a file inline char* ReadFileAsBinary(const char* fileName, char* buffer, size_t& size) { try { std::ifstream file(fileName, std::ios::binary | std::ios::ate); if (!file.is_open()) { std::cerr << "Could not open file for binary input: " << fileName << std::endl; return nullptr; } size = file.tellg(); file.seekg(0, std::ios::beg); delete[] buffer; buffer = new char[size + 1]; file.read(buffer, size); file.close(); buffer[size] = '\0'; return buffer; } catch (const std::exception& ex) { std::cerr << "Exception during binary file input: " << fileName << " was not successfully streamed to binary. " << ex.what() << std::endl; return nullptr; } } //file writing things inline bool WriteTextToFile(const char* fileName, const char* fileContents) { std::ofstream file(fileName); if (!file.is_open()) { std::cerr << "Could not open file for text output: " << fileName << std::endl; return false; } file << fileContents; file.close(); return true; } // Write binary data to a file inline bool WriteFileFromBinary(const char* fileName, const char* buffer, size_t size) { try { std::ofstream file(fileName, std::ios::out | std::ios::binary); if (!file.is_open()) { std::cerr << "Could not open file for binary output: " << fileName << std::endl; return false; } file.write(buffer, size); file.close(); return true; } catch (const std::exception& ex) { std::cerr << "Exception during binary file output: " << fileName << ". " << ex.what() << std::endl; return false; } } #endif