1
|
#ifndef _IO_FRONTEND_HPP
|
2
|
#define _IO_FRONTEND_HPP
|
3
|
|
4
|
#include <stdlib.h> /* Provides exit */
|
5
|
#include <stdio.h> /* Provides printf, scanf, sscanf */
|
6
|
#include <unistd.h> /* Provides isatty */
|
7
|
|
8
|
int ISATTY;
|
9
|
|
10
|
/* Standard Input procedures **************/
|
11
|
bool _get_bool(char* n){
|
12
|
char b[512];
|
13
|
bool r = 0;
|
14
|
int s = 1;
|
15
|
char c;
|
16
|
do {
|
17
|
if(ISATTY) {
|
18
|
if((s != 1)||(r == -1)) printf("\a");
|
19
|
printf("%s (1,t,T/0,f,F) ? ", n);
|
20
|
}
|
21
|
if(scanf("%s", b)==EOF) exit(0);
|
22
|
s = sscanf(b, "%c", &c);
|
23
|
r = -1;
|
24
|
if((c == '0') || (c == 'f') || (c == 'F')) r = 0;
|
25
|
if((c == '1') || (c == 't') || (c == 'T')) r = 1;
|
26
|
} while((s != 1) || (r == -1));
|
27
|
return r;
|
28
|
}
|
29
|
|
30
|
int _get_int(char* n){
|
31
|
char b[512];
|
32
|
int r;
|
33
|
int s = 1;
|
34
|
do {
|
35
|
if(ISATTY) {
|
36
|
if(s != 1) printf("\a");
|
37
|
printf("%s (integer) ? ", n);
|
38
|
}
|
39
|
if(scanf("%s", b)==EOF) exit(0);
|
40
|
s = sscanf(b, "%d", &r);
|
41
|
} while(s != 1);
|
42
|
return r;
|
43
|
}
|
44
|
|
45
|
double _get_double(char* n){
|
46
|
char b[512];
|
47
|
double r;
|
48
|
int s = 1;
|
49
|
do {
|
50
|
if(ISATTY) {
|
51
|
if(s != 1) printf("\a");
|
52
|
printf("%s (double) ? ", n);
|
53
|
}
|
54
|
if(scanf("%s", b)==EOF) exit(0);
|
55
|
s = sscanf(b, "%lf", &r);
|
56
|
} while(s != 1);
|
57
|
return r;
|
58
|
}
|
59
|
/* Standard Output procedures **************/
|
60
|
void _put_bool(char* n, bool _V){
|
61
|
if(ISATTY) {
|
62
|
printf("%s = ", n);
|
63
|
} else {
|
64
|
printf("'%s': ", n);
|
65
|
};
|
66
|
printf("'%i' ", (_V)? 1 : 0);
|
67
|
}
|
68
|
void _put_int(char* n, int _V){
|
69
|
if(ISATTY) {
|
70
|
printf("%s = ", n);
|
71
|
} else {
|
72
|
printf("'%s': ", n);
|
73
|
};
|
74
|
printf("'%d' ", _V);
|
75
|
}
|
76
|
|
77
|
void _put_float(char* n, float _V, int PREC){
|
78
|
if(ISATTY) {
|
79
|
printf("%s = ", n);
|
80
|
} else {
|
81
|
printf("'%s': ", n);
|
82
|
};
|
83
|
printf("'%.*f' ", PREC, _V);
|
84
|
printf("\n");
|
85
|
}
|
86
|
|
87
|
void _put_double(char* n, double _V, int PREC){
|
88
|
if(ISATTY) {
|
89
|
printf("%s = ", n);
|
90
|
} else {
|
91
|
printf("'%s': ", n);
|
92
|
};
|
93
|
printf("'%.*f' ", PREC, _V);
|
94
|
printf("\n");
|
95
|
}
|
96
|
|
97
|
|
98
|
#endif
|