summaryrefslogtreecommitdiffhomepage
path: root/file.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2020-03-28 22:27:01 +0100
committerRoland Reichwein <mail@reichwein.it>2020-03-28 22:27:01 +0100
commitee18ec019ef6f0ef9d7cd3b4cf0314291814cab0 (patch)
tree5f23e0bce52e24c3c04825ad008ec74a5e722a1c /file.cpp
parent7fdcbd50a35c17e8ea7d88fbcaa3080ee44351b3 (diff)
Add ELF handling (WIP)
Diffstat (limited to 'file.cpp')
-rw-r--r--file.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/file.cpp b/file.cpp
new file mode 100644
index 0000000..5e17d1a
--- /dev/null
+++ b/file.cpp
@@ -0,0 +1,46 @@
+#include "file.h"
+
+#include "minicc.h"
+
+#include <fstream>
+
+namespace fs = std::filesystem;
+
+std::vector<uint8_t> File::getFile(const fs::path& filename)
+{
+ std::ifstream file(filename.string(), std::ios::in | std::ios::binary | std::ios::ate);
+
+ if (file.is_open()) {
+ std::ifstream::pos_type fileSize = file.tellg();
+ file.seekg(0, std::ios::beg);
+
+ std::vector<uint8_t> bytes(fileSize, 0);
+ file.read(reinterpret_cast<char*>(bytes.data()), fileSize);
+
+ return bytes;
+
+ } else {
+ throw std::runtime_error("Opening "s + filename.string() + " for reading");
+ }
+}
+
+void File::setFile(const fs::path& filename, const std::string& s)
+{
+ File::setFile(filename, s.data(), s.size());
+}
+
+void File::setFile(const fs::path& filename, const char* data, size_t size)
+{
+ std::ofstream file(filename.string(), std::ios::out | std::ios::binary);
+ if (file.is_open()) {
+ file.write(data, size);
+ } else {
+ throw std::runtime_error("Opening "s + filename.string() + " for writing");
+ }
+}
+
+void File::setFile(const fs::path& filename, const std::vector<uint8_t>& data)
+{
+ File::setFile(filename, reinterpret_cast<const char*>(data.data()), data.size());
+}
+