Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / include / io_frontend.c @ 22fe1c93

History | View | Annotate | Download (1.72 KB)

1
#include <stdlib.h> /* Provides exit */
2
#include <stdio.h> /* Provides printf, scanf, sscanf */
3
#include <unistd.h> /* Provides isatty */
4
#include "io_frontend.h"
5

    
6
int ISATTY;
7

    
8
/* Standard Input procedures **************/
9
_Bool _get_bool(char* n){
10
   char b[512];
11
   _Bool r = 0;
12
   int s = 1;
13
   char c;
14
   do {
15
      if(ISATTY) {
16
         if((s != 1)||(r == -1)) printf("\a");
17
         printf("%s (1,t,T/0,f,F) ? ", n);
18
      }
19
      if(scanf("%s", b)==EOF) exit(0);
20
      s = sscanf(b, "%c", &c);
21
      r = -1;
22
      if((c == '0') || (c == 'f') || (c == 'F')) r = 0;
23
      if((c == '1') || (c == 't') || (c == 'T')) r = 1;
24
   } while((s != 1) || (r == -1));
25
   return r;
26
}
27
int _get_int(char* n){
28
   char b[512];
29
   int r;
30
   int s = 1;
31
   do {
32
      if(ISATTY) {
33
         if(s != 1) printf("\a");
34
         printf("%s (integer) ? ", n);
35
      }
36
      if(scanf("%s", b)==EOF) exit(0);
37
      s = sscanf(b, "%d", &r);
38
   } while(s != 1);
39
   return r;
40
}
41
double _get_double(char* n){
42
   char b[512];
43
   double r;
44
   int s = 1;
45
   do {
46
      if(ISATTY) {
47
         if(s != 1) printf("\a");
48
         printf("%s (double) ? ", n);
49
      }
50
      if(scanf("%s", b)==EOF) exit(0);
51
      s = sscanf(b, "%lf", &r);
52
   } while(s != 1);
53
   return r;
54
}
55
/* Standard Output procedures **************/
56
void _put_bool(char* n, _Bool _V){
57
  if(ISATTY) {
58
    printf("%s = ", n);
59
  } else {
60
    printf("'%s': ", n);
61
  };
62
  printf("'%i' ", (_V)? 1 : 0);
63
  printf("\n");
64
}
65
void _put_int(char* n, int _V){
66
  if(ISATTY) {
67
    printf("%s = ", n);
68
  } else {
69
    printf("'%s': ", n);
70
  };
71
  printf("'%d' ", _V);
72
  printf("\n");
73
}
74
void _put_double(char* n, double _V){
75
  if(ISATTY) {
76
    printf("%s = ", n);
77
  } else {
78
    printf("'%s': ", n);
79
  };
80
  printf("'%f' ", _V);
81
  printf("\n");
82
}