libStatGen Software  1
Input.cpp
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "Input.h"
19 #include "Error.h"
20 #include "Constant.h"
21 
22 #include <stdio.h>
23 #include <string.h>
24 
25 int InputPromptWidth = 25;
26 
27 static bool safe_gets(char * buffer, int n)
28 {
29  buffer[0] = 0;
30 
31  bool success = (fgets(buffer, n, stdin) != NULL);
32 
33  for (char * ptr = buffer; *ptr != 0; ptr++)
34  if (*ptr == '\n')
35  *ptr = 0;
36 
37  return success;
38 }
39 
40 void Input(const char * prompt, int & n, int _default)
41 {
42  char buffer[BUFSIZE];
43 
44  int success;
45  do
46  {
47  printf("%*s [%8d]: ", InputPromptWidth, prompt, _default);
48  safe_gets(buffer, BUFSIZE);
49  success = sscanf(buffer, "%d", &n);
50  if (success == EOF)
51  n = _default;
52  }
53  while (success == 0);
54 }
55 
56 void Input(const char * prompt, char & ch, char _default)
57 {
58  char buffer[BUFSIZE];
59 
60  int success;
61  do
62  {
63  printf("%*s [%8c]: ", InputPromptWidth, prompt, _default);
64  safe_gets(buffer, BUFSIZE);
65  success = sscanf(buffer, "%c", &ch);
66  if (success == EOF)
67  ch = _default;
68  }
69  while (success == 0);
70 }
71 
72 void Input(const char * prompt, double & d, double _default)
73 {
74  char buffer[BUFSIZE];
75 
76  int success;
77  do
78  {
79  printf("%*s [%8.2f]: ", InputPromptWidth, prompt, _default);
80  safe_gets(buffer, BUFSIZE);
81  success = sscanf(buffer, "%lf", &d);
82  if (success == EOF)
83  d = _default;
84  }
85  while (success == 0);
86 }
87 
88 void Input(const char * prompt, bool & b, bool _default)
89 {
90  char buffer[BUFSIZE];
91  int success;
92  char c;
93 
94  do
95  {
96  printf("%*s [%8s]: ", InputPromptWidth, prompt, _default ? "Y/n" : "y/N");
97  safe_gets(buffer, BUFSIZE);
98  success = sscanf(buffer, "%c", &c);
99  if (success == EOF)
100  b = _default;
101  else
102  switch (c)
103  {
104  case 'y' :
105  case 'Y' :
106  b = true;
107  break;
108  case 'n' :
109  case 'N' :
110  b = false;
111  break;
112  default :
113  success = 0;
114  }
115  }
116  while (success == 0);
117 }
118 
119 
120 void Input(const char * prompt, char * s, const char * _default)
121 {
122  char buffer[BUFSIZE];
123 
124  int success;
125  do
126  {
127  printf("%*s [%8s]: ", InputPromptWidth, prompt, _default);
128  safe_gets(buffer, BUFSIZE);
129  success = sscanf(buffer, " %[^\n]", s);
130  if (success == EOF)
131  strcpy(s, _default);
132  }
133  while (success == 0);
134 }
135 
136 void InputBounds(const char * prompt, int & n, int min, int max,
137  int _default)
138 {
139  Input(prompt, n, _default);
140  while ((n < min) || (n > max))
141  {
142  printf("\n*** Input value must be between %d and %d ***\n", min, max);
143  Input(prompt, n, _default);
144  }
145 }
146 
147 void InputBounds(const char * prompt, double & d, double min, double max,
148  double _default)
149 {
150  Input(prompt, d, _default);
151  while ((d < min) || (d > max))
152  {
153  printf("\n*** Input value must be between %.2f and %.2f ***\n", min, max);
154  Input(prompt, d, _default);
155  }
156 }
157 
158