-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
137 lines (106 loc) · 3.39 KB
/
main.c
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
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <ctype.h>
#include <time.h>
#include "utils.h"
#include "find_words.h"
#include "version_info.h"
// this adds windows support (needed since windows isn't postix-compatible)
#include "getline.h"
uint16_t rows, cols;
char** game, **solutions;
char* words;
size_t words_len = 64;
char silent = 0;
int main(int argc, char** argv){
FILE* inputFile = stdin;
if (argc >= 2) {
if (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
printVersionInfo();
return 0;
} else if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
puts("useage: wordfinder [file|options]\n"
" --help : show this help\n"
" --version : prints the version and lisense info\n"
" [file] : file must contain the dimensions separate by spaces,"
" the puzzle, and the words to find separated by spaces, just as"
" one would input without the file.\n\n"
);
return 0;
}
inputFile = fopen(argv[1], "r");
silent = 1;
// file not found
if (!inputFile) {
fprintf(stderr, "%s: error, file %s not found.\n", argv[0], argv[1]);
return 1;
}
}
/// TODO: program must ERROR if the file doesn't contain enough information
if (!silent) printf("Number of rows: ");
if (fscanf(inputFile, "%hu", &rows) == EOF) { // uint16_t == unsigned short ( "%hu" )
perror("\aERROR: Malformed input file (missing dimension)");
printFileHelper();
return 1;
}
if (!silent) printf("Number of cols: ");
if (fscanf(inputFile, "%hu", &cols) == EOF) {
perror("\aERROR: Malformed input file (missing dimension)");
printFileHelper();
return 1;
}
if (!silent) puts("\n\nEnter the puzzle (spaces and newlines ignored):\n");
game = (char**) malloc(rows * sizeof(char*));
solutions = (char**) malloc(rows * sizeof(char*));
for (uint16_t r = 0; r < rows; r++) {
*(game + r) = (char*) malloc(cols);
*(solutions + r) = (char*) malloc(cols);
for (uint16_t c = 0; c < cols; c++) {
char letter;
do
letter = fgetc(inputFile);
while (letter == '\n' || isspace(letter));
if (letter == EOF) {
perror("\aERROR: Malformed input (incomplete puzzle)\n");
printFileHelper();
return 2;
}
CHAR_AT(game, r, c) = letter;
CHAR_AT(solutions, r, c) = 0;
}
}
if (!silent) printf("Enter the words to find (separated by spaces): ");
words = (char*) malloc(words_len); // grown as needed
// keep checking for input every time they press enter without words
do
// no words to find
if (getline(&words, &words_len, inputFile) == -1) {
perror("\aERROR: Malformed input (words not given)\n");
printFileHelper();
return 3; // EOF
}
while (*words == '\n');
char* str = words;
while(*++str != '\0');
if (*--str == '\n')
*str = '\0';
// time how long it takes to solve the puzzle
clock_t diff, start = clock();
findWords(); // solve puzzle
diff = clock() - start;
// give processing time
long usec = diff * 1000000 / CLOCKS_PER_SEC;
printf("\nsolved in %ld milliseconds %ld microseconds\n\n",
usec / 1000, usec % 1000);
// print the board with solutions highlighted in bright-red
for (uint16_t r = 0; r < rows; r++) {
for (uint16_t c = 0; c < cols; c++)
if (CHAR_AT(solutions, r, c))
// print the solution char in bright-red
printf("\x1B[31;1m%c\x1B[0m ", CHAR_AT(game, r, c));
else
printf("%c ",CHAR_AT(game, r, c));
putc('\n', stdout);
}
}