Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commita8b45fa164b84cba9a744b79ed2bd912ab829510 (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2022-04-11 23:27:45 +0300
Committer
 GitHub <noreply@github.com>
2022-04-11 23:27:45 +0300
Files3dd3009acfa0aac8fc87a33dd5f20b8f8d086e47
Object-oriented_programming_FMI-a8b45fa164b84cba9a744b79ed2bd912ab829510.tar
Object-oriented_programming_FMI-a8b45fa164b84cba9a744b79ed2bd912ab829510.tar.gz
Object-oriented_programming_FMI-a8b45fa164b84cba9a744b79ed2bd912ab829510.zip
Parentb5438316e7200194ac3fced0439c951707208e6a (diff)
Add files via upload
3 files changed, 180 insertions, 0 deletions
-rw-r--r--Sem. 03/HexViewer/HexViewer.cpp139
-rw-r--r--Sem. 03/HexViewer/HexViewer.h21
-rw-r--r--Sem. 03/HexViewer/Source.cpp20
diff --git a/Sem. 03/HexViewer/HexViewer.cpp b/Sem. 03/HexViewer/HexViewer.cpp
new file mode 100644
index 0000000..e4da57e
--- /dev/null
+++ b/Sem. 03/HexViewer/HexViewer.cpp
@@ -0,0 +1,139 @@
+#pragma once
+#include "HexViewer.h"
+#include <fstream>
+#include <iostream>
+
+
+size_t getFileSize(std::ifstream& f)
+{
+ size_t currentPos = f.tellg();
+ f.seekg(0, std::ios::end);
+ size_t size = f.tellg();
+
+ f.seekg(currentPos);
+ return size;
+}
+HexViewer::HexViewer(const char* fileName)
+{
+ std::ifstream file(fileName, std::ios::binary);
+ if (!file.is_open())
+ {
+ size = 0;
+ data = nullptr;
+ return;
+ }
+ size_t bytesCount = getFileSize(file);
+ data = new unsigned char[bytesCount];
+
+ file.read((char*)data, bytesCount);
+ size = bytesCount;
+}
+
+unsigned char convertSymbolToNumber(char symbol)
+{
+ if (symbol >= '0' && symbol <= '9')
+ return symbol - '0';
+ else if (symbol >= 'A' && symbol <= 'Z')
+ return symbol - 'A' + 10;
+ return '\0';
+}
+
+size_t getSymbolIndex(char ch)
+{
+ if ('0' <= ch && ch <= '9')
+ return ch - '0';
+ else if ('A' <= ch && ch <= 'F')
+ return ch - 'A';
+ else
+ return 0; //error ?
+}
+
+unsigned char HexViewergetByte(const char* byteStr)
+{
+ if (strlen(byteStr) != 2)
+ return 0;
+ return getSymbolIndex(byteStr[1]) + getSymbolIndex(byteStr[0]) * 16;
+}
+
+void resize(unsigned char*& data, size_t& size, size_t newSize)
+{
+ unsigned char* newDataArr = new unsigned char[newSize];
+
+ size_t minSize = std::min(size, newSize);
+ for (unsigned i = 0; i < minSize; i++)
+ newDataArr[i] = data[i];
+
+ delete[] data;
+ data = newDataArr;
+ size = newSize;
+}
+
+void HexViewer::addByte(const char* byteStr)
+{
+ if (strlen(byteStr) != 2)
+ return;
+ unsigned char currentByte = 16*convertSymbolToNumber(byteStr[0]) + convertSymbolToNumber(byteStr[1]);
+ resize(data, size, size + 1);
+ data[size - 1] = currentByte;
+}
+void HexViewer::removeLastByte()
+{
+ if (size == 0)
+ return;
+ resize(data, size, size - 1);
+
+}
+void HexViewer::changeByte(size_t index, const char* byteStr)
+{
+ if (index >= size || strlen(byteStr) != 2)
+ return;
+
+ unsigned char currentByte = convertSymbolToNumber(byteStr[1]) + 16 * convertSymbolToNumber(byteStr[0]);
+ data[index] = currentByte;
+}
+
+void HexViewer::saveAs(const char* fileName) const
+{
+ std::ofstream file(fileName, std::ios::binary);
+
+ if (!file.is_open())
+ return;
+ file.write((const char*)data, size);
+}
+
+void convertToHex(unsigned char byte, char* byteHex)
+{
+ char symbols[] = { '0','1', '2', '3', '4', '5', '6','7',
+ '8','9', 'A', 'B', 'C', 'D', 'E', 'F' };
+ byteHex[2] = '\0';
+ byteHex[1] = symbols[byte % 16];
+ byteHex[0] = symbols[(byte / 16) % 16];
+}
+
+bool isLetter(char ch)
+{
+ return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
+}
+void HexViewer::print() const
+{
+ for (unsigned i = 0; i < size; i++)
+ {
+ char byteHex[3];
+ convertToHex(data[i], byteHex);
+ std::cout << byteHex << " ";
+ }
+ std::cout << std::endl;
+ for (unsigned i = 0; i < size; i++)
+ {
+ if (isLetter(data[i]))
+ std::cout << data[i];
+ else
+ std::cout << ".";
+ std::cout << " ";
+ }
+ std::cout << std::endl;
+}
+HexViewer::~HexViewer()
+{
+ delete[] data;
+}
diff --git a/Sem. 03/HexViewer/HexViewer.h b/Sem. 03/HexViewer/HexViewer.h
new file mode 100644
index 0000000..3e154dc
--- /dev/null
+++ b/Sem. 03/HexViewer/HexViewer.h
@@ -0,0 +1,21 @@
+#pragma once
+class HexViewer
+{
+ unsigned char* data;
+ size_t size;
+
+public:
+ HexViewer(const char* fileName);
+
+ HexViewer(const HexViewer&) = delete;
+ HexViewer& operator=(const HexViewer&) = delete;
+
+ void addByte(const char* byte);
+ void removeLastByte();
+ void changeByte(size_t index, const char* byte);
+
+ void saveAs(const char* fileName) const;
+
+ void print() const;
+ ~HexViewer();
+}; \ No newline at end of file
diff --git a/Sem. 03/HexViewer/Source.cpp b/Sem. 03/HexViewer/Source.cpp
new file mode 100644
index 0000000..f0dc05c
--- /dev/null
+++ b/Sem. 03/HexViewer/Source.cpp
@@ -0,0 +1,20 @@
+#include <iostream>
+#include <fstream>
+#include "HexViewer.h"
+using namespace std;
+
+int main()
+{
+ HexViewer h("MyNumber2.dat");
+
+ h.print();
+ h.addByte("FF");
+ h.print();
+
+ h.removeLastByte();
+ h.print();
+
+ h.changeByte(3, "63");
+ h.print();
+
+} \ No newline at end of file