SDDSlib
Loading...
Searching...
No Matches
bomb.c
Go to the documentation of this file.
1/**
2 * @file bomb.c
3 * @brief Provides functions to report errors and abort the program.
4 *
5 * @copyright
6 * - (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
7 * - (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
8 *
9 * @license
10 * This file is distributed under the terms of the Software License Agreement
11 * found in the file LICENSE included with this distribution.
12 *
13 * @author M. Borland, C. Saunders, R. Soliday
14 */
15
16#include "mdb.h"
17
18/**
19 * @brief Reports error messages to the terminal and aborts the program.
20 *
21 * This function prints the provided error and usage messages to the standard error stream and then terminates the program.
22 *
23 * @param error A string describing the error.
24 * @param usage A string describing the correct usage.
25 */
26void bomb(char *error, char *usage) {
27 if (error)
28 fprintf(stderr, "error: %s\n", error);
29 if (usage)
30 fprintf(stderr, "usage: %s\n", usage);
31 exit(1);
32}
33
34/**
35 * @brief Reports error messages to the terminal and returns a specified value.
36 *
37 * This function prints the provided error and usage messages to the standard error stream and returns the given return value.
38 *
39 * @param error A string describing the error.
40 * @param usage A string describing the correct usage.
41 * @param return_value The value to return after reporting the messages.
42 * @return The specified return value.
43 */
44long bombre(char *error, char *usage, long return_value) {
45 if (error)
46 fprintf(stderr, "error: %s\n", error);
47 if (usage)
48 fprintf(stderr, "usage: %s\n", usage);
49 return return_value;
50}
51
52/**
53 * @brief Reports error messages using a printf-style template and aborts the program.
54 *
55 * This function accepts a format string and a variable number of arguments, formats the message accordingly, prints it to the standard output, and then terminates the program.
56 *
57 * @param template A printf-style format string.
58 * @param ... Variable arguments corresponding to the format string.
59 */
60void bombVA(char *template, ...) {
61 char *p;
62 char c, *s;
63 int i;
64 long j;
65 va_list argp;
66 double d;
67
68 va_start(argp, template);
69 p = template;
70 while (*p) {
71 if (*p == '%') {
72 switch (*++p) {
73 case 'l':
74 switch (*++p) {
75 case 'd':
76 j = va_arg(argp, long int);
77 printf("%ld", j);
78 break;
79 case 'e':
80 d = va_arg(argp, double);
81 printf("%21.15le", d);
82 break;
83 case 'f':
84 d = va_arg(argp, double);
85 printf("%lf", d);
86 break;
87 case 'g':
88 d = va_arg(argp, double);
89 printf("%21.15lg", d);
90 break;
91 default:
92 printf("%%l%c", *p);
93 break;
94 }
95 break;
96 case 'c':
97 c = va_arg(argp, int);
98 putchar(c);
99 break;
100 case 'd':
101 i = va_arg(argp, int);
102 printf("%d", i);
103 break;
104 case 's':
105 s = va_arg(argp, char *);
106 fputs(s, stdout);
107 break;
108 case 'e':
109 d = va_arg(argp, double);
110 printf("%21.15e", d);
111 break;
112 case 'f':
113 d = va_arg(argp, double);
114 printf("%f", d);
115 break;
116 case 'g':
117 d = va_arg(argp, double);
118 printf("%21.15g", d);
119 break;
120 default:
121 printf("%%%c", *p);
122 break;
123 }
124 } else {
125 putchar(*p);
126 }
127 p++;
128 }
129 va_end(argp);
130 exit(1);
131}
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
long bombre(char *error, char *usage, long return_value)
Reports error messages to the terminal and returns a specified value.
Definition bomb.c:44
void bombVA(char *template,...)
Reports error messages using a printf-style template and aborts the program.
Definition bomb.c:60