aboutsummaryrefslogtreecommitdiff
path: root/week06/Exercise08.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'week06/Exercise08.cpp')
-rw-r--r--week06/Exercise08.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/week06/Exercise08.cpp b/week06/Exercise08.cpp
new file mode 100644
index 0000000..bcd4339
--- /dev/null
+++ b/week06/Exercise08.cpp
@@ -0,0 +1,53 @@
+#include <fstream>
+
+struct FileBuffer {
+private:
+ char* data;
+ size_t size;
+
+ void free() {
+ delete[] data;
+ }
+ void copyFrom(const FileBuffer& other) {
+ this->size = other.size;
+ this->data = new char[size];
+ for (int i = 0; i < size; i++) {
+ this->data[i] = other.data[i];
+ }
+ }
+
+public:
+ FileBuffer(const char* fileName) {
+ std::ifstream file(fileName);
+ if (!file.is_open()) {
+ throw "Couldn't open file!";
+ }
+
+ file.seekg(0, std::ios::end);
+ size = file.tellg();
+ data = new char[size];
+
+ file.seekg(0, std::ios::beg);
+ file.read(data, size);
+
+ file.close();
+ }
+
+ FileBuffer() {
+ data = nullptr;
+ size = 0;
+ }
+ ~FileBuffer() {
+ free();
+ }
+ FileBuffer(const FileBuffer& other) {
+ copyFrom(other);
+ }
+ FileBuffer& operator=(const FileBuffer& other) {
+ if (this != &other) {
+ free();
+ copyFrom(other);
+ }
+ return *this;
+ }
+};