SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
query.c
Go to the documentation of this file.
1/**
2 * @file query.c
3 * @brief Provides functions to prompt the user for various types of numerical input.
4 *
5 * This file contains functions that display prompts to the terminal for different
6 * numerical types, show default values, and return the user-entered value or the
7 * default if no input is provided.
8 *
9 * @copyright
10 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
11 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
12 *
13 * @license
14 * This file is distributed under the terms of the Software License Agreement
15 * found in the file LICENSE included with this distribution.
16 *
17 * @author M. Borland, C. Saunders, R. Soliday
18 */
19
20#include "mdb.h"
21
22/**
23 * @brief Prompts the user for a double precision number.
24 *
25 * Displays a prompt with a default value. If the user does not enter a number,
26 * the default value is returned.
27 *
28 * @param prompt The message to display to the user.
29 * @param def The default double value to return if no input is provided.
30 * @return The double value entered by the user or the default value.
31 */
32double query_double(char *prompt, double def) {
33 char s[100];
34 double val;
35
36 printf("%s [%g]: ", prompt, def);
37 if (!fgets(s, sizeof(s), stdin))
38 return def;
39 val = def;
40 if (*s)
41 sscanf(s, "%lf", &val);
42 return (val);
43}
44
45/**
46 * @brief Prompts the user for a float precision number.
47 *
48 * Displays a prompt with a default value. If the user does not enter a number,
49 * the default value is returned.
50 *
51 * @param prompt The message to display to the user.
52 * @param def The default float value to return if no input is provided.
53 * @return The float value entered by the user or the default value.
54 */
55float query_float(char *prompt, float def) {
56 char s[100];
57 float val;
58
59 printf("%s [%g]: ", prompt, def);
60 if (!fgets(s, sizeof(s), stdin))
61 return def;
62 val = def;
63 if (*s)
64 sscanf(s, "%f", &val);
65 return (val);
66}
67
68/**
69 * @brief Prompts the user for a long integer.
70 *
71 * Displays a prompt with a default value. If the user does not enter a number,
72 * the default value is returned.
73 *
74 * @param prompt The message to display to the user.
75 * @param def The default long integer value to return if no input is provided.
76 * @return The long integer entered by the user or the default value.
77 */
78long query_long(char *prompt, long def) {
79 char s[100];
80 long val;
81
82 printf("%s [%ld]: ", prompt, def);
83 if (!fgets(s, sizeof(s), stdin))
84 return def;
85 val = def;
86 if (*s)
87 sscanf(s, "%ld", &val);
88 return (val);
89}
90
91/**
92 * @brief Prompts the user for an integer.
93 *
94 * Displays a prompt with a default value. If the user does not enter a number,
95 * the default value is returned.
96 *
97 * @param prompt The message to display to the user.
98 * @param def The default integer value to return if no input is provided.
99 * @return The integer entered by the user or the default value.
100 */
101int query_int(char *prompt, int def) {
102 char s[100];
103 int val;
104
105 printf("%s [%d]: ", prompt, def);
106 if (!fgets(s, sizeof(s), stdin))
107 return def;
108 val = def;
109 if (*s)
110 sscanf(s, "%d", &val);
111 return (val);
112}
113
114/**
115 * @brief Prompts the user for a short integer.
116 *
117 * Displays a prompt with a default value. If the user does not enter a number,
118 * the default value is returned.
119 *
120 * @param prompt The message to display to the user.
121 * @param def The default short integer value to return if no input is provided.
122 * @return The short integer entered by the user or the default value.
123 */
124short query_short(char *prompt, short def) {
125 char s[100];
126 short val;
127
128 printf("%s [%d]: ", prompt, def);
129 if (!fgets(s, sizeof(s), stdin))
130 return def;
131 val = def;
132 if (*s)
133 sscanf(s, "%hd", &val);
134 return (val);
135}
double query_double(char *prompt, double def)
Prompts the user for a double precision number.
Definition query.c:32
float query_float(char *prompt, float def)
Prompts the user for a float precision number.
Definition query.c:55
long query_long(char *prompt, long def)
Prompts the user for a long integer.
Definition query.c:78
short query_short(char *prompt, short def)
Prompts the user for a short integer.
Definition query.c:124
int query_int(char *prompt, int def)
Prompts the user for an integer.
Definition query.c:101