SDDSlib
Loading...
Searching...
No Matches
buffer.c
Go to the documentation of this file.
1/**
2 * @file buffer.c
3 * @brief Provides buffering of text strings.
4 *
5 * This file contains functions to create, manage, and manipulate buffers that store
6 * lines of text strings. Buffers can be dynamically created, added to, cleared, and
7 * printed to files.
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 C. Saunders, R. Soliday
18 */
19
20/* file: buffer.c
21 * purpose: provide buffering of text strings
22 *
23 * Michael Borland, 1990
24 */
25#include "mdb.h"
26#include "buffer.h"
27
28#define DEBUG 0
29
31 char *name;
32 char **line;
33 int n_lines_stored;
34 int n_slots;
35 int n_slots_increment;
36 struct buffer_struct *next;
37} *buffers = NULL;
38
39#if DEBUG
40void list_buffers(void);
41#endif
42
43/**
44 * @brief Creates a new buffer with the specified name and slot increment.
45 *
46 * If a buffer with the given name already exists, the function will terminate
47 * the program with an error message.
48 *
49 * @param name The name of the buffer to create.
50 * @param increment The number of slots to allocate when expanding the buffer. If zero,
51 * defaults to 1.
52 */
53void create_buffer(char *name, int increment) {
54 struct buffer_struct *bptr;
55
56 if (buffers == NULL) {
57 buffers = tmalloc(sizeof(*buffers));
58 buffers->next = NULL;
59 buffers->n_slots = buffers->n_lines_stored = 0;
60 cp_str(&(buffers->name), name);
61 buffers->n_slots_increment = increment ? increment : 1;
62#if DEBUG
63 printf("buffer %s created--increment is %d\n",
64 buffers->name, buffers->n_slots_increment);
65#endif
66 } else {
67 if (find_buffer(name))
68 bomb("duplicate buffer creation (create_buffer)", NULL);
69 bptr = buffers;
70 while (bptr->next)
71 bptr = bptr->next;
72 bptr = bptr->next = tmalloc(sizeof(*buffers));
73 bptr->n_lines_stored = bptr->n_slots = 0;
74 cp_str(&(bptr->name), name);
75 bptr->n_slots_increment = increment ? increment : 1;
76#if DEBUG
77 printf("buffer %s created--increment is %d\n",
78 bptr->name, bptr->n_slots_increment);
79#endif
80 }
81#if DEBUG
82 list_buffers();
83#endif
84}
85
86/**
87 * @brief Adds a string to the specified buffer.
88 *
89 * If the buffer does not have enough slots to store the new string, it will be expanded
90 * by the buffer's slot increment value.
91 *
92 * @param name The name of the buffer to add the string to.
93 * @param string The string to add to the buffer.
94 */
95void add_to_buffer(char *name, char *string) {
96 struct buffer_struct *bptr;
97
98 if (!(bptr = find_buffer(name)))
99 bomb("unknown buffer referenced (add_to_buffer)", NULL);
100
101 if (bptr->n_slots == 0)
102 bptr->line = (char **)tmalloc(sizeof(*bptr->line) *
103 (bptr->n_slots = bptr->n_slots_increment));
104 else if (bptr->n_lines_stored == bptr->n_slots) {
105 if (!(bptr->line = (char **)trealloc(bptr->line, sizeof(*bptr->line) *
106 (bptr->n_slots += bptr->n_slots_increment))))
107 bomb("trealloc failure in add_to_buffer()", NULL);
108 }
109 cp_str(bptr->line + bptr->n_lines_stored++, string);
110#if DEBUG
111 printf("\"%s\" added to buffer %s--total of %d lines\n",
112 string, buffers->name, buffers->n_lines_stored);
113#endif
114}
115
116/**
117 * @brief Clears all strings from the specified buffer.
118 *
119 * Frees all memory allocated for the strings in the buffer and resets the line count.
120 *
121 * @param name The name of the buffer to clear.
122 */
123void clear_buffer(char *name) {
124 int i;
125 struct buffer_struct *bptr;
126
127 if (!(bptr = find_buffer(name)))
128 bomb("unknown buffer referenced (clear_buffer)", NULL);
129 for (i = 0; i < bptr->n_lines_stored; i++)
130 tfree(bptr->line[i]);
131 bptr->n_lines_stored = 0;
132}
133
134/**
135 * @brief Writes the contents of the specified buffer to a file using a specified format.
136 *
137 * Each line in the buffer is printed to the file according to the provided format string.
138 *
139 * @param fp The file pointer to write the buffer contents to.
140 * @param format The format string to use with fprintf for each line.
141 * @param name The name of the buffer to write.
142 */
143void fprintf_buffer(FILE *fp, char *format, char *name) {
144 struct buffer_struct *bptr;
145 int i;
146
147 if (!(bptr = find_buffer(name)))
148 bomb("unknown buffer referenced (fprintf_buffer)", NULL);
149 for (i = 0; i < bptr->n_lines_stored; i++) {
150 fprintf(fp, format, bptr->line[i]);
151 }
152}
153
154void fputs_buffer(char *name, FILE *fp) {
155 struct buffer_struct *bptr;
156 int i;
157
158 if (!(bptr = find_buffer(name)))
159 bomb("unknown buffer referenced (fputs_buffer)", NULL);
160 for (i = 0; i < bptr->n_lines_stored; i++) {
161 fputs(bptr->line[i], fp);
162 }
163}
164
165struct buffer_struct *find_buffer(char *name) {
166 struct buffer_struct *bptr;
167
168 if ((bptr = buffers)) {
169 do {
170 if (strcmp(name, bptr->name) == 0)
171 return (bptr);
172 } while ((bptr = bptr->next));
173 }
174 return (NULL);
175}
176
177/**
178 * @brief Checks if a buffer with the specified name exists.
179 *
180 * @param name The name of the buffer to check.
181 * @return 1 if the buffer exists, 0 otherwise.
182 */
183int buffer_exists(char *name) {
184 if (find_buffer(name))
185 return (1);
186 return (0);
187}
188
189#if DEBUG
190void list_buffers(void) {
191 struct buffer_struct *bptr;
192 bptr = buffers;
193
194 while (bptr) {
195 printf("buffer %s has %d lines\n", bptr->name, bptr->n_lines_stored);
196 bptr = bptr->next;
197 }
198}
199#endif
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:181
int tfree(void *ptr)
Frees a memory block and records the deallocation if tracking is enabled.
Definition array.c:230
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
Definition bomb.c:26
int buffer_exists(char *name)
Checks if a buffer with the specified name exists.
Definition buffer.c:183
void fprintf_buffer(FILE *fp, char *format, char *name)
Writes the contents of the specified buffer to a file using a specified format.
Definition buffer.c:143
void create_buffer(char *name, int increment)
Creates a new buffer with the specified name and slot increment.
Definition buffer.c:53
void clear_buffer(char *name)
Clears all strings from the specified buffer.
Definition buffer.c:123
void add_to_buffer(char *name, char *string)
Adds a string to the specified buffer.
Definition buffer.c:95
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
Definition cp_str.c:28