aboutsummaryrefslogtreecommitdiff
path: root/week09/Exercise3/Train.cpp
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2024-05-08 09:35:42 +0300
committerSyndamia <kamen@syndamia.com>2024-05-08 09:35:42 +0300
commite6f1ba664667047c74436f4f35536293f02d846f (patch)
tree6fc76bcdd2bc03aa740bab26645b072cacfd7c28 /week09/Exercise3/Train.cpp
parentcbd6d3aa606b18dde3f203e6edfab04935bbbd59 (diff)
downloadoop-2023-solutions-e6f1ba664667047c74436f4f35536293f02d846f.tar
oop-2023-solutions-e6f1ba664667047c74436f4f35536293f02d846f.tar.gz
oop-2023-solutions-e6f1ba664667047c74436f4f35536293f02d846f.zip
[w9] Fixed and finished ex3
Diffstat (limited to 'week09/Exercise3/Train.cpp')
-rw-r--r--week09/Exercise3/Train.cpp26
1 files changed, 23 insertions, 3 deletions
diff --git a/week09/Exercise3/Train.cpp b/week09/Exercise3/Train.cpp
index 68d4206..4dd793e 100644
--- a/week09/Exercise3/Train.cpp
+++ b/week09/Exercise3/Train.cpp
@@ -49,18 +49,37 @@ Train::Train(Train&& other) {
other.currentRegion = nullptr;
}
+Train& Train::operator=(Train&& other) {
+ if (this != &other) {
+ free();
+
+ strcpy(this->model, other.model);
+ this->railID = other.railID;
+
+ this->regionsFileName = other.regionsFileName;
+ other.regionsFileName = nullptr;
+ this->currentRegion = other.currentRegion;
+ other.currentRegion = nullptr;
+ }
+ return *this;
+}
+
void Train::setCurrentRegion(std::ifstream& inFile) {
int start = inFile.tellg();
while (!inFile.eof() && inFile.peek() != '\n') {
inFile.get();
}
- inFile.clear();
+ if (inFile.eof()) {
+ inFile.clear();
+ inFile.seekg(0, std::ios::end);
+ }
int length = inFile.tellg();
this->currentRegion = new char[length + 1];
inFile.seekg(start, std::ios::beg);
- inFile.getline(this->currentRegion, length + 1);
+ inFile.getline(this->currentRegion, length);
+ this->currentRegion[length] = '\0';
}
Train::Train(const char model[128], unsigned railID, const char* regionsFileName) {
@@ -108,7 +127,8 @@ void Train::TransferNextRegion() {
throw "Couldn't open regions file!";
}
- while (!fileStringEq(inFile, currentRegion));
+ while (!fileStringEq(inFile, currentRegion)) {
+ }
setCurrentRegion(inFile);
inFile.close();