Volksdata 1.0b7
RDF library
Loading...
Searching...
No Matches
buffer.c
Go to the documentation of this file.
1#include "volksdata/buffer.h"
2
3/* * * Inline extern prototypes * * */
4
6
7VOLK_Buffer *VOLK_buffer_new (const unsigned char *data, const size_t size);
9 VOLK_Buffer *buf, const size_t size, const unsigned char *data);
10bool VOLK_buffer_eq (const VOLK_Buffer *buf1, const VOLK_Buffer *buf2);
11int VOLK_buffer_cmp (const VOLK_Buffer *buf1, const VOLK_Buffer *buf2);
12static inline char unescape_char (const char c);
13
14
15/* * * API * * */
16
19 VOLK_Buffer *buf, const size_t size, const unsigned char *data)
20{
21 // Do not rely on glibc realloc to handle zero size. See man 3 realloc -
22 // Nonportable behavior.
23 if (size == 0) {
24 if (buf->addr) {
25 free (buf->addr);
26 buf->addr = NULL;
27 }
28 } else {
29 // If buf->addr is NULL, realloc == malloc. This is portable.
30 unsigned char *tmp = realloc (buf->addr, size);
31 if (UNLIKELY (size > 0 && tmp == NULL)) return VOLK_MEM_ERR;
32
33 buf->addr = tmp;
34 }
35 buf->size = size;
36
37 if (data && buf->addr) memcpy (buf->addr, data, buf->size);
38
39 return VOLK_OK;
40}
41
42
44{
45 for (size_t i = 0; i < buf->size; i++) {
46 char chr = ((char*)buf->addr)[i];
47 if (isprint (chr)) {
48 fputc (chr, stdout);
49 } else {
50 printf ("\\x%02x", chr);
51 }
52 }
53 printf ("\n");
54}
55
56
57char *
59{
60 size_t i, str_size = 1; // terminating NUL
61
62 // Calculate alloc size first.
63 for (i = 0; i < buf->size; i++) {
64 char chr = ((char*)buf->addr)[i];
65 if (isprint (chr)) str_size ++;
66 else str_size += 4; // 4 characters for ASCII representation (\xNN).
67 }
68
69 LOG_TRACE("Byte buffer str size: %lu", str_size);
70
71 char *cstr = malloc (str_size);
72
73 size_t cur = 0; // Position in target string.
74 for (i = 0; i < str_size - 1; i++) {
75 char chr = ((char*)buf->addr)[i];
76
77 if (isprint (chr)) {
78 cstr[cur] = chr;
79 cur ++;
80 } else {
81 sprintf (cstr + cur, "\\x%02x", chr);
82 cur += 4;
83 }
84 }
85
86 cstr[str_size - 1] = 0; // terminating NUL.
87
88 return cstr;
89}
90
91
93{
94 if (LIKELY (buf) && !(buf->flags & VOLK_BUF_BORROWED)) free (buf->addr);
95}
96
98{
99 VOLK_buffer_done (buf);
100 free (buf);
101}
102
103
104/*
105 * Buffer triples.
106 */
107
110{
111 VOLK_BufferTriple *sspo = malloc (sizeof (*sspo));
112 if (!sspo) return NULL;
113
114 if (UNLIKELY (VOLK_btriple_init (sspo, s, p, o))) {
115 free (sspo);
116 return NULL;
117 }
118
119 return sspo;
120}
121
122
126{
127 sspo->s = s;
128 sspo->p = p;
129 sspo->o = o;
130
131 return VOLK_OK;
132}
133
134
135void
137{
138 if (UNLIKELY (!sspo)) return;
139
140 VOLK_buffer_done (sspo->s);
141 VOLK_buffer_done (sspo->p);
142 VOLK_buffer_done (sspo->o);
143}
144
145
146void
148{
149 if (UNLIKELY (!sspo)) return;
150
151 VOLK_buffer_free (sspo->s);
152 VOLK_buffer_free (sspo->p);
153 VOLK_buffer_free (sspo->o);
154
155 free (sspo);
156}
157
158
159/*
160 * Statics.
161 */
162
163static inline char unescape_char (const char c) {
164 switch (c) {
165 case 't': return '\t';
166 case 'b': return '\b';
167 case 'n': return '\n';
168 case 'r': return '\r';
169 case 'f': return '\f';
170 default: return c;
171 }
172}
173
174
175/* Extern inline prototypes. */
176
#define UNLIKELY(x)
Definition core.h:39
#define LIKELY(x)
Definition core.h:38
bool VOLK_buffer_eq(const VOLK_Buffer *buf1, const VOLK_Buffer *buf2)
Return whether two buffers are equal.
Definition buffer.h:219
void VOLK_btriple_done(VOLK_BufferTriple *sspo)
Free the internal pointers of a buffer triple.
Definition buffer.c:136
VOLK_Key VOLK_buffer_hash(const VOLK_Buffer *buf)
Hash a buffer.
Definition buffer.h:175
void VOLK_buffer_done(VOLK_Buffer *buf)
Free the content of a buffer.
Definition buffer.c:92
VOLK_Buffer * VOLK_btriple_pos(const VOLK_BufferTriple *trp, VOLK_TriplePos n)
Get serialized triple by term position.
Definition buffer.h:297
char * VOLK_buffer_as_str(const VOLK_Buffer *buf)
Format a buffer into anb ASCII string.
Definition buffer.c:58
VOLK_rc VOLK_buffer_init(VOLK_Buffer *buf, const size_t size, const unsigned char *data)
Initialize or reuse a buffer handle.
Definition buffer.c:18
VOLK_Key VOLK_btriple_hash(const VOLK_BufferTriple *strp)
Hash a buffer triple.
Definition buffer.h:317
VOLK_Buffer * VOLK_default_ctx_buf
Serialized default context.
Definition buffer.c:5
void VOLK_btriple_free(VOLK_BufferTriple *sspo)
Free a buffer triple and all its internal pointers.
Definition buffer.c:147
void VOLK_buffer_print(const VOLK_Buffer *buf)
Print a byte string of a given length in a human-readable format.
Definition buffer.c:43
VOLK_TriplePos
Triple position of s, p, o.
Definition buffer.h:19
VOLK_rc VOLK_btriple_init(VOLK_BufferTriple *sspo, VOLK_Buffer *s, VOLK_Buffer *p, VOLK_Buffer *o)
Initialize internal term pointers in a heap-allocated buffer triple.
Definition buffer.c:124
void VOLK_buffer_free(VOLK_Buffer *buf)
Free a buffer.
Definition buffer.c:97
int VOLK_buffer_cmp(const VOLK_Buffer *buf1, const VOLK_Buffer *buf2)
Compare two buffers.
Definition buffer.h:206
VOLK_BufferTriple * VOLK_btriple_new(VOLK_Buffer *s, VOLK_Buffer *p, VOLK_Buffer *o)
Create a new buffer triple.
Definition buffer.c:109
VOLK_Buffer * VOLK_buffer_new(const unsigned char *data, const size_t size)
Create a new buffer and optionally populate it with data.
Definition buffer.h:111
@ VOLK_BUF_BORROWED
Definition buffer.h:28
#define LOG_TRACE(...)
Definition core.h:276
size_t VOLK_Key
Term key, i.e., hash of a serialized term.
Definition core.h:230
#define VOLK_MEM_ERR
Memory allocation error.
Definition core.h:144
#define VOLK_OK
Generic success return code.
Definition core.h:83
int VOLK_rc
Definition core.h:79
Triple of byte buffers.
Definition buffer.h:60
VOLK_Buffer * o
Definition buffer.h:63
VOLK_Buffer * s
Definition buffer.h:61
VOLK_Buffer * p
Definition buffer.h:62
General-purpose data buffer.
Definition buffer.h:47
VOLK_BufferFlag flags
Definition buffer.h:50
unsigned char * addr
Definition buffer.h:48
size_t size
Definition buffer.h:49