Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / include / io_frontend.c @ f5a568dd

History | View | Annotate | Download (2.27 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
   int 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 (_Bool)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
  fflush(file);
71
}
72
void _put_int(FILE* file, char* n, int _V){
73
  if(ISATTY) {
74
    printf("%s = ", n);
75
  } else {
76
    printf("'%s': ", n);
77
  };
78
  printf("'%d' ", _V);
79
  printf("\n");
80
  fprintf(file, "%d\n", _V);
81
  fflush(file);
82
}
83

    
84
void _put_float(FILE* file, char* n, float _V, int PREC){
85
  if(ISATTY) {
86
    printf("%s = ", n);
87
  } else {
88
    printf("'%s': ", n);
89
  };
90
  printf("'%.*f' ", PREC, _V);
91
  printf("\n");
92
  fprintf(file, "%.*f\n", PREC, _V);
93
  fflush(file);
94
}
95

    
96
void _put_double(FILE* file, char* n, double _V, int PREC){
97
  if(ISATTY) {
98
    printf("%s = ", n);
99
  } else {
100
    printf("'%s': ", n);
101
  };
102
  printf("'%.*f' ", PREC, _V);
103
  printf("\n");
104
  fprintf(file, "%.*f\n", PREC, _V);
105
  fflush(file);
106
}