BRL-CAD
vlb.c
Go to the documentation of this file.
1 /* V L B . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2004-2014 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 #include "common.h"
22 
23 #include <string.h>
24 
25 #include "bu/log.h"
26 #include "bu/malloc.h"
27 #include "bu/vlb.h"
28 #include "bu/vls.h"
29 
30 
31 #define VLB_BLOCK_SIZE 512
32 
33 
34 void
35 bu_vlb_init(struct bu_vlb *vlb)
36 {
37  vlb->buf = (uint8_t *)bu_calloc(1, VLB_BLOCK_SIZE, "bu_vlb");
39  vlb->nextByte = 0;
40  vlb->magic = BU_VLB_MAGIC;
41 }
42 
43 
44 void
45 bu_vlb_initialize(struct bu_vlb *vlb, size_t initialSize)
46 {
47  if (UNLIKELY(initialSize <= 0)) {
48  bu_log("bu_vlb_initialize: WARNING - illegal initial size (%zu), ignored\n", initialSize);
49  bu_vlb_init(vlb);
50  return;
51  }
52  vlb->buf = (uint8_t *)bu_calloc(1, initialSize, "bu_vlb");
53  vlb->bufCapacity = initialSize;
54  vlb->nextByte = 0;
55  vlb->magic = BU_VLB_MAGIC;
56 }
57 
58 
59 void
60 bu_vlb_write(struct bu_vlb *vlb, unsigned char *start, size_t len)
61 {
62  size_t addBlocks = 0;
63  size_t currCapacity;
64 
65  BU_CKMAG(vlb, BU_VLB_MAGIC, "magic for bu_vlb");
66  currCapacity = vlb->bufCapacity;
67  while (currCapacity <= (vlb->nextByte + len)) {
68  addBlocks++;
69  currCapacity += VLB_BLOCK_SIZE;
70  }
71 
72  if (addBlocks) {
73  vlb->buf = (uint8_t *)bu_realloc(vlb->buf, currCapacity, "enlarging vlb");
74  vlb->bufCapacity = currCapacity;
75  }
76 
77  memcpy(&vlb->buf[vlb->nextByte], start, len);
78  vlb->nextByte += len;
79 }
80 
81 
82 void
83 bu_vlb_reset(struct bu_vlb *vlb)
84 {
85  BU_CKMAG(vlb, BU_VLB_MAGIC, "magic for bu_vlb");
86  vlb->nextByte = 0;
87 }
88 
89 
90 unsigned char *
91 bu_vlb_addr(struct bu_vlb *vlb)
92 {
93  BU_CKMAG(vlb, BU_VLB_MAGIC, "magic for bu_vlb");
94  return vlb->buf;
95 }
96 
97 
98 size_t
99 bu_vlb_buflen(struct bu_vlb *vlb)
100 {
101  BU_CKMAG(vlb, BU_VLB_MAGIC, "magic for bu_vlb");
102  return vlb->nextByte;
103 }
104 
105 
106 void
107 bu_vlb_free(struct bu_vlb *vlb)
108 {
109  BU_CKMAG(vlb, BU_VLB_MAGIC, "magic for bu_vlb");
110  if (vlb->buf != NULL) {
111  bu_free(vlb->buf, "vlb");
112  }
113  vlb->buf = NULL;
114  vlb->bufCapacity = 0;
115  vlb->nextByte = (size_t)-1;
116  vlb->magic = 0;
117 }
118 
119 
120 void
121 bu_vlb_print(struct bu_vlb *vlb, FILE *fd)
122 {
123  size_t ret;
124 
125  BU_CKMAG(vlb, BU_VLB_MAGIC, "magic for bu_vlb");
126 
127  ret = fwrite(vlb->buf, 1, vlb->nextByte, fd);
128  if (ret != vlb->nextByte)
129  perror("fwrite failed");
130 }
131 
132 
133 void
134 bu_pr_vlb(const char *title, const struct bu_vlb *vlb)
135 {
136  size_t i;
137  unsigned char *c;
138  struct bu_vls v = BU_VLS_INIT_ZERO;
139 
140  BU_CKMAG(vlb, BU_VLB_MAGIC, "magic for bu_vlb");
141 
142  /* optional title */
143  if (title) {
144  bu_vls_strcat(&v, title);
145  bu_vls_strcat(&v, ": ");
146  }
147 
148  /* print each byte to string buffer */
149  c = vlb->buf;
150  for (i=0; i<vlb->nextByte; i++) {
151  bu_vls_printf(&v, "%02x ", *c);
152  c++;
153  }
154 
155  /* print as one call */
156  bu_log("%s", bu_vls_addr(&v));
157 
158  bu_vls_free(&v);
159 }
160 
161 
162 /*
163  * Local Variables:
164  * mode: C
165  * tab-width: 8
166  * indent-tabs-mode: t
167  * c-file-style: "stroustrup"
168  * End:
169  * ex: shiftwidth=4 tabstop=8
170  */
uint32_t magic
Definition: vlb.h:45
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Definition: vlb.h:44
void bu_vlb_init(struct bu_vlb *vlb)
Definition: vlb.c:35
#define BU_CKMAG(_ptr, _magic, _str)
Definition: magic.h:233
void bu_vls_strcat(struct bu_vls *vp, const char *s)
Definition: vls.c:368
Header file for the BRL-CAD common definitions.
size_t nextByte
Definition: vlb.h:48
#define VLB_BLOCK_SIZE
Definition: vlb.c:31
void bu_vlb_write(struct bu_vlb *vlb, unsigned char *start, size_t len)
Definition: vlb.c:60
void bu_pr_vlb(const char *title, const struct bu_vlb *vlb)
Definition: vlb.c:134
void bu_vlb_print(struct bu_vlb *vlb, FILE *fd)
Definition: vlb.c:121
void bu_vls_free(struct bu_vls *vp)
Definition: vls.c:248
#define BU_VLB_MAGIC
Definition: magic.h:64
void * bu_calloc(size_t nelem, size_t elsize, const char *str)
Definition: malloc.c:321
void * bu_realloc(void *ptr, size_t siz, const char *str)
char * bu_vls_addr(const struct bu_vls *vp)
Definition: vls.c:111
unsigned char * buf
Definition: vlb.h:46
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
void bu_vlb_initialize(struct bu_vlb *vlb, size_t initialSize)
Definition: vlb.c:45
void bu_vlb_reset(struct bu_vlb *vlb)
Definition: vlb.c:83
unsigned char * bu_vlb_addr(struct bu_vlb *vlb)
Definition: vlb.c:91
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
#define BU_VLS_INIT_ZERO
Definition: vls.h:84
void bu_vlb_free(struct bu_vlb *vlb)
Definition: vlb.c:107
Definition: vls.h:56
size_t bu_vlb_buflen(struct bu_vlb *vlb)
Definition: vlb.c:99
size_t bufCapacity
Definition: vlb.h:47
#define UNLIKELY(expression)
Definition: common.h:282