1
/* buffer.h - automatic buffer structure */
4
* Copyright (c) 2008, Natacha Porté
6
* Permission to use, copy, modify, and distribute this software for any
7
* purpose with or without fee is hereby granted, provided that the above
8
* copyright notice and this permission notice appear in all copies.
10
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
#ifndef LITHIUM_BUFFER_H
20
#define LITHIUM_BUFFER_H
29
/* struct buf • character array buffer */
31
char * data; /* actual character data */
32
size_t size; /* size of the string */
33
size_t asize; /* allocated size (0 = volatile buffer) */
34
size_t unit; /* reallocation unit size (0 = read-only buffer) */
35
int ref; /* reference count */
44
/* CONST_BUF • global buffer from a string litteral */
45
#define CONST_BUF(name, string) static struct buf name = { string, sizeof string -1, sizeof string }
48
/* VOLATILE_BUF • macro for creating a volatile buffer on the stack */
49
#define VOLATILE_BUF(name, strname) struct buf name = { strname, strlen(strname) }
52
/* BUFPUTSL • optimized bufputs of a string litteral */
53
#define BUFPUTSL(output, litteral) bufput(output, litteral, sizeof litteral - 1)
56
/***********************
57
* FUNCTION ATTRIBUTES *
58
***********************/
60
/* BUF_ALLOCATOR • the function returns a completely new ponter */
62
#define BUF_ALLOCATOR __attribute__ ((malloc))
68
/* BUF_PRINTF_LIKE • marks the function as behaving like printf */
70
#define BUF_PRINTF_LIKE(format_index, first_variadic_index) __attribute__ ((format (printf, format_index, first_variadic_index)));
72
#define BUF_PRINTF_LIKE(format_index, first_variadic_index)
80
/* bufcasecmp • case-insensitive buffer comparison */
81
int bufcasecmp(const struct buf *, const struct buf *);
83
/* bufcmp • case-sensitive buffer comparison */
84
int bufcmp(const struct buf *, const struct buf *);
86
/* bufcmps • case-sensitive comparison of a string to a buffer */
87
int bufcmps(const struct buf *, const char *);
89
/* bufdup • buffer duplication */
90
struct buf * bufdup(const struct buf *, size_t) BUF_ALLOCATOR;
92
/* bufgrow • increasing the allocated size to the given value */
93
int bufgrow(struct buf *, size_t);
95
/* bufnew • allocation of a new buffer */
96
struct buf * bufnew(size_t) BUF_ALLOCATOR;
98
/* bufnullterm • NUL-termination of the string array (making a C-string) */
99
void bufnullterm(struct buf *);
101
/* bufprintf • formatted printing to a buffer */
102
void bufprintf(struct buf *, const char *, ...) BUF_PRINTF_LIKE(2, 3);
104
/* bufput • appends raw data to a buffer */
105
void bufput(struct buf *, const void*, size_t);
107
/* bufputs • appends a NUL-terminated string to a buffer */
108
void bufputs(struct buf *, const char*);
110
/* bufputc • appends a single char to a buffer */
111
void bufputc(struct buf *, char);
113
/* bufrelease • decrease the reference count and free the buffer if needed */
114
void bufrelease(struct buf *);
116
/* bufreset • frees internal data of the buffer */
117
void bufreset(struct buf *);
119
/* bufset • safely assigns a buffer to another */
120
void bufset(struct buf **, struct buf *);
122
/* bufslurp • removes a given number of bytes from the head of the array */
123
void bufslurp(struct buf *, size_t);
125
/* buftoi • converts the numbers at the beginning of the buf into an int */
126
int buftoi(struct buf *, size_t, size_t *);
131
/* vbufprintf • stdarg variant of formatted printing into a buffer */
133
vbufprintf(struct buf *, const char*, va_list);
135
#endif /* def BUFFER_STDARG */
137
#endif /* ndef LITHIUM_BUFFER_H */