-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cpp
251 lines (225 loc) · 6.27 KB
/
Util.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#pragma warning(disable:4996)
#include <ctime>
#include <sstream>
#include "Util.h"
#include "HierarchyHeaders.h"
#include "DefinedCodes.h"
bool Util::ERROR = false;
bool Util::isCin(istream &in)
{
return (&in == &cin);
}
bool Util::isCout(ostream &out)
{
return (&out == &cout);
}
bool Util::stringsEqual(const char* const first, const char* const second)
{
return (strcmp(first, second) == 0);
}
uint Util::characterCount(const char* buffer, char ch)
{
uint counter = 0;
int length = strlen(buffer);
for (int i = 0; i < length; ++i)
counter += (buffer[i] == ch);
return counter;
}
char* Util::charToString(char ch)
{
char *c = new char;
*c = ch;
return c;
}
char* Util::trim(const char *str)
{
int length = strlen(str);
int start = 0, stop = length - 1;
while (start < length && isWhitespace(str[start]))
++start;
while (stop >= 0 && isWhitespace(str[stop]))
--stop;
char *trimmedString = new char[stop - start + 2];
for (length = start; length <= stop; ++length)
trimmedString[length - start] = str[length];
trimmedString[length - start] = '\0';
return trimmedString;
}
char* Util::removeTabs(const char *str)
{
char *p, *copy = new char[2 + strlen(str)];
strcpy(copy, str);
while ((p = strchr(copy, '\t')) != nullptr)
{
copy[p - copy] = ' ';
}
return copy;
}
char* Util::getTime(void)
{
time_t now = time(0);
tm* time = localtime(&now);
char *timeString = new char[16];
sprintf(timeString, "[%02d:%02d:%02d] ", time->tm_hour, time->tm_min, time->tm_sec);
return timeString;
}
void Util::warning(const char *line)
{
Logger::client()->writeToBuffer("ERROR >>> Linia \"");
Logger::client()->writeToBuffer(line);
Logger::client()->writeToBuffer("\" nu a putut fi convertita la AutoMobil. Am trecut peste ea");
Logger::client()->writeBuffer();
}
void Util::clearBuffer(istream &in)
{
in.clear();
in.ignore(MAX, '\n');
Logger::client()->write("Util >>> clearBuffer");
}
void Util::validateTrue(bool condition, char *message)
{
if (!condition)
{
Logger::client()->write(strcat("EROARE >>> ", message));
Util::forgetString(message);
exit(EXIT_FAILURE);
}
}
void Util::forgetString(char *&str)
{
if (str != nullptr)
{
delete[] str;
str = nullptr;
}
}
void Util::forgetArray(char **&str, int length)
{
for (int i = 0; i < length; ++i)
forgetString(str[i]);
delete[] str;
str = nullptr;
}
char* Util::copyString(const char *str)
{
validateTrue(str != nullptr, "Nu pot determina lungimea unui string null");
size_t size = (2 + strlen(str)) * sizeof(char);
char *newString = new char[size];
strcpy_s(newString, size, str);
return newString;
}
void Util::openFile(const char *fileName)
{
char *command = new char[128];
strcpy(command, "explorer /e,");
strcat(command, fileName);
system(command);
Util::forgetString(command);
}
bool Util::isWhitespace(char ch)
{
return (ch == ' ') || (ch == '\t');
}
// numara cate caractere albe are parametrul la inceput si la sfarsit
uint Util::whitespacesCount(char *buffer)
{
uint count = 0;
int length = strlen(buffer);
for (int i = 0; i < length && isWhitespace(buffer[i]); ++i)
++count;
for (int i = length - 1; i >= 0 && isWhitespace(buffer[i]); ++i)
++count;
return count;
}
template<typename T>
bool Util::successfullyRead(const char *buffer, T &value)
{
const char *typeName = typeid(T).name();
char *format[] = { "%d", "%f", "%u", "%c" };
char *type[] = { _int_, _float_, _uint_, _char_ };
for (int i = 0; i < 4; ++i)
if (Util::stringsEqual(typeName, type[i]))
return (sscanf(buffer, format[i], &value) > 0);
return false;
}
char* Util::readString(istream &in, const char *message, uint minLength)
{
char buffer[MAX];
uint length;
do
{
if (isCin(in))
cout << message;
in.getline(buffer, MAX - 1);
length = strlen(trim(buffer));
} while (length < minLength);
char *newString = copyString(trim(buffer));
return newString;
}
template<>
char* Util::read<char*>(istream &in, const char *message)
{
return readString(in, message, 2);
}
template<typename T>
T Util::read(istream &in, const char *message)
{
if (Util::isCin(in))
{
const char *typeName = typeid(T).name();
char *format[] = { "%d", "%f", "%u", "%c" };
char *type[] = { _int_, _float_, _uint_, _char_ };
for (int index = 0; index < 4; ++index)
{
if (Util::stringsEqual(typeName, type[index]))
{
T value;
char *buffer = Util::readString(in, message, 1);
int code = sscanf(buffer, format[index], &value);
while (code == 0)
{
Logger::client()->writeToBuffer("ERROR >>> Wrong Number Format: \"");
Logger::client()->writeToBuffer(buffer);
Logger::client()->writeToBuffer("\" nu contine un prefix de tip ");
Logger::client()->writeToBuffer(type[index]);
Logger::client()->writeBuffer();
buffer = Util::readString(in, message, 1);
code = sscanf(buffer, format[index], &value);
if (Util::stringsEqual(buffer, "."))
code = 0;
}
return value;
}
}
}
Util::ERROR = true;
return 0;
}
template<typename T>
void Util::write(ostream &out, const char* message, T content, bool writeNewLine)
{
if (!isCout(out)) // scriere in fisier
out << content << separator;
else // scriere la consola
{
out.width(16);
out.fill(' ');
out << message << tab << content;
}
if (writeNewLine)
out << enter;
}
template bool Util::successfullyRead(const char *buffer, char &value);
template bool Util::successfullyRead(const char *buffer, int &value);
template bool Util::successfullyRead(const char *buffer, float &value);
template bool Util::successfullyRead(const char *buffer, uint &value);
template char* Util::read<char*>(istream &in, const char* message);
template char Util::read<char>(istream &in, const char* message);
template float Util::read<float>(istream &in, const char* message);
template int Util::read<int>(istream &in, const char* message);
template uint Util::read<uint>(istream &in, const char* message);
template void Util::write(ostream &out, const char* message, char* content, bool writeNewLine);
template void Util::write(ostream &out, const char* message, char content, bool writeNewLine);
template void Util::write(ostream &out, const char* message, float content, bool writeNewLine);
template void Util::write(ostream &out, const char* message, int content, bool writeNewLine);
template void Util::write(ostream &out, const char* message, uint content, bool writeNewLine);