-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper_functions.h
147 lines (130 loc) · 3.83 KB
/
helper_functions.h
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
/*
* helper_functions.h
*
* Created on: 2 July, 2017
* Author: Junsheng Fu
*/
#ifndef CALENG_FACTORY_HELPER_FUNCTIONS_H
#define CALENG_FACTORY_HELPER_FUNCTIONS_H
#include <iostream>
#include <string>
#include <exception>
#include <fstream>
/**
* Print the usage of the program
*/
void printUsage(){
std::cout<< "--------------------------------------------------"<<std::endl
<< "Usage 1: calc <engine_name> <file_list>"<<std::endl
<< "Usage 2: calc <engine_name> <list of integers>" << std::endl;
}
/**
* Check the input argument values are valid or not
* @param argc number of arguments
* @param argv list of arguments
* @return true or false based on the validation
*/
bool checkValidArg(int argc, char* argv[]){
if (argc < 3){
std::cout << "No enough argument values: please follow these two usages."<<std::endl;
printUsage();
return false;
}
else{ // "naive" is the engine_name that has been implemented
std::string engine_name = argv[1];
if (engine_name != "Multiplier" and engine_name != "Divider" ){
std::cout << "Invalid Math Engine: use either \"Multipiler\" or \"Divider\"."<<std::endl;
return false;
}
}
return true;
}
/**
* Check the string's ending
* @param source the string to be checked
* @param ending the pattern ending string
* @return true or false based on the result
*/
bool ending_check(std::string const & source, std::string const & ending)
{
if (ending.size() > source.size()) return false;
return std::equal(ending.rbegin(), ending.rend(), source.rbegin());
}
/**
* Read the files contain numbers, and save them into arr.
* Stop reading the numbers into arr as soon as meet a "illegal number" (e.g #)
* @param file_list file lists that contains numbers
* @param arr save all the numbers from the file lists
* @return true or false based on the status
*/
bool readTxtFiles(std::vector <std::string>& file_list, std::vector <double>& arr){
if (file_list.empty()){
std::cout<<"Inputs Error: no input files are given!"<<std::endl;
return false;
}
// read data into arr
for(int i=0; i<file_list.size(); i++){
std::fstream myfile(file_list[i].c_str(), std::ios_base::in);
if(!myfile){
std::cout<<"Input Error: Nonexistent *.txt file."<<std::endl;
return false;
}
double a;
while (myfile >> a)
{
arr.push_back(a);
}
}
return true;
}
/**
* Parse the list file or individual numbers given by the user through commands
* @param arr a double vector which is used for saving the numbers
* @param argc number of arguments
* @param argv list of arguments
* @return true or false based on the status
*/
bool getInputs(std::vector <double>& arr, int argc, char* argv[]){
// read file_list or list of integers
std::string arg;
double n;
std::string::size_type sz; // alias of size_t
std::string ending = ".txt";
std::vector<std::string> file_list;
arg = argv[2];
if (arg == "input"){
// read all .txt files to a vector, named file_list
for (int i=3; i < argc; i++){
arg = argv[i];
if (ending_check(arg, ending)){// if the ending is ".txt"
file_list.push_back(arg);
}
else{
std::cout<<"Inputs Error: illegal input files! Please give *.txt file."<<std::endl;
return false;
}
}
// read integers in every files into arr
if (!readTxtFiles(file_list, arr)){
return false;
} else{
return true;
}
}
else{ // check case of only integers
for(int i=2; i< argc; i++){
arg = argv[i];
try {
n = std::stod(arg, &sz);
}
catch (std::exception& e){
//std::cout <<e.what()<<std::endl;
std::cout <<"Inputs Error: illegal integers inputs!" << std::endl;
return false;
}
arr.push_back(n);
}
return true;
}
}
#endif //CALENG_FACTORY_HELPER_FUNCTIONS_H