libStatGen Software
1
|
00001 /* 00002 * Copyright (C) 2010 Regents of the University of Michigan 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #include "Input.h" 00019 #include "Error.h" 00020 #include "Constant.h" 00021 00022 #include <stdio.h> 00023 #include <string.h> 00024 00025 int InputPromptWidth = 25; 00026 00027 static bool safe_gets(char * buffer, int n) 00028 { 00029 buffer[0] = 0; 00030 00031 bool success = (fgets(buffer, n, stdin) != NULL); 00032 00033 for (char * ptr = buffer; *ptr != 0; ptr++) 00034 if (*ptr == '\n') 00035 *ptr = 0; 00036 00037 return success; 00038 } 00039 00040 void Input(const char * prompt, int & n, int _default) 00041 { 00042 char buffer[BUFSIZE]; 00043 00044 int success; 00045 do 00046 { 00047 printf("%*s [%8d]: ", InputPromptWidth, prompt, _default); 00048 safe_gets(buffer, BUFSIZE); 00049 success = sscanf(buffer, "%d", &n); 00050 if (success == EOF) 00051 n = _default; 00052 } 00053 while (success == 0); 00054 } 00055 00056 void Input(const char * prompt, char & ch, char _default) 00057 { 00058 char buffer[BUFSIZE]; 00059 00060 int success; 00061 do 00062 { 00063 printf("%*s [%8c]: ", InputPromptWidth, prompt, _default); 00064 safe_gets(buffer, BUFSIZE); 00065 success = sscanf(buffer, "%c", &ch); 00066 if (success == EOF) 00067 ch = _default; 00068 } 00069 while (success == 0); 00070 } 00071 00072 void Input(const char * prompt, double & d, double _default) 00073 { 00074 char buffer[BUFSIZE]; 00075 00076 int success; 00077 do 00078 { 00079 printf("%*s [%8.2f]: ", InputPromptWidth, prompt, _default); 00080 safe_gets(buffer, BUFSIZE); 00081 success = sscanf(buffer, "%lf", &d); 00082 if (success == EOF) 00083 d = _default; 00084 } 00085 while (success == 0); 00086 } 00087 00088 void Input(const char * prompt, bool & b, bool _default) 00089 { 00090 char buffer[BUFSIZE]; 00091 int success; 00092 char c; 00093 00094 do 00095 { 00096 printf("%*s [%8s]: ", InputPromptWidth, prompt, _default ? "Y/n" : "y/N"); 00097 safe_gets(buffer, BUFSIZE); 00098 success = sscanf(buffer, "%c", &c); 00099 if (success == EOF) 00100 b = _default; 00101 else 00102 switch (c) 00103 { 00104 case 'y' : 00105 case 'Y' : 00106 b = true; 00107 break; 00108 case 'n' : 00109 case 'N' : 00110 b = false; 00111 break; 00112 default : 00113 success = 0; 00114 } 00115 } 00116 while (success == 0); 00117 } 00118 00119 00120 void Input(const char * prompt, char * s, const char * _default) 00121 { 00122 char buffer[BUFSIZE]; 00123 00124 int success; 00125 do 00126 { 00127 printf("%*s [%8s]: ", InputPromptWidth, prompt, _default); 00128 safe_gets(buffer, BUFSIZE); 00129 success = sscanf(buffer, " %[^\n]", s); 00130 if (success == EOF) 00131 strcpy(s, _default); 00132 } 00133 while (success == 0); 00134 } 00135 00136 void InputBounds(const char * prompt, int & n, int min, int max, 00137 int _default) 00138 { 00139 Input(prompt, n, _default); 00140 while ((n < min) || (n > max)) 00141 { 00142 printf("\n*** Input value must be between %d and %d ***\n", min, max); 00143 Input(prompt, n, _default); 00144 } 00145 } 00146 00147 void InputBounds(const char * prompt, double & d, double min, double max, 00148 double _default) 00149 { 00150 Input(prompt, d, _default); 00151 while ((d < min) || (d > max)) 00152 { 00153 printf("\n*** Input value must be between %.2f and %.2f ***\n", min, max); 00154 Input(prompt, d, _default); 00155 } 00156 } 00157 00158