aboutsummaryrefslogtreecommitdiff
path: root/week09/Exercise3/Train.cpp
blob: 4dd793eef93b6dd79f483de1972d56929b16136c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#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;
}

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();
	}
	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);
	this->currentRegion[length] = '\0';
}

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();
}