SDDSlib
Loading...
Searching...
No Matches
headers.c File Reference

Provides functions to manage and format table header strings. More...

#include "mdb.h"

Go to the source code of this file.

Functions

void add_to_headers (char **header, long n_headers, char **item, long min_width, long format)
 
long format_length (char *format)
 
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.
 

Detailed Description

Provides functions to manage and format table header strings.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file headers.c.

Function Documentation

◆ add_to_headers()

void add_to_headers ( char ** header,
long n_headers,
char ** item,
long min_width,
long format )

Definition at line 18 of file headers.c.

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}

◆ add_to_standard_headers()

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.

This function facilitates adding a new set of standard headers, including name, unit, and format strings, to the respective header arrays. It ensures that the new headers conform to the specified minimum width.

Parameters
name_headerThe header string for names.
unit_headerThe header string for units.
printf_stringThe header string for printf format specifiers.
new_nameThe new name to add to the name header.
new_unitThe new unit to add to the unit header.
new_formatThe new format specifier to add to the printf string header.
min_widthThe minimum acceptable width for the new headers.

Definition at line 96 of file headers.c.

103 {
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}

◆ format_length()

long format_length ( char * format)

Definition at line 70 of file headers.c.

70 {
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}
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26