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

Provides buffering of text strings. More...

#include "mdb.h"
#include "buffer.h"

Go to the source code of this file.

Classes

struct  buffer_struct
 

Macros

#define DEBUG   0
 

Functions

void create_buffer (char *name, int increment)
 Creates a new buffer with the specified name and slot increment.
 
void add_to_buffer (char *name, char *string)
 Adds a string to the specified buffer.
 
void clear_buffer (char *name)
 Clears all strings from the specified buffer.
 
void fprintf_buffer (FILE *fp, char *format, char *name)
 Writes the contents of the specified buffer to a file using a specified format.
 
void fputs_buffer (char *name, FILE *fp)
 
struct buffer_structfind_buffer (char *name)
 
int buffer_exists (char *name)
 Checks if a buffer with the specified name exists.
 

Variables

struct buffer_structbuffers = NULL
 

Detailed Description

Provides buffering of text strings.

This file contains functions to create, manage, and manipulate buffers that store lines of text strings. Buffers can be dynamically created, added to, cleared, and printed to files.

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

Definition in file buffer.c.

Macro Definition Documentation

◆ DEBUG

#define DEBUG   0

Definition at line 28 of file buffer.c.

Function Documentation

◆ add_to_buffer()

void add_to_buffer ( char * name,
char * string )

Adds a string to the specified buffer.

If the buffer does not have enough slots to store the new string, it will be expanded by the buffer's slot increment value.

Parameters
nameThe name of the buffer to add the string to.
stringThe string to add to the buffer.

Definition at line 95 of file buffer.c.

95 {
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}
void * trealloc(void *old_ptr, uint64_t size_of_block)
Reallocates a memory block to a new size.
Definition array.c:181
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
char * cp_str(char **s, char *t)
Copies a string, allocating memory for storage.
Definition cp_str.c:28

◆ buffer_exists()

int buffer_exists ( char * name)

Checks if a buffer with the specified name exists.

Parameters
nameThe name of the buffer to check.
Returns
1 if the buffer exists, 0 otherwise.

Definition at line 183 of file buffer.c.

183 {
184 if (find_buffer(name))
185 return (1);
186 return (0);
187}

◆ clear_buffer()

void clear_buffer ( char * name)

Clears all strings from the specified buffer.

Frees all memory allocated for the strings in the buffer and resets the line count.

Parameters
nameThe name of the buffer to clear.

Definition at line 123 of file buffer.c.

123 {
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}
int tfree(void *ptr)
Frees a memory block and records the deallocation if tracking is enabled.
Definition array.c:230

◆ create_buffer()

void create_buffer ( char * name,
int increment )

Creates a new buffer with the specified name and slot increment.

If a buffer with the given name already exists, the function will terminate the program with an error message.

Parameters
nameThe name of the buffer to create.
incrementThe number of slots to allocate when expanding the buffer. If zero, defaults to 1.

Definition at line 53 of file buffer.c.

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

◆ find_buffer()

struct buffer_struct * find_buffer ( char * name)

Definition at line 165 of file buffer.c.

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

◆ fprintf_buffer()

void fprintf_buffer ( FILE * fp,
char * format,
char * name )

Writes the contents of the specified buffer to a file using a specified format.

Each line in the buffer is printed to the file according to the provided format string.

Parameters
fpThe file pointer to write the buffer contents to.
formatThe format string to use with fprintf for each line.
nameThe name of the buffer to write.

Definition at line 143 of file buffer.c.

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

◆ fputs_buffer()

void fputs_buffer ( char * name,
FILE * fp )

Definition at line 154 of file buffer.c.

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