Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / include / io_frontend.hpp @ 52c5ba00

History | View | Annotate | Download (1.99 KB)

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(FILE* file, 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
   fprintf(file, "%i\n",r);
28
   return r;
29
}
30

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

47
double _get_double(FILE* file, char* n){
48
   char b[512];
49
   double r;
50
   int s = 1;
51
   do {
52
      if(ISATTY) {
53
         if(s != 1) printf("\a");
54
         printf("%s (double) ? ", n);
55
      }
56
      if(scanf("%s", b)==EOF) exit(0);
57
      s = sscanf(b, "%lf", &r);
58
   } while(s != 1);
59
   fprintf(file, "%f\n", r);
60
   return r;
61
}
62
/* Standard Output procedures **************/
63
void _put_bool(FILE* file, char* n, bool _V){
64
  if(ISATTY) {
65
    printf("%s = ", n);
66
  } else {
67
    printf("'%s': ", n);
68
  };
69
  printf("'%i' ", (_V)? 1 : 0);
70
  printf("\n");
71
  fprintf(file, "%i\n", _V);
72
}
73
void _put_int(FILE* file, char* n, int _V){
74
  if(ISATTY) {
75
    printf("%s = ", n);
76
  } else {
77
    printf("'%s': ", n);
78
  };
79
  printf("'%d' ", _V);
80
  printf("\n");
81
  fprintf(file, "%d\n", _V);
82
}
83
void _put_double(FILE* file, char* n, double _V){
84
  if(ISATTY) {
85
    printf("%s = ", n);
86
  } else {
87
    printf("'%s': ", n);
88
  };
89
  printf("'%f' ", _V);
90
  printf("\n");
91
  fprintf(file, "%f\n", _V);
92
}
93

94
#endif
95