Project

General

Profile

Download (2.3 KB) Statistics
| Branch: | Tag: | Revision:
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
  fflush(file);
73
}
74

    
75
void _put_int(FILE* file, char* n, int _V){
76
  if(ISATTY) {
77
    printf("%s = ", n);
78
  } else {
79
    printf("'%s': ", n);
80
  };
81
  printf("'%d' ", _V);
82
  printf("\n");
83
  fprintf(file, "%d\n", _V);
84
  fflush(file);
85
}
86

    
87
void _put_float(FILE* file, char* n, float _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
  fprintf(file, "%.*f\n", PREC, _V);
96
  fflush(file);
97
}
98

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

    
111

    
112
#endif
(10-10/14)