-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
698 lines (677 loc) · 14.2 KB
/
input.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#include <stdio.h>
#include <stdbool.h>
#include "header/input.h"
#include "header/output.h"
#include "header/examples.h"
#include "header/dynarray.h"
#include "header/struc.h"
#include "header/IAfunct.h"
#include "header/AtFiDi.h"
#include "header/Wwvla.h"
#include "header/Ahaf.h"
#include "header/DynDaStr.h"
#include "header/Algo.h"
int readInput(char* buf, unsigned int bufsz, unsigned int* outlen)
{
char c;
unsigned int pos = 0;
bool flag = false;
do
{
c = getchar();
if (c == EOF)
return RI_EOF;
if (c == '\n')
break;
//Do not 'leak' buffsize (actually totally unecessary)
if(pos <= bufsz -2)
{
buf[pos] = c;
++pos;
}
//Let the user type as long as he wants (looks more professional)
else
{
flag = true;
}
} while(1);
if (flag || pos > bufsz-1)
{
buf[bufsz-1] = '\0';
*outlen = bufsz;
return RI_INSUFFICIENT_BUFFER_SIZE;
}
//Terminate with 0; otherwise using it as string (someone might do that one day
//will case problems)
buf[pos] = '\0';
*outlen = pos+1;
return 0;
}
int interpretInput(const char* in, unsigned int insz, II_Action* action)
{
if (insz < 2)
return II_NOINTERPRET;
switch(in[0])
{
case 'e':
*action = Exit;
return 0;
case 'h':
*action = Help;
return 0;
case 't': // Trial version for executing example Programms
*action = ExecuteExample;
return 0;
case 'd': // Chapter Dynamic Allocation
*action = ExecuteDynArray;
return 0;
case 's': // Chapter Structure
*action = ExecuteStructure;
return 0;
case 'i': // Chapter Input/Output function
*action = ExecuteIAfunct;
return 0;
case 'f': // Chapter attributes of files and working with directories
*action = ExecuteAtFiDi;
return 0;
case 'w': // Chapter working with variable long argumentlists
*action = ExecuteWwvla;
return 0;
case 'm':
*action = ExecuteAhaf;
return 0;
case 'k':
*action = ExecuteDynDaStr;
return 0;
case 'r':
*action = ExecuteAlgo;
return 0;
case 'a':
*action = ExecuteAll;
return 0;
}
return II_NOINTERPRET;
}
int interpretExNo(const char* in, unsigned int insz, int* exampleNo)
{
unsigned int i = 2;
if (insz < 4 || in[1] != ' ')
return II_MALFORMED;
*exampleNo = 0;
while(i < insz -1)
{
if (in[i] < '0' || in[i] > '9')
return II_NAN;
*exampleNo *= 10;
*exampleNo += in[i]-'0';
i++;
if (in[i] == '\0')
return 0;
if (in[i] == ' ')
return IEN_WHITE_SPACE;
}
return IEN_NOINTERPRET;
}
//TODO Allow passing of input
int executeExample(int exNo)
{
int res = 0;
printout("\n========");
indentpush();
switch(exNo)
{
case 0:
res = ex0();
break;
default: res = EE_UNKNOWN_EXNO;
}
indentpop();
printout("========\n");
return res;
}
int executeDynArray(int exNo)
{
int res = 0;
printout("\n========");
indentpush();
switch(exNo)
{
case 0:
res=ex10();
break;
case 1:
res=ex11();
break;
case 2:
res=ex12();
break;
case 3:
res=ex13();
break;
case 4:
res=ex14();
break;
default: res = EE_UNKNOWN_EXNO;
}
indentpop();
printout("========\n");
return res;
}
int executeStructure(int exNo)
{
int res = 0;
printout("\n========");
indentpush();
switch(exNo)
{
case 0:
res=ex20();
break;
case 1:
res=ex21();
break;
case 2:
res=ex22();
break;
case 3:
res=ex23();
break;
case 4:
res=ex24();
break;
case 5:
res=ex25();
break;
case 6:
res=ex26();
break;
case 7:
res=ex27();
break;
case 8:
res=ex28();
break;
case 9:
res=ex29();
break;
case 10:
res=ex210();
break;
case 11:
res=ex211();
break;
default: res = EE_UNKNOWN_EXNO;
}
indentpop();
printout("========\n");
return res;
}
int executeIAfunct(int exNo)
{
int res = 0;
printout("\n========");
indentpush();
switch(exNo)
{
case 0:
res=ex30();
break;
case 1:
res=ex31();
break;
case 2:
res=ex32();
break;
case 3:
res=ex33();
break;
case 4:
res=ex34();
break;
case 5:
res=ex35();
break;
case 6:
res=ex36();
break;
case 7:
res=ex37();
break;
case 8:
res=ex38();
break;
case 9:
res=ex39();
break;
case 10:
res=ex310();
break;
case 11:
res=ex311();
break;
case 12:
res=ex312();
break;
case 13:
res=ex313();
break;
case 14:
res=ex314();
break;
case 15:
res=ex315();
break;
case 16:
res=ex316();
break;
case 17:
res=ex317();
break;
case 18:
res=ex318();
break;
case 19:
res=ex319();
break;
case 20:
res=ex320();
break;
case 21:
res=ex321();
break;
case 22:
res=ex322();
break;
case 23:
res=ex323();
break;
default: res = EE_UNKNOWN_EXNO;
}
indentpop();
printout("========\n");
return res;
}
int executeAtFiDi(int exNo)
{
int res = 0;
printout("\n========");
indentpush();
switch(exNo)
{
case 0:
res=ex40();
break;
case 1:
res=ex41();
break;
case 2:
res=ex42();
break;
case 3:
res=ex43();
break;
case 4:
res=ex44();
break;
default: res = EE_UNKNOWN_EXNO;
}
indentpop();
printout("========\n");
return res;
}
int executeWwvla(int exNo)
{
int res = 0;
printout("\n========");
indentpush();
switch(exNo)
{
case 0:
res=ex50();
break;
case 1:
res=ex51();
break;
case 2:
res=ex52();
break;
case 3:
res=ex53();
break;
case 4:
res=ex54();
break;
case 5:
res=ex55();
break;
case 6:
res=ex56();
break;
case 7:
res=ex57();
break;
case 8:
res=ex58();
break;
case 9:
res=ex59();
break;
default: res = EE_UNKNOWN_EXNO;
}
indentpop();
printout("========\n");
return res;
}
int executeAhaf(int exNo)
{
int res = 0;
printout("\n========");
indentpush();
switch(exNo)
{
case 0:
res=ex60();
break;
case 1:
fprintf(stderr, "Caused problems, not implemented\n");
//res=ex61();
break;
case 2:
fprintf(stderr, "Caused problems, not implemented\n");
//res=ex62();
break;
case 3:
res=ex63();
break;
case 4:
res=ex64();
break;
case 5:
res=ex65();
break;
case 6:
res=ex66();
break;
case 7:
res=ex67();
break;
case 8:
res=ex68();
break;
case 9:
res=ex69();
break;
case 10:
res=ex610();
break;
case 11:
res=ex611();
break;
case 12:
res=ex612();
break;
case 13:
res=ex613();
break;
case 14:
res=ex614();
break;
case 15:
res=ex615();
break;
case 16:
res=ex616();
break;
case 17:
res=ex617();
break;
default: res = EE_UNKNOWN_EXNO;
}
indentpop();
printout("========\n");
return res;
}
int executeDynDaStr(int exNo)
{
int res = 0;
printout("\n========");
indentpush();
switch(exNo)
{
case 0:
res=ex70();
break;
case 1:
res=ex71();
break;
case 2:
res=ex72();
break;
default: res = EE_UNKNOWN_EXNO;
}
indentpop();
printout("========\n");
return res;
}
int executeAlgo(int exNo)
{
int res = 0;
printout("\n========");
indentpush();
switch(exNo)
{
case 0:
res=ex80();
break;
case 1:
res=ex81();
break;
case 2:
res=ex82();
break;
case 3:
res=ex83();
break;
case 4:
res=ex84();
break;
case 5:
res=ex85();
break;
case 6:
res=ex86();
break;
case 7:
res=ex87();
break;
case 8:
res=ex88();
break;
case 9:
res=ex89();
break;
case 10:
res=ex810();
break;
case 11:
res=ex811();
break;
case 12:
res=ex812();
break;
case 13:
res=ex813();
break;
default: res = EE_UNKNOWN_EXNO;
}
indentpop();
printout("========\n");
return res;
}
int help(void)
{
printout("\n *************************************************");
printout("\n This is the Help-Text for using this Programm ");
printout("\n Syntax of the Programm is i.e.' t 0 ' for Example first function");
printout("\n --- You may find some programms implementd, following Rheinwerk Computing C von A bis Z ---");
printout("\n BASIC's: \n\t (h) Print this helptext \n\t (e) Exit the programm");
printout("\n (t) Execute Examples not mentioned in the Book \n\t (0) Hello World ");
printout("\n (d) Execute Examples of Chapter Dynamic Storage management"
"\n\t (0) Function realloc() with dynamic Storage reservation"
"\n\t (1) Function dyn_string1 dynamic storage for strings"
"\n\t (2) Function for dynamic 2 dim array"
"\n\t (3) Function to change allocated memory"
"\n\t (4) Splitting required memory");
printout("\n (s) Exectue programms of Chapter Structure"
"\n\t (0) First structure of data types "
"\n\t (1) Second structure with print to see"
"\n\t (2) Structure with pointer and Call-by-Value"
"\n\t (3) Structure with dynamic allocation and output fct"
"\n\t (4) Structure comparsion between two structures"
"\n\t (5) Array of strucutres"
"\n\t (6) Storage, sort, search and print of max 100 Adresses"
"\n\t (7) Organisation programm, 100 Adresses, 20 Meetings"
"\n\t (8) Difference between Struct and Union"
"\n\t (9) Enum as BOOL listing"
"\n\t (10) Adresses with typedef"
"\n\t (11) Comparesion between packed, enum and original space.");
printout("\n (i) Execute programs of Chapter Input/Output functions."
"\n\t (0) Open data with fopen "
"\n\t (1) Putchar funciton "
"\n\t (2) ECHO function with fgetc echo text to terminal"
"\n\t (3) Reading data by writing in Terminal or after start"
"\n\t (4) Copy a file char by char"
"\n\t (5) Input and formating csv file"
"\n\t (6) Flag EOF with feof "
"\n\t (7) ungetc, write last char bevor EOF in file"
"\n\t (8) Demonstration of fseek()"
"\n\t (9) right usage of fgets by string comparsion"
"\n\t (10) Reading the n'th line of file"
"\n\t (11) Reading from line 1 to line 2 of file"
"\n\t (12) Searching for a string in file"
"\n\t (13) Searching for a Word in file"
"\n\t (14) Store/print adresses with fwrite and fread"
"\n\t (15) Proof if Little or Big Endian"
"\n\t (16) unrestoreable delte"
"\n\t (17) Create file and tmp and change file"
"\n\t (18) Read and write formated strings with sscanf"
"\n\t From here on we only have low level functions:"
"\n\t (19) Function open() creats a testfile"
"\n\t (20) Function close() open() creates testfile"
"\n\t (21) Function write with static text from buffer"
"\n\t (22) Function customer storage with open(), read() and write()"
"\n\t (23) Function fileno and numbers of some files");
printout("\n (f) Execute programs of chapter Attributes of files and "
"directories (not ANSI C)"
"\n\t (0) Check which kind of file is input"
"\n\t (1) Create new directory"
"\n\t (2) Change directory and create a \"testfile\""
"\n\t (3) Delete created\"testfile\" in desired directory (only empty)"
"\n\t (4) Delete on unix non-empty directories");
printout("\n (w) Execute programs of chapter Working with variable long argument"
" lists and time routines"
"\n\t (0) Sum numbers with stdarg.h"
"\n\t (1) Sum a defined number of numbers"
"\n\t (2) Add multible strings"
"\n\t (3) Printf simulated with vprintf"
"\n\t (4) Error management"
"\n\t (5) Macro with abitrary many arguments"
"\n From here on the functions are from chapter time routines"
"\n\t (6) Function to print the system time"
"\n\t (7) Function to calc age in year month and day"
"\n\t (8) Function to calc Weekday out of date"
"\n\t (9) Print runtime of program");
printout("\n (m) Execute programs of chapter Additonal Header and functions (ANSI C)"
"\n\t (0) Error proof with assert.h (ignored with NDEBUG)"
"\n\t (1) Using math.h to calc some stuff (Not implemented)"
"\n\t (2) Using tgmath.h to calc some sqrt's (Not implemented)"
"\n\t (3) Check if double and if long value"
"\n\t (4) Convert Integer to string value"
"\n\t (5) Pseudo random numbers"
"\n\t (6) Random number with setted starting value"
"\n\t (7) Random number with function time()"
"\n\t (8) Sort values with qsort"
"\n\t (9) Search for a word in file with bsearch"
"\n\t (10) Jump through programm with setjmp.h"
"\n\t (11) A simple shell without functions"
"\n\t (12) trigger SIGINT with raise()"
"\n\t (13) Search for char with memchr()"
"\n\t (14) Compare a fixed number of bytes with memcmp()"
"\n\t (15) Repalce a word in a string with memcpy()"
"\n\t (16) Copy 10 bytes of string with memmove()"
"\n\t (17) Cut string with memset()");
printout("\n (k) Execute programs of chapter dynamic datastructures"
"\n\t (0) Add abitrary many structs to a chain (lin. list)"
"\n\t (1) count push and pop's"
"\n\t (2) FIFO queue");
printout("\n (r) Execute programs of chapter Algorithm"
"\n\t (0) Selection-Sort"
"\n\t (1) Insertion-Sort"
"\n\t (2) Bubble-Sort"
"\n\t (3) Shellsort"
"\n\t (4) Runtime comparsion between shellsort and insertion sort"
"\n\t (5) Qicksort with Runtime check"
"\n\t (6) Runtime comparsion between qsort() stdlib.h and selfmade qsort"
"\n\t (7) Linear searching for added ZIP-Codes"
"\n\t (8) Binary search of qsort()-sorted list"
"\n\t (9) Binary tree, function without any functions"
"\n\t (10) Zip-Code list with full binary tree"
"\n\t (11) Hashing strings with defined values"
"\n\t (12) kmp string search algorithm with fixed string and search"
"\n\t (13) Solving a way from starting point to target with barriers");
printout("\n**************************************************");
return 0;
}
int executeInput(II_Action action, const char* in, unsigned int insz)
{
int exNo;
switch(action)
{
case None:
return 0;
case Exit:
return EI_EXIT;
case Help:
return help();
case ExecuteExample:
if (interpretExNo(in, insz, &exNo) < 0)
return EI_EX_DETER_FAILED;
return executeExample(exNo);
case ExecuteDynArray:
if (interpretExNo(in, insz, &exNo) < 0)
return EI_EX_DETER_FAILED;
return executeDynArray(exNo);
case ExecuteStructure:
if (interpretExNo(in, insz, &exNo) < 0)
return EI_EX_DETER_FAILED;
return executeStructure(exNo);
case ExecuteIAfunct:
if(interpretExNo(in, insz, &exNo) < 0)
return EI_EX_DETER_FAILED;
return executeIAfunct(exNo);
case ExecuteAtFiDi:
if(interpretExNo(in, insz, &exNo) < 0)
return EI_EX_DETER_FAILED;
return executeAtFiDi(exNo);
case ExecuteWwvla:
if(interpretExNo(in, insz, &exNo) < 0)
return EI_EX_DETER_FAILED;
return executeWwvla(exNo);
case ExecuteAhaf:
if(interpretExNo(in, insz, &exNo) < 0)
return EI_EX_DETER_FAILED;
return executeAhaf(exNo);
case ExecuteDynDaStr:
if(interpretExNo(in, insz, &exNo) < 0)
return EI_EX_DETER_FAILED;
return executeDynDaStr(exNo);
case ExecuteAlgo:
if(interpretExNo(in, insz, &exNo) < 0)
return EI_EX_DETER_FAILED;
return executeAlgo(exNo);
case ExecuteAll:
//TODO
return 0;
}
return EI_UNKNOWN_ACTION;
}