Project

General

Profile

Download (1.92 KB) Statistics
| Branch: | Tag: | Revision:
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
   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
   return (_Bool)r;
26
}
27

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

    
43
double _get_double(char* n){
44
   char b[512];
45
   double r;
46
   int s = 1;
47
   do {
48
      if(ISATTY) {
49
         if(s != 1) printf("\a");
50
         printf("%s (double) ? ", n);
51
      }
52
      if(scanf("%s", b)==EOF) exit(0);
53
      s = sscanf(b, "%lf", &r);
54
   } while(s != 1);
55
   return r;
56
}
57
/* Standard Output procedures **************/
58
void _put_bool(char* n, _Bool _V){
59
  if(ISATTY) {
60
    printf("%s = ", n);
61
  } else {
62
    printf("'%s': ", n);
63
  };
64
  printf("'%i' ", (_V)? 1 : 0);
65
  printf("\n");
66
}
67
void _put_int(char* n, int _V){
68
  if(ISATTY) {
69
    printf("%s = ", n);
70
  } else {
71
    printf("'%s': ", n);
72
  };
73
  printf("'%d' ", _V);
74
  printf("\n");
75
}
76

    
77
void _put_float(char* n, float _V, int PREC){
78
  if(ISATTY) {
79
    printf("%s = ", n);
80
  } else {
81
    printf("'%s': ", n);
82
  };
83
  printf("'%.*f' ", PREC, _V);
84
  printf("\n");
85
}
86

    
87
void _put_double(char* n, double _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
}
(9-9/18)