-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflector.cpp
66 lines (55 loc) · 1.97 KB
/
reflector.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
#include <iostream>
#include <fstream>
using namespace std;
#include "errors.h"
#include "reflector.hpp"
//helper to check the pairing of configuration file
int Reflector :: check_pairs(ifstream &the_file){
int counter = 0, test_num = 0, paired_num = 0;
for( ; !the_file.eof(); ++counter){ //grab two numbers at a time
the_file >> test_num >> ws;
// if finish on an uneven number, complain
if(the_file.eof()){
cerr << "Error: incorrect number of reflector parameters (uneven amount)." << endl;
return INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
}
the_file >> paired_num >> ws;
// if trying to map to itself, complain
if(test_num == paired_num){
cerr << "Error: impossible reflector mapping (cannot map a letter to itself)." << endl;
return INVALID_REFLECTOR_MAPPING;
}
}
the_file.seekg(0);
//check for repeats BEFORE checking for too few pairs
if(!check_repeat(the_file)){
cerr << "Error: invalid reflector mapping (cannot map a letter twice, or to itself)." << endl;
return INVALID_REFLECTOR_MAPPING;
}
// test to see if loop counter shows wrong number of parameters
if(counter > ALPH_COUNT/2){
cerr << "Error: invalid number of reflector parameters (too many)." << endl;
return INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
} else if (counter < ALPH_COUNT/2){
cerr << "Error: invalid number of reflector parameters (too few)." << endl;
return INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
}
the_file.seekg(0);
return NO_ERROR;
}
//calls helper functions in order to interrogate file
int Reflector :: file_check(ifstream &the_file){
int status = NO_ERROR;
if(is_empty(the_file)){
cerr << "Error: invalid number of reflector parameters (file is empty)." << endl;
return INCORRECT_NUMBER_OF_REFLECTOR_PARAMETERS;
}
status = validate_numbers(the_file);
if(status)
return status;
status = check_pairs(the_file);
if(status)
return status;
the_file.seekg(0);
return status;
}