Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / include / io_frontend.c @ 6fa45cb6

History | View | Annotate | Download (1.96 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(FILE* file, 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
   fprintf(file, "%i\n",r);
26
   return r;
27
}
28

    
29
int _get_int(FILE* file, char* n){
30
   char b[512];
31
   int r;
32
   int s = 1;
33
   do {
34
      if(ISATTY) {
35
         if(s != 1) printf("\a");
36
         printf("%s (integer) ? ", n);
37
      }
38
      if(scanf("%s", b)==EOF) exit(0);
39
      s = sscanf(b, "%d", &r);
40
   } while(s != 1);
41
   fprintf(file, "%d\n", r);
42
   return r;
43
}
44

    
45
double _get_double(FILE* file, 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
   fprintf(file, "%f\n", r);
58
   return r;
59
}
60
/* Standard Output procedures **************/
61
void _put_bool(FILE* file, char* n, _Bool _V){
62
  if(ISATTY) {
63
    printf("%s = ", n);
64
  } else {
65
    printf("'%s': ", n);
66
  };
67
  printf("'%i' ", (_V)? 1 : 0);
68
  printf("\n");
69
  fprintf(file, "%i\n", _V);
70
}
71
void _put_int(FILE* file, char* n, int _V){
72
  if(ISATTY) {
73
    printf("%s = ", n);
74
  } else {
75
    printf("'%s': ", n);
76
  };
77
  printf("'%d' ", _V);
78
  printf("\n");
79
  fprintf(file, "%d\n", _V);
80
}
81
void _put_double(FILE* file, char* n, double _V){
82
  if(ISATTY) {
83
    printf("%s = ", n);
84
  } else {
85
    printf("'%s': ", n);
86
  };
87
  printf("'%f' ", _V);
88
  printf("\n");
89
  fprintf(file, "%f\n", _V);
90
}