35 int n_slots_increment;
40void list_buffers(
void);
56 if (buffers == NULL) {
57 buffers =
tmalloc(
sizeof(*buffers));
59 buffers->n_slots = buffers->n_lines_stored = 0;
60 cp_str(&(buffers->name), name);
61 buffers->n_slots_increment = increment ? increment : 1;
63 printf(
"buffer %s created--increment is %d\n",
64 buffers->name, buffers->n_slots_increment);
67 if (find_buffer(name))
68 bomb(
"duplicate buffer creation (create_buffer)", NULL);
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;
77 printf(
"buffer %s created--increment is %d\n",
78 bptr->name, bptr->n_slots_increment);
98 if (!(bptr = find_buffer(name)))
99 bomb(
"unknown buffer referenced (add_to_buffer)", NULL);
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);
109 cp_str(bptr->line + bptr->n_lines_stored++,
string);
111 printf(
"\"%s\" added to buffer %s--total of %d lines\n",
112 string, buffers->name, buffers->n_lines_stored);
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;
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]);
154void fputs_buffer(
char *name, FILE *fp) {
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);
168 if ((bptr = buffers)) {
170 if (strcmp(name, bptr->name) == 0)
172 }
while ((bptr = bptr->next));
184 if (find_buffer(name))
190void list_buffers(
void) {
195 printf(
"buffer %s has %d lines\n", bptr->name, bptr->n_lines_stored);
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
int tfree(void *ptr)
Frees a memory block and records the deallocation if tracking is enabled.
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
void bomb(char *error, char *usage)
Reports error messages to the terminal and aborts the program.
int buffer_exists(char *name)
Checks if a buffer with the specified name exists.
void fprintf_buffer(FILE *fp, char *format, char *name)
Writes the contents of the specified buffer to a file using a specified format.
void create_buffer(char *name, int increment)
Creates a new buffer with the specified name and slot increment.
void clear_buffer(char *name)
Clears all strings from the specified buffer.
void add_to_buffer(char *name, char *string)
Adds a string to the specified buffer.
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.