-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI.cpp
More file actions
285 lines (257 loc) · 6.39 KB
/
CLI.cpp
File metadata and controls
285 lines (257 loc) · 6.39 KB
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
/*
Sudoku Solver Command Line Interface
Written by David Wiebe
*/
#include <iostream>
#include <string.h>
#include <string>
#include <time.h>
#include "sudoku.h"
#include "fileParse.h"
#include "generator.h"
enum InputCase
{
UNKNOWN = 0,
HELP,
PRINT_FILE,
GENERATE,
SOLVE_FILE,
TEST_FILE,
TEST_SCRIPT
};
int printHelper(int argc, char **argv);
int generateHelper(int argc, char **argv);
int solveFileHelper(int argc, char **argv);
int testFileHelper(int argc, char **argv);
int testScriptHelper(int argc, char **argv);
void lower(char *str);
void printHelp();
InputCase parseFirstInput(char *input);
int main(int argc, char *argv[])
{
if (argc <= 1)
{
printHelp();
return 0;
}
char *arg_1 = argv[1];
lower(arg_1);
const InputCase input = parseFirstInput(arg_1);
switch (input)
{
case (UNKNOWN):
case (HELP):
printHelp();
return 0;
case (PRINT_FILE):
printHelper(argc, argv);
return 0;
case (GENERATE):
generateHelper(argc, argv);
return 0;
case (SOLVE_FILE):
solveFileHelper(argc, argv);
return 0;
case (TEST_FILE):
testFileHelper(argc, argv);
return 0;
case (TEST_SCRIPT):
testScriptHelper(argc, argv);
return 0;
}
}
int printHelper(int argc, char **argv)
{
if (argc != 3)
{
throw std::invalid_argument("Print mode expects 1 argument");
}
const char *filePath = argv[2];
Sudoku *sudoku = sudokuFromFile(filePath);
std::cout << sudoku->ToString() << std::endl;
delete sudoku;
return 0;
}
int generateHelper(int argc, char **argv)
{
if (argc != 3 && argc != 4)
{
throw std::invalid_argument("Generate mode expects 1 or 2 arguments");
}
const int size = atoi(argv[2]);
int seed;
if (argc == 4)
{
seed = atoi(argv[3]);
}
else
{
seed = time(0);
}
Sudoku *sudoku = generateSudoku(size, seed);
std::cout << sudoku->ToString() << std::endl;
delete sudoku;
return 0;
}
int solveFileHelper(int argc, char **argv)
{
if (argc != 3)
{
throw std::invalid_argument("Solve-File mode expects 1 argument");
}
const char *filePath = argv[2];
if (filePath == nullptr)
{
throw std::invalid_argument("Input file path cannot be null");
}
if (fileExists(filePath) == false)
{
throw std::invalid_argument("Input file does not exist");
}
Sudoku *sudoku = sudokuFromFile(filePath);
Sudoku *solution = sudoku->Solution();
std::cout << solution->ToString() << std::endl;
delete sudoku;
delete solution;
return 0;
}
int testFileHelper(int argc, char **argv)
{
if (argc != 4)
{
throw std::invalid_argument("Test-File mode expects 2 arguments");
}
const char *inputFilePath = argv[2];
if (inputFilePath == nullptr)
{
throw std::invalid_argument("Input file path cannot be null");
}
if (fileExists(inputFilePath) == false)
{
throw std::invalid_argument("Input file does not exist");
}
const char *compairFilePath = argv[3];
if (compairFilePath == nullptr)
{
throw std::invalid_argument("Compair file path cannot be null");
}
if (fileExists(compairFilePath) == false)
{
throw std::invalid_argument("Compair file does not exist");
}
Sudoku *input = sudokuFromFile(inputFilePath);
Sudoku *compair = sudokuFromFile(compairFilePath);
bool testResult = test(input, compair);
delete input;
delete compair;
std::cout
<< "TEST "
<< (testResult
? "PASSED"
: "FAILED")
<< std::endl;
return 0;
}
int testScriptHelper(int argc, char **argv)
{
if (argc != 3)
{
throw std::invalid_argument("Test-Script mode expects 1 argument");
}
const char *testFilePath = argv[2];
if (testFilePath == nullptr)
{
throw std::invalid_argument("Input file path cannot be null");
}
if (fileExists(testFilePath) == false)
{
throw std::invalid_argument("Input file does not exist");
}
char *fileData = loadText(testFilePath);
replaceChar(fileData, '\n', ' ');
const char **filePaths = (const char **)split(fileData, " ");
for (int i = 0; filePaths[i] != 0; i++)
{
const char *filePath = filePaths[i];
if (fileExists(filePath) == false)
{
throw std::invalid_argument("File in test script does not exist");
}
}
for (int i = 0; filePaths[i] != 0; i += 2)
{
const char *inputFilePath = filePaths[i];
const char *compairFilePath = filePaths[i + 1];
Sudoku *input = sudokuFromFile(inputFilePath);
Sudoku *compair = sudokuFromFile(compairFilePath);
bool testResult = test(input, compair);
delete input;
delete compair;
std::cout
<< "TEST NUMBER "
<< i / 2
<< ": "
<< (testResult
? "PASSED"
: "FAILED")
<< std::endl;
}
delete[] fileData;
for (int i = 0; filePaths[i] != 0; i++)
{
delete[] filePaths[i];
}
delete[] filePaths;
return 0;
}
void lower(char *str)
{
if (str == nullptr)
{
throw std::invalid_argument("str cannot be null");
}
for (int i = 0; str[i]; i++)
{
str[i] = tolower(str[i]);
}
}
void printHelp()
{
std::cout
<< "Sudoku Solver Written by David Wiebe\n"
<< "To use run one of: \n"
<< "--Print-File <Filepath>\n"
<< "--Generate <size> <optional: seed>\n"
<< "--Solve-File <Filepath>\n"
<< "--Test-File <Input Filepath> <Compair Filepath>\n"
<< "--Test-Script <Script Filepath>\n"
<< std::endl;
}
InputCase parseFirstInput(char *input)
{
if (strcmp(input, "-h") == 0 || strcmp(input, "h") == 0 || strcmp(input, "help") == 0 || strcmp(input, "-help") == 0)
{
return HELP;
}
if (strcmp(input, "--print-file") == 0)
{
return PRINT_FILE;
}
if (strcmp(input, "--generate") == 0)
{
return GENERATE;
}
if (strcmp(input, "--solve-file") == 0)
{
return SOLVE_FILE;
}
if (strcmp(input, "--test-file") == 0)
{
return TEST_FILE;
}
if (strcmp(input, "--test-script") == 0)
{
return TEST_SCRIPT;
}
return UNKNOWN;
}