-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheck_format.cpp
306 lines (264 loc) · 7.88 KB
/
Check_format.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
@author: Sir0ga90
@version: 0.1
*/
//======================================================================//Includes
#include "Check_format.h"
//======================================================================//Function definitions
/*
check if file formats fits needed format
in: is - input streem
in: w_fill_from - vector of string from which wall will be filled,
contain only wall strings
*/
bool check_file_frmt(ifstream& is, Vecf& w_fill_from)
try{
const short wl_sz_line = 0;
Vecf f_strings;
int hig = 0;
int wid = 0;
int block_num_id = 0; // index of block number val
int block_num = 0; // number of block types
int blocks_id = 0; // index of first block
file_to_str_vec(is, f_strings);
get_wall_size_str(f_strings.at(wl_sz_line), hig, wid);
check_input_wall(f_strings, w_fill_from, hig, wid);
block_num_id = hig + 1;
block_num = check_block_num_line(f_strings.at(block_num_id));
blocks_id = block_num_id + 1;
check_blk_typs(f_strings, blocks_id, block_num);
is.close(); //before reopennig
return true;
}
catch (exception& e){
cerr << "CHECK ERROR: " << e.what() << endl;
system("pause");
}
//======================================================================//
/*
read string from streem
in: is - input streem
*/
void read_str(ifstream& is){
string s;
s = get_str(is);
}
//==============================//
string get_str(ifstream& ist){
string s;
getline(ist, s);
return s;
}
//======================================================================//
/*
read first line, with sizes of wall, and check it according to the format
in: s - string for line
in: h - hight of main wall
in: w - width of main wall
*/
void get_wall_size_str(string s, int& h, int& w){
string::iterator it = s.begin();
if (isdigit(*it)){
w = chk_w_sz_loop(it, s);
}
else
throw exception("Unexpected wall-width value\n");
if (it >= s.end())
throw exception("Not good matrix size input, only one parametr\n");
else if (*it != ' ')
throw exception("Unexpected symbol after wall-width\n");
it++;
if (isdigit(*it)){
h = chk_w_sz_loop(it, s);
}
else
throw exception("Unexpected wall-hight value\n");
if (!isdigit(*it) || it + 1 != s.end()){ // if last num not a number or next char not the end of string
throw exception("Unexpected end of matrix-size line\n");
}
}
//======================================================================//
/*
take all digits for values of hight & width of wall
in: it - iterator for going through the string with values
in: s - string with values
*/
int chk_w_sz_loop(string::iterator& it, const string& s){
int i = 0;
string num;
static int pos = false;
for (; isdigit(*it) && it + 1 < s.end(); it++){ //it+1 for not go out of scope
num.push_back(*it);
}
if (pos) // check it wid or hig
num.push_back(*it);
else
pos = true;
return stoi(num);
}
//======================================================================//
/*
take strings from file & push it in vector
& check if file have empty lines before end
in: is - input streem
in: vec - vector to fill
*/
void file_to_str_vec(ifstream& is, Vecf& vec){
string s;
while (!is.eof()){
s = get_str(is);
if (s == "")
throw exception("Unexpected clear string in file\n");
vec.push_back(s);
}//while (!is.eof())
}
//======================================================================//
/*
check string for wall lines
in: vec - full input vector
in: w_vec - vector only for wall lines
in: h - hight of main wall
in: w - width of main wall
*/
void check_input_wall(Vecf& vec, Vecf& w_vec, int h, int w){
int start_index = 1;
for (int i = start_index; i <start_index + h; i++){
check_w_line(vec.at(i), w);
w_vec.push_back( vec.at(i) ); //fill vector of strings only for wall
}
}
//======================================================================//
/*
check if width of line is according to wall width
in: s - line
in: w - width of main wall
*/
void check_w_line(string& s, int w){
if (s.size() < w || s.size() > w)
throw exception("Wrong wall-line input\n");
else
return;
}
//======================================================================//
/*
check for correctnes of input number of block types
in: s - string with ONE number
*/
int check_block_num_line(string& s){
int block_type_num = 0;
string::iterator it = s.begin();
if (isdigit(*it)){
block_type_num = chk_blk_num_loop(it, s);
}
else
throw exception("Unexpected number of blocks\n");
if (!isdigit(*it) || it + 1 != s.end()){ // if last num not a number or next char not the end of string
throw exception("Unexpected number of block types value\n");
}
return block_type_num;
}
//======================================================================//
/*
take all digits for value of block types number
in: it - iterator for going through the string with values
out: number of block types
*/
int chk_blk_num_loop(string::iterator& it, const string& s){
string num;
static int pos = false;
for (; isdigit(*it) && it + 1 < s.end(); it++){ //it+1 for not go out of scope
num.push_back(*it);
}
num.push_back(*it);
return stoi(num);
}
//======================================================================//
/*
check blocks for correctnes of input h & w & num of block types
in: vec - input vector
in: begin - index of first block
in: block_num
*/
void check_blk_typs(Vecf& vec, int begin, int block_num){
int i = begin;
for (; i < vec.size(); i++){
if (i >= begin + block_num) // greater number of block types
throw exception("Unexpected end of file\n \
Check number of blocks");
check_blk_typ(vec.at(i));
}//for (; i < vec.size(); i++)
if (i < begin + block_num) // check for less number of block types
throw exception("Real number of block-types less then declared\n");
}
//======================================================================//
/*
check if all block parametrs is on their place
in: s - string with block parameters
*/
void check_blk_typ(string s){
int h = 0;
int w = 0;
int n = 0;
string::iterator it = s.begin();
if (isdigit(*it)){
w = chk_blk_sz_n_loop(it, s); //wid
}
else
throw exception("Unexpected block-width value\n");
if (it >= s.end())
throw exception("Not good block size input, only one parametr\n");
else if (*it != ' ')
throw exception("Unexpected symbol after block-width\n");
it++;
if (isdigit(*it)){
h = chk_blk_sz_n_loop(it, s); //hig
}
else
throw exception("Unexpected block-hight value\n");
if (it >= s.end())
throw exception("Not good block size input, only two parametrs\n");
else if (*it != ' ')
throw exception("Unexpected symbol after block-hight\n");
it++;
if (isdigit(*it)){
n = chk_blk_sz_n_loop(it, s); //num
}
else
throw exception("Unexpected block number value\n");
if (!isdigit(*it) || it + 1 != s.end()){ // if last num not a number or next char not the end of string
throw exception("Unexpected end of block type line\n");
}
}
//======================================================================//
/*
take all digits for value of block types number
in: it - iterator for going through the string with values
out:switch(pos)
case 0 -> width of block
case 1 -> hight of block
case 2 -> number of this type blocks
*/
int chk_blk_sz_n_loop(string::iterator& it, const string& s){ //sz - size; n - number
int i = 0;
string num;
static int pos = 0;
for (; isdigit(*it) && it + 1 < s.end(); it++){ //it+1 for not go out of scope
num.push_back(*it);
}
switch (pos){
case 0:
pos++;
break;
case 1:
num.push_back(*it);
pos++;
break;
case 2:
num.push_back(*it);
break;
default:
pos = 0;
break;
}
return stoi(num);
}