-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasyio.cpp
More file actions
760 lines (631 loc) · 18.4 KB
/
easyio.cpp
File metadata and controls
760 lines (631 loc) · 18.4 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
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/************************************************************************
THIS FILE COLLAPSES THREE MODULES, FORMERLY CALLED
EASYIO_PRIM.C, EASYIO_SCAN.C and EASYIO_READ.C
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "easyio.h"
/*----------------------------------------------------------------------*/
/* */
/* easyio_prim.C */
/* */
/* This module contains some procedures which help the input */
/* data process for an user program. */
/* Function contained are the following: */
/* - clear_screen : clear a page of the screen */
/* - readln : read the first character ignoring the */
/* remaining input stream until the */
/* following <CR> */
/* - beep : beeps */
/* - pausa : waits the user to press <CR> */
/* */
/*----------------------------------------------------------------------*/
void clear_screen() {
system("clear");
return;
}
char readln() {
char d = getchar();
char c = d;
while (d != '\n')
d = getchar();
return (c);
}
void beep() {
putchar(7);
return;
}
void pausa() {
fprintf(stdout, "\n\n");
fprintf(stdout, " ***** Press <RETURN> to continue *****");
readln();
clear_screen();
return;
}
/*----------------------------------------------------------------------*/
/* */
/* easyio_scan.C */
/* */
/* */
/* This module contains procedures similar to the scanf() function:*/
/* a variable is read from the standard input; once a valid value */
/* is given, this value is stored in the 'parm' pointed location, */
/* otherwise the parm pointer is unchanged. */
/* Functions: */
/* scandigit() */
/* scanint() */
/* scandouble() */
/* scanstring() */
/* Return values: */
/* - EASYIO_OKAY : the value is valid; */
/* - EASYIO_DEFAULT : the first character in input is <CR>; */
/* - EASYIO_ERROR : error detected: string too long or */
/* non valid characters included. */
/* As begin comment / EOL character, '#' is used. */
/* */
/*----------------------------------------------------------------------*/
//
// scandigit: max string length: 1 character;
// valid characters: '0' to '9'
//
int scandigit(int* parm) {
int c = readln();
switch (c) {
case '\n':
return(EASYIO_DEFAULT);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
*parm = c - '0';
return(EASYIO_OKAY);
default:
return(EASYIO_ERROR);
}
}
/*----------------------------------------------------------------------*/
//
// scanint: max string length: EASYIO_MAX_INT_LEN;
// valid characters: '0'-'9'
//
int scanint(int* parm) {
int c;
int buf[EASYIO_MAX_BUF_LEN];
int buf_len;
for (buf_len = 0; ((c = getchar()) != '\n') && (c != '#') &&
(buf_len < EASYIO_MAX_BUF_LEN); buf_len++) buf[buf_len] = c;
if (c == '#') c = readln(); /* empty buffer for next line */
switch (buf_len) {
case 0:
return(EASYIO_DEFAULT);
case EASYIO_MAX_BUF_LEN:
return(EASYIO_ERROR);
default:
break;
}
int intero = 0;
int count = 0;
short neg = 1;
for (int i = 0; i < buf_len; i++) {
c = buf[i];
if ((c != ' ') && (c != '\t')) {
if (count == -1) return(EASYIO_ERROR);
else count++;
}
if (count > EASYIO_MAX_INT_LEN)
return(EASYIO_ERROR);
if (c == '-')
neg = -1;
else if ((c >= '0') && (c <= '9'))
intero = (intero * 10) + buf[i] - '0';
else if ((c == ' ') || (c == '\t')) {
if (count != 0) count = -1;
}
else return(EASYIO_ERROR);
}
*parm = neg * intero;
return(EASYIO_OKAY);
}
/*----------------------------------------------------------------------*/
//
// scanlong: max string length: EASYIO_MAX_INT_LEN;
// valid characters: '0'-'9'
//
int scanlong(long* parm) {
int c;
int buf[EASYIO_MAX_BUF_LEN];
int buf_len;
for (buf_len = 0; ((c = getchar()) != '\n') && (c != '#') &&
(buf_len < EASYIO_MAX_BUF_LEN); buf_len++) buf[buf_len] = c;
if (c == '#') c = readln(); /* empty buffer for next line */
switch (buf_len) {
case 0:
return(EASYIO_DEFAULT);
case EASYIO_MAX_BUF_LEN:
return(EASYIO_ERROR);
default:
break;
}
long intero = 0;
int count = 0;
short neg = 1;
for (int i = 0; i < buf_len; i++) {
c = buf[i];
if ((c != ' ') && (c != '\t')) {
if (count == -1) return(EASYIO_ERROR);
else count++;
}
if (count > EASYIO_MAX_INT_LEN)
return(EASYIO_ERROR);
if (c == '-')
neg = -1;
else if ((c >= '0') && (c <= '9'))
intero = (intero * 10) + buf[i] - '0';
else if ((c == ' ') || (c == '\t')) {
if (count != 0) count = -1;
}
else return(EASYIO_ERROR);
}
*parm = neg * intero;
return(EASYIO_OKAY);
}
/*----------------------------------------------------------------------*/
//
// scandouble: max string length: EASYIO_MAX_FLOAT_LEN;
// valid characters: '0'-'9', '.'
//
int scandouble(double* parm) {
int c;
int buf_len;
int buf[EASYIO_MAX_BUF_LEN];
for (buf_len = 0; ((c = getchar()) != '\n') && (c != '#') &&
(buf_len < EASYIO_MAX_BUF_LEN); buf_len++) buf[buf_len] = c;
if (c == '#') c = readln(); /* empty buffer for next line */
switch (buf_len) {
case 0:
return(EASYIO_DEFAULT);
case EASYIO_MAX_BUF_LEN:
return(EASYIO_ERROR);
default:
break;
}
double mantissa = 0.0;
int count = 0;
int pot = 0;
double potenza = 1.0;
short neg = 1;
for (int i = 0; i < buf_len; i++) {
c = buf[i];
if ((c != ' ') && (c != '\t')) {
if (count == -1) return(EASYIO_ERROR);
else count++;
}
if (count > EASYIO_MAX_FLOAT_LEN)
return(EASYIO_ERROR);
if (c == '-')
neg = -1;
else if ((c >= '0') && (c <= '9')) {
mantissa = (mantissa * 10) + buf[i] - '0';
if (pot) potenza *= 10;
}
else if (c == '.') {
if ((buf[i + 1] < '0') || (buf[i + 1] > '9'))
return(EASYIO_ERROR);
if (pot != 0)
return(EASYIO_ERROR);
pot = 1;
}
else if ((c == ' ') || (c == '\t')) {
if (count != 0) count = -1;
}
else return(EASYIO_ERROR);
}
*parm = neg * mantissa / potenza;
return(EASYIO_OKAY);
}
/*----------------------------------------------------------------------*/
//
// scanstring: max string length: EASYIO_MAX_STR_LEN;
// valid characters: '0'-'9','.','a'-'z','A','Z'
//
int scanstring(char* parm) {
int c;
char buf[EASYIO_MAX_STR_LEN];
int buf_len = 0;
for (buf_len = 0; ((c = getchar()) != '\n') && (c != '#') &&
(buf_len < EASYIO_MAX_STR_LEN); buf_len++) {
buf[buf_len] = c;
if ((c != '.') && (c < '0') && (c > '9') &&
(c < 'a') && (c > 'z') && (c < 'A') &&
(c > 'Z')) return(EASYIO_ERROR);
}
if (c == '#') c = readln(); /* empty buffer for next line */
switch (buf_len) {
case 0:
return(EASYIO_DEFAULT);
case EASYIO_MAX_STR_LEN:
return(EASYIO_ERROR);
default:
buf[buf_len] = '\0';
//strcpy_s(parm, EASYIO_MAX_STR_LEN, buf);
#define _CRT_SECURE_NO_WARNINGS strcpy(parm, buf);
return(EASYIO_OKAY);
}
}
/*----------------------------------------------------------------------*/
/* */
/* easyio_read.C */
/* */
/* This module contains procedures which print on the standard */
/* output a string containing user prompt and proposed default */
/* value. */
/* a Read procedure (using the scan functions) is performed; in */
/* case of error or out of range value the read procedure is */
/* proposed again after a beep. A maximum number of attempts is */
/* defined to avoid loops in shells. */
/* read_bool() */
/* read_digit() */
/* read_int() */
/* read_double() */
/* read_string() */
/* */
/*----------------------------------------------------------------------*/
#define FPTR stderr
//
// read_bool:
// prompt: prompt user string;
// def : proposed default
//
short read_bool(const char* prompt, short def) {
int answer;
for (int timeout = 0; timeout <= 5; timeout++) {
if (def == false)
fprintf(FPTR, "%s (y/n) [NO] > ", prompt);
else
fprintf(FPTR, "%s (y/n) [YES] > ", prompt);
answer = readln();
switch (answer) {
case '\n':
return(def);
case 'y':
case 'Y':
return(true);
case 'n':
case 'N':
return(false);
default:
beep();
break;
}
}
fprintf(FPTR, "\n FATAL ERROR: too many wrong attempts ");
fprintf(FPTR, "in read_bool() \n");
exit(-1);
return(false); // not used: just to be nice with the compiler
}
//
// overloaded version without default
//
short read_bool(const char* prompt) {
return(read_bool(prompt, true));
}
/*----------------------------------------------------------------------*/
//
// read_digit:
// prompt: prompt user string;
// def : proposed default
// min : inferior limit
// max : superior limit
//
int read_digit(const char* prompt, int def, int min, int max) {
if (min < 0) min = 0;
if (max < 0) max = 0;
if (min > 9) min = 9;
if (max > 9) max = 9;
if (min > max) {
int tmp = min;
min = max;
max = tmp;
}
if (def < min) def = min;
if (def > max) def = max;
short esito;
int digit;
short failed = 0;
for (int timeout = 0; timeout <= 5; timeout++) {
fprintf(FPTR, "%s ", prompt);
if (failed) fprintf(FPTR, "(%d::%d) ", min, max);
fprintf(FPTR, "[%d] > ", def);
esito = scandigit(&digit);
switch (esito) {
case EASYIO_DEFAULT:
return(def);
case EASYIO_OKAY:
if ((digit >= min) && (digit <= max))
return(digit);
case EASYIO_ERROR:
beep();
failed = 1;
break;
}
}
fprintf(FPTR, "\n FATAL ERROR: too many wrong attempts ");
fprintf(FPTR, "in read_digit() \n");
exit(-1);
return(false); // not used: just to be nice with the compiler
}
//
// overloaded version with no check
//
int read_digit(const char* prompt, int def) {
return(read_digit(prompt, def, 0, 9));
}
//
// overloaded version with no check and default
//
int read_digit(const char* prompt) {
return(read_digit(prompt, 0, 0, 9));
}
/*----------------------------------------------------------------------*/
//
// read_int:
// prompt: prompt user string;
// unit : measurement unit of the reading value
// def : proposed default
// min : inferior limit
// max : superior limit
//
int read_int(const char* prompt, const char* unit, int def, int min, int max) {
if (min > max) {
int tmp = min;
min = max;
max = tmp;
}
if (def < min) def = min;
if (def > max) def = max;
int intero;
short esito;
short failed = 0;
for (int timeout = 0; timeout <= 5; timeout++) {
fprintf(FPTR, "%s ", prompt);
if (failed) fprintf(FPTR, "(%d::%d) ", min, max);
fprintf(FPTR, "[%d%s] > ", def, unit);
esito = scanint(&intero);
switch (esito) {
case EASYIO_DEFAULT:
return(def);
case EASYIO_OKAY:
if ((intero >= min) && (intero <= max))
return(intero);
case EASYIO_ERROR:
beep();
failed = 1;
break;
}
}
fprintf(FPTR, "\n FATAL ERROR: too many wrong attempts ");
fprintf(FPTR, "in read_int() \n");
exit(-1);
return(false); // not used: just to be nice with the compiler
}
//
// overloading without unit and/or range
//
int read_int(const char* prompt, int def, int min, int max) {
return(read_int(prompt, "", def, min, max));
}
int read_int(const char* prompt, const char* unit, int def) {
return(read_int(prompt, unit, def, EIO_MIN_INT, EIO_MAX_INT));
}
int read_int(const char* prompt, int def) {
return(read_int(prompt, def, EIO_MIN_INT, EIO_MAX_INT));
}
int read_int(const char* prompt) {
return(read_int(prompt, 0, EIO_MIN_INT, EIO_MAX_INT));
}
/*----------------------------------------------------------------------*/
//
// read_long:
// prompt: prompt user string;
// unit : measurement unit of the reading value
// def : proposed default
// min : inferior limit
// max : superior limit
//
long read_long(const char* prompt, const char* unit, long def, long min, long max) {
if (min > max) {
long tmp = min;
min = max;
max = tmp;
}
if (def < min) def = min;
if (def > max) def = max;
long intero;
short esito;
short failed = 0;
for (int timeout = 0; timeout <= 5; timeout++) {
fprintf(FPTR, "%s ", prompt);
if (failed) fprintf(FPTR, "(%ld::%ld) ", min, max);
fprintf(FPTR, "[%ld%s] > ", def, unit);
esito = scanlong(&intero);
switch (esito) {
case EASYIO_DEFAULT:
return(def);
case EASYIO_OKAY:
if ((intero >= min) && (intero <= max))
return(intero);
case EASYIO_ERROR:
beep();
failed = 1;
break;
}
}
fprintf(FPTR, "\n FATAL ERROR: too many wrong attempts ");
fprintf(FPTR, "in read_long() \n");
exit(-1);
return(false); // not used: just to be nice with the compiler
}
//
// overloading without unit and/or range
//
long read_long(const char* prompt, long def, long min, long max) {
return(read_long(prompt, "", def, min, max));
}
long read_long(const char* prompt, const char* unit, long def) {
return(read_long(prompt, unit, def, EIO_MIN_INT, EIO_MAX_INT));
}
long read_long(const char* prompt, long def) {
return(read_long(prompt, def, EIO_MIN_INT, EIO_MAX_INT));
}
long read_long(const char* prompt) {
return(read_long(prompt, 0, EIO_MIN_INT, EIO_MAX_INT));
}
/*----------------------------------------------------------------------*/
//
// read_double:
// prompt: prompt user string;
// unit : measurement unit of the reading value
// def : proposed default
// min : inferior limit
// max : superior limit
//
double read_double(const char* prompt, const char* unit, double def,
double min, double max) {
if (min > max) {
double tmp = min;
min = max;
max = tmp;
}
if (def < min) def = min;
if (def > max) def = max;
double doppio;
short esito;
short failed = 0;
for (int timeout = 0; timeout <= 5; timeout++) {
fprintf(FPTR, "%s ", prompt);
if (failed) fprintf(FPTR, "(%.4f::%.4f) ", min, max);
fprintf(FPTR, "[%.4f%s] > ", def, unit);
esito = scandouble(&doppio);
switch (esito) {
case EASYIO_DEFAULT:
return(def);
case EASYIO_OKAY:
if ((doppio >= min) && (doppio <= max))
return(doppio);
case EASYIO_ERROR:
beep();
failed = 1;
break;
}
}
fprintf(FPTR, "\n FATAL ERROR: too many wrong attempts ");
fprintf(FPTR, "in read_double() \n");
exit(-1);
return(false); // not used: just to be nice with the compiler
}
//
// overloading without unit and/or range
double read_double(const char* prompt, double def, double min, double max) {
return(read_double(prompt, "", def, min, max));
}
double read_double(const char* prompt, const char* unit, double def) {
return(read_double(prompt, unit, def, EIO_MIN_INT, EIO_MAX_INT));
}
double read_double(const char* prompt, double def) {
return(read_double(prompt, def, EIO_MIN_INT, EIO_MAX_INT));
}
double read_double(const char* prompt) {
return(read_double(prompt, 0, EIO_MIN_INT, EIO_MAX_INT));
}
/*----------------------------------------------------------------------*/
//
// read_string:
// prompt: prompt user string;
// def : proposed default
//
const char* read_string(const char* prompt, const char* def) {
short esito;
static char stringa[EASYIO_MAX_STR_LEN];
for (int timeout = 0; timeout <= 5; timeout++) {
fprintf(FPTR, "%s [%s] > ", prompt, def);
esito = scanstring(stringa);
switch (esito) {
case EASYIO_DEFAULT:
return(def);
case EASYIO_OKAY:
return(stringa);
case EASYIO_ERROR:
beep();
break;
}
}
fprintf(FPTR, "\n FATAL ERROR: too many wrong attempts ");
fprintf(FPTR, "in read_string() \n");
exit(-1);
return(false); // not used: just to be nice with the compiler
}
//
// overloading without default
//
const char* read_string(const char* prompt) {
return(read_string(prompt, ""));
}
/*----------------------------------------------------------------------*/
//
// read_bool:
// prompt: prompt user string;
// def : proposed default
// tru : inferior limit (true)
// fal : superior limit (false)
//
bool read_bool(const char* prompt, int def, int tru, int fal) {
if (tru > fal) {
int tmp = tru;
tru = fal;
fal = tmp;
}
if (def < tru) def = tru;
if (def > fal) def = fal;
int intero;
short esito;
short failed = 0;
for (int timeout = 0; timeout <= 5; timeout++) {
fprintf(FPTR, "%s ", prompt);
if (failed) fprintf(FPTR, "(%d::%d) ", tru, fal);
fprintf(FPTR, "[%d%s] > ", def,"");
esito = scanint(&intero);
switch (esito) {
case EASYIO_DEFAULT:
if (def == 0)
return(false);
else if (def == 1)
return(true);
case EASYIO_OKAY:
if (intero == 0)
return(false);
else if(intero == 1)
return(true);
case EASYIO_ERROR:
beep();
failed = 1;
break;
}
}
fprintf(FPTR, "\n FATAL ERROR: too many wrong attempts ");
fprintf(FPTR, "in read_bool() \n");
exit(-1);
return(false); // not used: just to be nice with the compiler
}
//
// overloading without unit and/or range
//
bool read_bool(const char* prompt, int def) {
return(read_bool(prompt, def, true, false));
}