SDDSlib
Loading...
Searching...
No Matches
headers.c
Go to the documentation of this file.
1/**
2 * @file headers.c
3 * @brief Provides functions to manage and format table header strings.
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
18void add_to_headers(
19 char **header, /* array of header strings */
20 long n_headers,
21 char **item, /* array of items to use for new column */
22 long min_width, /* minimum acceptable width of new column */
23 long format /* index of item that is a C format specifier */
24) {
25 long max_strlen;
26 long i, j, len, excess, width;
27 char *ptr;
28
29 max_strlen = min_width;
30 for (i = 0; i < n_headers; i++) {
31 if (format == i) {
32 width = format_length(item[i]) + 2;
33 if (width > max_strlen)
34 max_strlen = width;
35 } else if ((len = strlen(item[i])) > max_strlen)
36 max_strlen = len;
37 }
38
39 for (i = 0; i < n_headers; i++) {
40 if (i != format) {
41 excess = max_strlen - strlen(item[i]);
42 len = excess / 2.0 + 0.5;
43 ptr = header[i] + strlen(header[i]);
44 for (j = 0; j < len; j++, ptr++)
45 *ptr = ' ';
46 *ptr = 0;
47 excess -= len;
48 strcat(header[i], item[i]);
49 ptr = header[i] + strlen(header[i]);
50 for (j = 0; j < excess; j++, ptr++)
51 *ptr = ' ';
52 *ptr = 0;
53 } else {
54 excess = max_strlen - format_length(item[i]);
55 len = excess / 2;
56 ptr = header[i] + strlen(header[i]);
57 for (j = 0; j < len; j++, ptr++)
58 *ptr = ' ';
59 *ptr = 0;
60 strcat(ptr, item[i]);
61 excess -= len;
62 ptr = header[i] + strlen(header[i]);
63 for (j = 0; j < excess; j++, ptr++)
64 *ptr = ' ';
65 *ptr = 0;
66 }
67 }
68}
69
70long format_length(char *format) {
71 char *ptr;
72 long width = 0;
73
74 ptr = format;
75 if (*ptr != '%' || *(ptr + 1) == '%' || 1 != sscanf(ptr + 1, "%ld", &width) || width <= 0)
76 bomb("format specifier invalid", NULL);
77
78 return (width);
79}
80
81/**
82 * @brief Adds new standard headers to name, unit, and printf string headers.
83 *
84 * This function facilitates adding a new set of standard headers, including name, unit, and
85 * format strings, to the respective header arrays. It ensures that the new headers conform
86 * to the specified minimum width.
87 *
88 * @param name_header The header string for names.
89 * @param unit_header The header string for units.
90 * @param printf_string The header string for printf format specifiers.
91 * @param new_name The new name to add to the name header.
92 * @param new_unit The new unit to add to the unit header.
93 * @param new_format The new format specifier to add to the printf string header.
94 * @param min_width The minimum acceptable width for the new headers.
95 */
97 char *name_header,
98 char *unit_header,
99 char *printf_string,
100 char *new_name,
101 char *new_unit,
102 char *new_format,
103 long min_width) {
104 char *header[3];
105 char *item[3];
106
107 header[0] = name_header;
108 header[1] = unit_header;
109 header[2] = printf_string;
110 item[0] = new_name;
111 item[1] = new_unit;
112 item[2] = new_format;
113 add_to_headers(header, 3, item, min_width, 2);
114}
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
void add_to_standard_headers(char *name_header, char *unit_header, char *printf_string, char *new_name, char *new_unit, char *new_format, long min_width)
Adds new standard headers to name, unit, and printf string headers.
Definition headers.c:96