SDDSlib
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 static char s[100];
34 static double val;
35
36 printf("%s [%g]: ", prompt, def);
37 fgets(s, 99, stdin);
38 val = def;
39 if (*s)
40 sscanf(s, "%lf", &val);
41 return (val);
42}
43
44/**
45 * @brief Prompts the user for a float precision number.
46 *
47 * Displays a prompt with a default value. If the user does not enter a number,
48 * the default value is returned.
49 *
50 * @param prompt The message to display to the user.
51 * @param def The default float value to return if no input is provided.
52 * @return The float value entered by the user or the default value.
53 */
54float query_float(char *prompt, float def) {
55 static char s[100];
56 static float val;
57
58 printf("%s [%g]: ", prompt, def);
59 fgets(s, 99, stdin);
60 val = def;
61 if (*s)
62 sscanf(s, "%f", &val);
63 return (val);
64}
65
66/**
67 * @brief Prompts the user for a long integer.
68 *
69 * Displays a prompt with a default value. If the user does not enter a number,
70 * the default value is returned.
71 *
72 * @param prompt The message to display to the user.
73 * @param def The default long integer value to return if no input is provided.
74 * @return The long integer entered by the user or the default value.
75 */
76long query_long(char *prompt, long def) {
77 static char s[100];
78 static long val;
79
80 printf("%s [%ld]: ", prompt, def);
81 fgets(s, 99, stdin);
82 val = def;
83 if (*s)
84 sscanf(s, "%ld", &val);
85 return (val);
86}
87
88/**
89 * @brief Prompts the user for an integer.
90 *
91 * Displays a prompt with a default value. If the user does not enter a number,
92 * the default value is returned.
93 *
94 * @param prompt The message to display to the user.
95 * @param def The default integer value to return if no input is provided.
96 * @return The integer entered by the user or the default value.
97 */
98int query_int(char *prompt, int def) {
99 static char s[100];
100 static int val;
101
102 printf("%s [%d]: ", prompt, def);
103 fgets(s, 99, stdin);
104 val = def;
105 if (*s)
106 sscanf(s, "%d", &val);
107 return (val);
108}
109
110/**
111 * @brief Prompts the user for a short integer.
112 *
113 * Displays a prompt with a default value. If the user does not enter a number,
114 * the default value is returned.
115 *
116 * @param prompt The message to display to the user.
117 * @param def The default short integer value to return if no input is provided.
118 * @return The short integer entered by the user or the default value.
119 */
120short query_short(char *prompt, short def) {
121 static char s[100];
122 static short val;
123
124 printf("%s [%d]: ", prompt, def);
125 fgets(s, 99, stdin);
126 val = def;
127 if (*s)
128 sscanf(s, "%hd", &val);
129 return (val);
130}
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:54
long query_long(char *prompt, long def)
Prompts the user for a long integer.
Definition query.c:76
short query_short(char *prompt, short def)
Prompts the user for a short integer.
Definition query.c:120
int query_int(char *prompt, int def)
Prompts the user for an integer.
Definition query.c:98