diff options
| author | Syndamia <kamen@syndamia.com> | 2024-05-07 22:17:32 +0300 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-05-07 22:17:32 +0300 |
| commit | cbd6d3aa606b18dde3f203e6edfab04935bbbd59 (patch) | |
| tree | ca4288a359e7061ce74f16f3e8f05f7d40ade95f /week09/Exercise3/Train.cpp | |
| parent | 88f15e35713c9632216931d26443dc588238732f (diff) | |
| download | oop-2023-solutions-cbd6d3aa606b18dde3f203e6edfab04935bbbd59.tar oop-2023-solutions-cbd6d3aa606b18dde3f203e6edfab04935bbbd59.tar.gz oop-2023-solutions-cbd6d3aa606b18dde3f203e6edfab04935bbbd59.zip | |
[w9] Added rough solution to ex 3
Diffstat (limited to 'week09/Exercise3/Train.cpp')
| -rw-r--r-- | week09/Exercise3/Train.cpp | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/week09/Exercise3/Train.cpp b/week09/Exercise3/Train.cpp new file mode 100644 index 0000000..68d4206 --- /dev/null +++ b/week09/Exercise3/Train.cpp @@ -0,0 +1,132 @@ +#include "Train.h" +#include <iostream> +#include <cstring> +#include <fstream> + +void Train::free() { + delete[] regionsFileName; + delete[] currentRegion; +} + +void Train::copyFrom(const Train& other) { + strcpy(this->model, other.model); + this->railID = other.railID; + this->regionsFileName = new char[strlen(other.regionsFileName) + 1]; + strcpy(this->regionsFileName, other.regionsFileName); + this->currentRegion = new char[strlen(other.currentRegion) + 1]; + strcpy(this->currentRegion, other.currentRegion); +} + +Train::Train() { + model[0] = '\0'; + railID = 0; + regionsFileName = currentRegion = nullptr; +} + +Train::~Train() { + free(); +} + +Train::Train(const Train& other) { + copyFrom(other); +} + +Train& Train::operator=(const Train& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Train::Train(Train&& other) { + strcpy(this->model, other.model); + this->railID = other.railID; + + this->regionsFileName = other.regionsFileName; + other.regionsFileName = nullptr; + this->currentRegion = other.currentRegion; + other.currentRegion = nullptr; +} + +void Train::setCurrentRegion(std::ifstream& inFile) { + int start = inFile.tellg(); + while (!inFile.eof() && inFile.peek() != '\n') { + inFile.get(); + } + inFile.clear(); + + int length = inFile.tellg(); + this->currentRegion = new char[length + 1]; + + inFile.seekg(start, std::ios::beg); + inFile.getline(this->currentRegion, length + 1); +} + +Train::Train(const char model[128], unsigned railID, const char* regionsFileName) { + strcpy(this->model, model); + this->railID = railID; + this->regionsFileName = new char[strlen(regionsFileName) + 1]; + strcpy(this->regionsFileName, regionsFileName); + + std::ifstream inFile(regionsFileName); + if (!inFile.is_open()) { + throw "Couldn't open regions file!"; + } + + setCurrentRegion(inFile); + + inFile.close(); +} + +// Обикаля сегашния ред във файла и сравнява всяка буква със съответната от str +// Във всички случаи предвижва inFile напред към следващия ред +bool fileStringEq(std::ifstream& inFile, const char* str) { + if (inFile.eof()) { + inFile.close(); + throw -1; + } + + bool equal = true; + while (!inFile.eof() && inFile.peek() != '\n') { + if (inFile.peek() != *str) { + equal = false; + } + inFile.get(); + if (*str != '\0') str++; + } + + if (!inFile.eof()) { + inFile.get(); + } + return equal && *str == '\0'; +} + +void Train::TransferNextRegion() { + std::ifstream inFile(regionsFileName); + if (!inFile.is_open()) { + throw "Couldn't open regions file!"; + } + + while (!fileStringEq(inFile, currentRegion)); + setCurrentRegion(inFile); + + inFile.close(); +} + +void Train::SwitchRailLine(unsigned railID, const char* regionsFileName) { + this->railID = railID; + delete[] this->regionsFileName; + + this->regionsFileName = new char[strlen(regionsFileName) + 1]; + strcpy(this->regionsFileName, regionsFileName); + + std::ifstream inFile(regionsFileName); + if (!inFile.is_open()) { + throw "Couldn't open regions file!"; + } + + setCurrentRegion(inFile); + + inFile.close(); +} |
