-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
353 lines (311 loc) · 8.24 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
// External Variables
ascii_store ascii_collection[VOCABSIZE];
ascii_store default_ascii;
int main(){
int keepgoing = 1;
safe_makefile(FILENAME);
set_default_ascii(default_ascii.store);
while (keepgoing){
printf("\nEnter a command");
printf("\n'l' = re(load) existing records,");
printf("\n'a' = add record to database,");
printf("\n'u' = generate ASCII art for word");
printf("\n'd' = display a single character,");
printf("\n'f' = find undefined characters,");
printf("\n's' = save records to database,");
printf("\n'q' = quit program");
printf("\n>>> ");
// printf("\nCommands: 'l' = (re)load existing records, a' = add record, 'u' = use typeface");
// printf("\n 'd' = display character, 'f' = find undefined characters");
// printf("\n 's' = save records, 'q' = quit program\n>>>");
switch(tolower(getchar())){
case 'l':
printf("Loading existing records\n");
load_ascii_database(FILENAME);
break;
case 'f':
find_undefined_alpha();
break;
case 'a':
printf("Add record\n");
add_record();
break;
case 'd':
display_char();
break;
case 'q':
printf("Exiting program now\n");
keepgoing = 0;
break;
case 's':
printf("Saving records made thus far\n");
save_ascii_records(FILENAME);
break;
case 'u':
printf("Using typface\n");
use_typeface();
break;
case '\n':
break;
default:
printf("Invalid command\n");
break;
}
}
return 0;
}
void set_default_ascii(char store[]){
int i,j;
store[0] = '\0';
for (i=0; i<MAXHEIGHT; i++){
for (j=0; j<5; j++){
str_append(store, ' ');
}
str_append(store, '\n');
}
}
int map_char_to_array_index(char a){
int i;
if (a == ' '){
i = VOCABSIZE-1;
return i;
} else if (isalpha(a)){
if (isupper(a)){
i = a - 'A' + 26;
return i;
}
if (islower(a)){
i = a - 'a';
return i;
}
} else { //This section is actually never called. DEBUGGING
printf("Invalid character {%c} entered. Must be an alphabet or the space char.", a);
return -1;
}
}
int decide_which_char(){
/* Purpose: returns i, which is the index position of the char in ascii_collection;
will check if the character is an alphabet, else it returns -1 */
char c;
int i;
printf("Please enter the character:\n>>>");
getchar(); //Gets rid of the \n char
c = getchar();
if (!(isalpha(c)||c==' ')){
printf("The character you entered is not an alphabet or the space char. Please try again.");
return -1;
}
i = map_char_to_array_index(c);
return i;
}
void get_ascii(char store[]){
/*Purpose: Reads the user's ASCII definition of an alphabet or number from the command line.
* The \n character is read in and saved in the store[] char array as well. However,
* previous_carriage_return will keep track of whether or not a \n immediately follows
* a \n, in which case, the program will stop reading from the command line. In other
* words, two successive carriage returns will cause the reading from the command line
* the stop. */
int previous_carriage_return = 0;
int height_counter = 0;
int condition = 1;
int starting_whitespace = 1;
int index = 0;
char c, d;
printf("Please enter the typeface design. Use carriage return to get to the next line.\n");
while(condition){
if (starting_whitespace){
d = getchar();
while (d == '\n'){
d = getchar();
}
starting_whitespace = 0;
store[index++] = d;
} else {
c = getchar();
if(!previous_carriage_return){
if(c=='\n'){
previous_carriage_return = 1;
height_counter++;
}
store[index++] = c;
} else {
if(c=='\n'){
condition = 0;
} else {
previous_carriage_return = 0;
store[index++] = c;
}
}
}
}
if (height_counter != MAXHEIGHT){
printf("Your input typeface is not %d lines high. This is invalid. Please re-enter your design.", MAXHEIGHT);
set_default_ascii(store);
}
}
void add_record(){
int i;
printf("Which character's typeface would you like to add a record for? ");
i = decide_which_char();
if (i == -1){
;
} else {
get_ascii(ascii_collection[i].store);
ascii_collection[i].user_defined_bool = 1;
}
}
void save_ascii_records(char* filename){
FILE* file;
int i;
file = fopen(filename, "wb");
if (file==0){
printf("Cannot open file: %s\n", FILENAME);
} else{
for (i=0; i<VOCABSIZE; i++){
if (ascii_collection[i].user_defined_bool){
fwrite(&ascii_collection[i], sizeof(ascii_store), 1, file);
} else {
fwrite(&default_ascii, sizeof(ascii_store), 1, file);
}
}
}
fclose(file);
// Gets rid of trailing characters in STDIN
char c;
while ((c=getchar()) != '\n'){
;
}
}
void display_char(){
int i;
char c;
printf("Which character's typeface would you like to display? ");
i = decide_which_char();
printf("%s", ascii_collection[i].store);
getchar(); //Gets rid of \n character. DEBUGGING REQUIRED.
}
void load_ascii_database(char* filename){
FILE* file;
int numrecsread;
file = fopen(FILENAME, "rb");
if (file==0){
printf("Cannot read file: %s\n", filename);
} else{
numrecsread = fread(ascii_collection, sizeof(ascii_store), VOCABSIZE, file);
}
if (numrecsread != VOCABSIZE){
printf("Error: %d records were read into memory, but there ought to be %d records in total", numrecsread, VOCABSIZE);
}
// Gets rid of any trailing characters in STDIN
char c;
while ((c=getchar()) != '\n'){
;
}
}
void use_typeface(){
char c;
char array[MAXCHAR];
int i = 0;
int j, k, l;
int condition = 1;
int display_boolean = 1;
// Initialize h_disp
h_disp disp_array;
disp_array.line1[0] = '\0';
disp_array.line2[0] = '\0';
disp_array.line3[0] = '\0';
disp_array.line4[0] = '\0';
disp_array.line5[0] = '\0';
disp_array.line6[0] = '\0';
printf("Please enter a sentence. This will be converted to the defined ASCII typeface:\n>>>");
getchar(); //Gets rid of the \n character. DEBUGGING REQUIRED.
while (condition){
c = getchar();
// if (c=='\n'||c==EOF){
// condition = 0;
// }
if (isalpha(c)||c==' '){
array[i++] = c;
} else if (c=='\n'){
condition = 0;
} else {
printf("Invalid input. Only alphabets allowed.");
display_boolean = 0;
}
}
if (display_boolean) {
for (j=0; j<i; j++){
k = map_char_to_array_index(array[j]);
//Not very intelligent code here. Manually copy and paste. DEBUGGING REQUIRED.
for (l=0; ascii_collection[k].store[l] != '\n'; l++){
str_append(disp_array.line1, ascii_collection[k].store[l]);
}
for (l++; ascii_collection[k].store[l] != '\n'; l++){
str_append(disp_array.line2, ascii_collection[k].store[l]);
}
for (l++; ascii_collection[k].store[l] != '\n'; l++){
str_append(disp_array.line3, ascii_collection[k].store[l]);
}
for (l++; ascii_collection[k].store[l] != '\n'; l++){
str_append(disp_array.line4, ascii_collection[k].store[l]);
}
for (l++; ascii_collection[k].store[l] != '\n'; l++){
str_append(disp_array.line5, ascii_collection[k].store[l]);
}
for (l++; ascii_collection[k].store[l] != '\n'; l++){
str_append(disp_array.line6, ascii_collection[k].store[l]);
}
}
printf("%s\n", disp_array.line1);
printf("%s\n", disp_array.line2);
printf("%s\n", disp_array.line3);
printf("%s\n", disp_array.line4);
printf("%s\n", disp_array.line5);
printf("%s\n", disp_array.line6);
}
}
void find_undefined_alpha(){
int i;
int num_undefined = 0;
char c;
char undefined_alpha[VOCABSIZE];
undefined_alpha[0] = '\0';
for (i=0; i<VOCABSIZE; i++){
if (!ascii_collection[i].user_defined_bool){
if (i<=25){
c = 'a' + i;
num_undefined++;
str_append(undefined_alpha, c);
} else {
c = 'A' + i - 26;
num_undefined++;
str_append(undefined_alpha, c);
}
}
}
if (num_undefined){
printf("The following characters still do not have custom typefaces defined for them:\n");
printf("%s\n", undefined_alpha);
} else {
printf("All characters already have a custom typeface defined for them.\n");
}
// Gets rid of any trailing characters in STDIN
while ((c=getchar()) != '\n'){
;
}
}
void safe_makefile(char* filename){
FILE* file;
file = fopen(filename, "ab");
fclose(file);
}
void str_append(char s[], char c){
int len = strlen(s);
s[len] = c;
s[len+1] = '\0';
}