BRL-CAD
vlb.h
Go to the documentation of this file.
1/* V L B . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2004-2023 United States Government as represented by
5 * the U.S. Army Research Laboratory.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * version 2.1 as published by the Free Software Foundation.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; see the file named COPYING for more
18 * information.
19 */
20
21#ifndef BU_VLB_H
22#define BU_VLB_H
23
24#include "common.h"
25
26#include <stdio.h> /* For FILE */
27
28#include "bu/defines.h"
29#include "bu/magic.h"
30
31__BEGIN_DECLS
32
33/*----------------------------------------------------------------------*/
34/** @addtogroup bu_vlb
35 * @brief
36 * The variable length buffer package.
37 */
38/** @{ */
39/** @file bu/vlb.h */
40
41
42/**
43 * Variable Length Buffer: bu_vlb support
44 */
45struct bu_vlb {
46 uint32_t magic;
47 unsigned char *buf; /**< Dynamic memory for the buffer */
48 size_t bufCapacity; /**< Current capacity of the buffer */
49 size_t nextByte; /**< Number of bytes currently used in the buffer */
50};
51typedef struct bu_vlb bu_vlb_t;
52#define BU_VLB_NULL ((struct bu_vlb *)0)
53
54/**
55 * assert the integrity of a bu_vlb struct.
56 */
57#define BU_CK_VLB(_vp) BU_CKMAG(_vp, BU_VLB_MAGIC, "bu_vlb")
58
59/**
60 * initializes a bu_vlb struct without allocating any memory.
61 */
62#define BU_VLB_INIT(_vp) { \
63 (_vp)->magic = BU_VLB_MAGIC; \
64 (_vp)->buf = NULL; \
65 (_vp)->bufCapacity = (_vp)->nextByte = 0; \
66 }
67
68/**
69 * macro suitable for declaration statement initialization of a bu_vlb
70 * struct. does not allocate memory.
71 */
72#define BU_VLB_INIT_ZERO { BU_VLB_MAGIC, NULL, 0, 0 }
73
74/**
75 * returns truthfully whether a bu_vlb struct has been initialized.
76 * is not reliable unless the struct has been allocated with
77 * BU_ALLOC(), bu_calloc(), or a previous call to bu_vlb_init() or
78 * BU_VLB_INIT() has been made.
79 */
80#define BU_VLB_IS_INITIALIZED(_vp) (((struct bu_vlb *)(_vp) != BU_VLB_NULL) && ((_vp)->magic == BU_VLB_MAGIC))
81
82/**
83 * Initialize the specified bu_vlb structure and mallocs the initial
84 * block of memory.
85 *
86 * @param vlb Pointer to an uninitialized bu_vlb structure
87 */
88BU_EXPORT extern void bu_vlb_init(struct bu_vlb *vlb);
89
90/**
91 * Initialize the specified bu_vlb structure and mallocs the initial
92 * block of memory with the specified size
93 *
94 * @param vlb Pointer to an uninitialized bu_vlb structure
95 * @param initialSize The desired initial size of the buffer
96 */
97BU_EXPORT extern void bu_vlb_initialize(struct bu_vlb *vlb,
98 size_t initialSize);
99
100/**
101 * Write some bytes to the end of the bu_vlb structure. If necessary,
102 * additional memory will be allocated.
103 *
104 * @param vlb Pointer to the bu_vlb structure to receive the bytes
105 * @param start Pointer to the first byte to be copied to the bu_vlb structure
106 * @param len The number of bytes to copy to the bu_vlb structure
107 */
108BU_EXPORT extern void bu_vlb_write(struct bu_vlb *vlb,
109 unsigned char *start,
110 size_t len);
111
112/**
113 * Reset the bu_vlb counter to the start of its byte array. This
114 * essentially ignores any bytes currently in the buffer, but does not
115 * free any memory.
116 *
117 * @param vlb Pointer to the bu_vlb structure to be reset
118 */
119BU_EXPORT extern void bu_vlb_reset(struct bu_vlb *vlb);
120
121/**
122 * Get a pointer to the byte array held by the bu_vlb structure
123 *
124 * @param vlb Pointer to the bu_vlb structure
125 * @return A pointer to the byte array contained by the bu_vlb structure
126 */
127BU_EXPORT extern unsigned char *bu_vlb_addr(struct bu_vlb *vlb);
128
129/**
130 * Return the number of bytes used in the bu_vlb structure
131 *
132 * @param vlb Pointer to the bu_vlb structure
133 * @return The number of bytes written to the bu_vlb structure
134 */
135BU_EXPORT extern size_t bu_vlb_buflen(struct bu_vlb *vlb);
136
137/**
138 * Free the memory allocated for the byte array in the bu_vlb
139 * structure. Also uninitializes the structure.
140 *
141 * @param vlb Pointer to the bu_vlb structure
142 */
143BU_EXPORT extern void bu_vlb_free(struct bu_vlb *vlb);
144/**
145 * Write the current byte array from the bu_vlb structure to a file
146 *
147 * @param vlb Pointer to the bu_vlb structure that is the source of the bytes
148 * @param fd Pointer to a FILE to receive the bytes
149 */
150BU_EXPORT extern void bu_vlb_print(struct bu_vlb *vlb,
151 FILE *fd);
152
153/**
154 * Print the bytes set in a variable-length byte array.
155 */
156BU_EXPORT extern void bu_pr_vlb(const char *title, const struct bu_vlb *vlb);
157
158/** @} */
159
160__END_DECLS
161
162#endif /* BU_VLB_H */
163
164/*
165 * Local Variables:
166 * mode: C
167 * tab-width: 8
168 * indent-tabs-mode: t
169 * c-file-style: "stroustrup"
170 * End:
171 * ex: shiftwidth=4 tabstop=8
172 */
Header file for the BRL-CAD common definitions.
unsigned char * bu_vlb_addr(struct bu_vlb *vlb)
void bu_pr_vlb(const char *title, const struct bu_vlb *vlb)
void bu_vlb_initialize(struct bu_vlb *vlb, size_t initialSize)
void bu_vlb_print(struct bu_vlb *vlb, FILE *fd)
void bu_vlb_reset(struct bu_vlb *vlb)
void bu_vlb_free(struct bu_vlb *vlb)
size_t bu_vlb_buflen(struct bu_vlb *vlb)
void bu_vlb_init(struct bu_vlb *vlb)
void bu_vlb_write(struct bu_vlb *vlb, unsigned char *start, size_t len)
Global registry of recognized magic numbers.
Definition: vlb.h:45
uint32_t magic
Definition: vlb.h:46
size_t bufCapacity
Definition: vlb.h:48
unsigned char * buf
Definition: vlb.h:47
size_t nextByte
Definition: vlb.h:49