Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Object-oriented Programming
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
uni-projects-a6fb61db10a93801065d34d19fb6d7166511e58d.tar
uni-projects-a6fb61db10a93801065d34d19fb6d7166511e58d.tar.gz
uni-projects-a6fb61db10a93801065d34d19fb6d7166511e58d.zip
Parent4a530b9ae9f50ce0e117931933db3a11285f29af (diff)
Made naming and slight formatting adjustments
5 files changed, 63 insertions, 58 deletions
-rw-r--r--Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp6
-rw-r--r--Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.h4
-rw-r--r--Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.cpp (renamed from Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.cpp)63
-rw-r--r--Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.h (renamed from Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.h)18
-rw-r--r--Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/source.cpp30
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp
index 42a03c6..bc2bee6 100644
--- a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp
@@ -1,6 +1,8 @@
#include "Bytex.h"
-char Bytex::digitToHexChar(unsigned int digit) {
+/* Private functions */
+
+char Bytex::digitToHexChar(int digit) {
if (digit < 10)
return '0' + digit;
return 'A' + digit - 10;
@@ -22,6 +24,8 @@ void Bytex::hexValToByte() {
byteVal = hexCharToNum(hexVal[0]) * 16 + hexCharToNum(hexVal[1]);
}
+/* Public functions */
+
Bytex::Bytex() : Bytex('\0')
{}
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.h b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.h
index fc39e7a..67f3c57 100644
--- a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.h
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.h
@@ -1,8 +1,8 @@
class Bytex {
- char hexVal[2];
char byteVal;
+ char hexVal[2]; // Normal bytes are up to 255, which is FF in Hex
- char digitToHexChar(unsigned int digit);
+ char digitToHexChar(int digit);
int hexCharToNum(char hexChar);
void byteValToHex();
void hexValToByte();
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.cpp
index f553bbe..63979bc 100644
--- a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.cpp
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.cpp
@@ -1,9 +1,9 @@
-#include "File.h"
+#include "HexEditor.h"
/* Helper functions */
size_t fileSize(std::ifstream& file) {
- int currIndex = file.tellg();
+ size_t currIndex = file.tellg();
file.seekg(0, std::ios::end);
size_t size = file.tellg();
@@ -12,13 +12,13 @@ size_t fileSize(std::ifstream& file) {
return size;
}
-/* Private File functions */
+/* Private functions */
-void File::resizeBuffer() {
+void HexEditor::resizeBuffer() {
size_t newLen = (length == 0) ? 8 : length << 1;
Bytex* temp = new Bytex[newLen];
- for (int i = 0; i < length; i++)
+ for (size_t i = 0; i < length; i++)
temp[i] = fileBuffer[i];
if (length != 0) delete[] fileBuffer;
@@ -26,16 +26,16 @@ void File::resizeBuffer() {
length = newLen;
}
-char File::bytexToReadableChar(Bytex bytex) const {
+char HexEditor::bytexToReadableChar(const Bytex& bytex) const {
char b = bytex.getByte();
if ((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z'))
- return bytex.getByte();
+ return b;
return '.';
}
-/* Public File functions */
+/* Public functions */
-File::File(const char* filePath) {
+HexEditor::HexEditor(const char* filePath) {
std::ifstream inFile(filePath);
if (!inFile.is_open()) {
std::cout << "Error opening file" << std::endl;
@@ -45,23 +45,37 @@ File::File(const char* filePath) {
size_t size = fileSize(inFile);
length = count = 0;
- for (int i = 0; i < size; i++)
+ for (size_t i = 0; i < size; i++)
add(inFile.get());
inFile.close();
size_t pathLen = strlen(filePath) + 1;
originalFilePath = new char[pathLen];
- for (int i = 0; i < pathLen; i++)
- originalFilePath[i] = filePath[i];
+ strcpy(originalFilePath, filePath);
}
-File::~File() {
+HexEditor::~HexEditor() {
delete[] fileBuffer;
delete[] originalFilePath;
}
-void File::viewFile() const {
+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++) {
@@ -75,28 +89,13 @@ void File::viewFile() const {
std::cout << std::endl;
}
-void File::change(int index, Bytex bytex) {
- fileBuffer[index] = bytex;
-}
-
-void File::add(Bytex bytex) {
- if (count == length)
- resizeBuffer();
-
- fileBuffer[count++] = bytex;
-}
-
-void File::remove() {
- fileBuffer[count--] = '\0';
-}
-
-void File::save() const {
+void HexEditor::save() const {
saveAs(originalFilePath);
}
-void File::saveAs(const char* filePath) const {
+void HexEditor::saveAs(const char* filePath) const {
std::ofstream outFile(filePath);
- for (int i = 0; i < count; i++)
+ for (size_t i = 0; i < count; i++)
outFile.put(fileBuffer[i].getByte());
outFile.close();
}
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.h b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.h
index 753e734..aa56041 100644
--- a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.h
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/HexEditor.h
@@ -1,27 +1,29 @@
#include <iostream>
#include <fstream>
#include <cstring>
+#pragma warning(disable:4996)
+
#include "Bytex.h"
-class File {
+class HexEditor {
Bytex* fileBuffer;
size_t count;
size_t length;
char* originalFilePath; // There are no actual restrictions on path lengths in Linux
- char bytexToReadableChar(Bytex bytex) const;
-
+ char bytexToReadableChar(const Bytex& bytex) const;
void resizeBuffer();
public:
- File(const char* filePath);
- ~File();
+ HexEditor(const char* filePath);
+ ~HexEditor();
- void viewFile() const;
- void change(int index, Bytex bytex);
- void add(Bytex bytex);
+ void change(size_t index, const Bytex& bytex);
+ void add(const Bytex& bytex);
void remove();
+
+ void viewFile() const;
void save() const;
void saveAs(const char* filePath) const;
};
diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/source.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/source.cpp
index 90baaee..60ea3e2 100644
--- a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/source.cpp
+++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/source.cpp
@@ -1,4 +1,4 @@
-#include "File.h"
+#include "HexEditor.h"
void createFile() {
int x = 1816466293;
@@ -9,21 +9,21 @@ void createFile() {
int main() {
createFile();
- File myFile("myData.dat");
- myFile.viewFile();
- myFile.change(1, "68"); // 68x0 h (value in hex)
- myFile.change(2, 68); // 68 D (value in decimal)
- myFile.viewFile();
- myFile.remove();
- myFile.viewFile();
- myFile.save();
+ HexEditor hexEdit("myData.dat");
+ hexEdit.viewFile();
+ hexEdit.change(1, "68"); // 68x0 h (value in hex)
+ hexEdit.change(2, 68); // 68 D (value in decimal)
+ hexEdit.viewFile();
+ hexEdit.remove();
+ hexEdit.viewFile();
+ hexEdit.save();
- File myFileCopy("myData.dat");
+ HexEditor hexEditCopy("myData.dat");
- myFile.add(46); // 46 . (value in decimal)
- myFile.add("15"); // 15x0 (value in hex)
- myFile.change(0, "4B"); // 4Bx0 K (value in hex)
+ hexEdit.add(46); // 46 . (value in decimal)
+ hexEdit.add("15"); // 15x0 (value in hex)
+ hexEdit.change(0, "4B"); // 4Bx0 K (value in hex)
- myFileCopy.viewFile();
- myFile.viewFile();
+ hexEditCopy.viewFile();
+ hexEdit.viewFile();
}