Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming / Solutions Sem. 03 / ex4 / HexEditor.cpp
diff options
context:
space:
mode:
Commita6fb61db10a93801065d34d19fb6d7166511e58d (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-03-31 09:37:42 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-03-31 09:37:42 +0300
Filesec3db1d6147713c184c9ba23fd9dd866c599edcc /Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.cpp
uni-projects-a6fb61db10a93801065d34d19fb6d7166511e58d.tar
uni-projects-a6fb61db10a93801065d34d19fb6d7166511e58d.tar.gz
uni-projects-a6fb61db10a93801065d34d19fb6d7166511e58d.zip
Parent4a530b9ae9f50ce0e117931933db3a11285f29af (diff)
Made naming and slight formatting adjustments
1 files changed, 101 insertions, 0 deletions
-rw-r--r--Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.cpp101
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.cpp
new file mode 100644
index 0000000..63979bc
--- /dev/null
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.cpp
@@ -0,0 +1,101 @@
+#include "HexEditor.h"
+
+/* Helper functions */
+
+size_t fileSize(std::ifstream& file) {
+ size_t currIndex = file.tellg();
+ file.seekg(0, std::ios::end);
+
+ size_t size = file.tellg();
+ file.seekg(currIndex, std::ios::beg);
+
+ return size;
+}
+
+/* Private functions */
+
+void HexEditor::resizeBuffer() {
+ size_t newLen = (length == 0) ? 8 : length << 1;
+
+ Bytex* temp = new Bytex[newLen];
+ for (size_t i = 0; i < length; i++)
+ temp[i] = fileBuffer[i];
+
+ if (length != 0) delete[] fileBuffer;
+ fileBuffer = temp;
+ length = newLen;
+}
+
+char HexEditor::bytexToReadableChar(const Bytex& bytex) const {
+ char b = bytex.getByte();
+ if ((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z'))
+ return b;
+ return '.';
+}
+
+/* Public functions */
+
+HexEditor::HexEditor(const char* filePath) {
+ std::ifstream inFile(filePath);
+ if (!inFile.is_open()) {
+ std::cout << "Error opening file" << std::endl;
+ return;
+ }
+
+ size_t size = fileSize(inFile);
+ length = count = 0;
+
+ for (size_t i = 0; i < size; i++)
+ add(inFile.get());
+
+ inFile.close();
+
+ size_t pathLen = strlen(filePath) + 1;
+ originalFilePath = new char[pathLen];
+ strcpy(originalFilePath, filePath);
+}
+
+HexEditor::~HexEditor() {
+ delete[] fileBuffer;
+ delete[] originalFilePath;
+}
+
+void HexEditor::change(size_t index, const Bytex& bytex) {
+ fileBuffer[index] = bytex;
+}
+
+void HexEditor::add(const Bytex& bytex) {
+ if (count == length)
+ resizeBuffer();
+
+ fileBuffer[count++] = bytex;
+}
+
+void HexEditor::remove() {
+ fileBuffer[count--] = Bytex('\0');
+}
+
+void HexEditor::viewFile() const {
+ char buffer[2];
+
+ for (size_t i = 0; i < count; i++) {
+ fileBuffer[i].getHex(buffer);
+ std::cout << buffer << " ";
+ }
+ std::cout << std::endl;
+
+ for (size_t i = 0; i < count; i++)
+ std::cout << bytexToReadableChar(fileBuffer[i]) << " ";
+ std::cout << std::endl;
+}
+
+void HexEditor::save() const {
+ saveAs(originalFilePath);
+}
+
+void HexEditor::saveAs(const char* filePath) const {
+ std::ofstream outFile(filePath);
+ for (size_t i = 0; i < count; i++)
+ outFile.put(fileBuffer[i].getByte());
+ outFile.close();
+}