diff options
Solved dimitriev hw (sem 3, ex4)
5 files changed, 221 insertions, 0 deletions
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 new file mode 100644 index 0000000..42a03c6 --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.cpp @@ -0,0 +1,46 @@ +#include "Bytex.h" + +char Bytex::digitToHexChar(unsigned int digit) { + if (digit < 10) + return '0' + digit; + return 'A' + digit - 10; +} + +int Bytex::hexCharToNum(char hexChar) { + if (hexChar <= '9') + return hexChar - '0'; + return hexChar - 'A' + 10; +} + +void Bytex::byteValToHex() { + int front = byteVal / 16, back = byteVal % 16; + hexVal[0] = digitToHexChar(front); + hexVal[1] = digitToHexChar(back); +} + +void Bytex::hexValToByte() { + byteVal = hexCharToNum(hexVal[0]) * 16 + hexCharToNum(hexVal[1]); +} + +Bytex::Bytex() : Bytex('\0') +{} + +Bytex::Bytex(char byteVal) { + this->byteVal = byteVal; + byteValToHex(); +} + +Bytex::Bytex(const char* hexVal) { + this->hexVal[0] = hexVal[0]; + this->hexVal[1] = hexVal[1]; + hexValToByte(); +} + +char Bytex::getByte() const { + return byteVal; +} + +void Bytex::getHex(char* output) const { + output[0] = hexVal[0]; + output[1] = hexVal[1]; +} 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 new file mode 100644 index 0000000..fc39e7a --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/Bytex.h @@ -0,0 +1,17 @@ +class Bytex { + char hexVal[2]; + char byteVal; + + char digitToHexChar(unsigned int digit); + int hexCharToNum(char hexChar); + void byteValToHex(); + void hexValToByte(); + +public: + Bytex(); + Bytex(char byteVal); + Bytex(const char* hexVal); + + char getByte() const; + void getHex(char* output) const; +}; diff --git a/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.cpp b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.cpp new file mode 100644 index 0000000..f553bbe --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.cpp @@ -0,0 +1,102 @@ +#include "File.h" + +/* Helper functions */ + +size_t fileSize(std::ifstream& file) { + int currIndex = file.tellg(); + file.seekg(0, std::ios::end); + + size_t size = file.tellg(); + file.seekg(currIndex, std::ios::beg); + + return size; +} + +/* Private File functions */ + +void File::resizeBuffer() { + size_t newLen = (length == 0) ? 8 : length << 1; + + Bytex* temp = new Bytex[newLen]; + for (int i = 0; i < length; i++) + temp[i] = fileBuffer[i]; + + if (length != 0) delete[] fileBuffer; + fileBuffer = temp; + length = newLen; +} + +char File::bytexToReadableChar(Bytex bytex) const { + char b = bytex.getByte(); + if ((b >= 'A' && b <= 'Z') || (b >= 'a' && b <= 'z')) + return bytex.getByte(); + return '.'; +} + +/* Public File functions */ + +File::File(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 (int 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]; +} + +File::~File() { + delete[] fileBuffer; + delete[] originalFilePath; +} + +void File::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 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 { + saveAs(originalFilePath); +} + +void File::saveAs(const char* filePath) const { + std::ofstream outFile(filePath); + for (int 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/File.h new file mode 100644 index 0000000..753e734 --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/File.h @@ -0,0 +1,27 @@ +#include <iostream> +#include <fstream> +#include <cstring> +#include "Bytex.h" + +class File { + 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; + + void resizeBuffer(); + +public: + File(const char* filePath); + ~File(); + + void viewFile() const; + void change(int index, Bytex bytex); + void add(Bytex bytex); + void remove(); + 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 new file mode 100644 index 0000000..90baaee --- /dev/null +++ b/Year 1/Object-oriented Programming/Solutions Sem. 03/ex4/source.cpp @@ -0,0 +1,29 @@ +#include "File.h" + +void createFile() { + int x = 1816466293; + std::ofstream file("myData.dat", std::ios::binary); + file.write( (const char*)&x, sizeof(x)); +} + +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(); + + File myFileCopy("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) + + myFileCopy.viewFile(); + myFile.viewFile(); +} |
