comparison hoedown/src/buffer.h @ 2:2dce41ab17e8

hoedown: import 3.0.7
author David Demelier <markand@malikania.fr>
date Wed, 24 Feb 2016 20:54:52 +0100
parents
children
comparison
equal deleted inserted replaced
1:4d89bd8a3f7f 2:2dce41ab17e8
1 /* buffer.h - simple, fast buffers */
2
3 #ifndef HOEDOWN_BUFFER_H
4 #define HOEDOWN_BUFFER_H
5
6 #include <stdio.h>
7 #include <stddef.h>
8 #include <stdarg.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 #if defined(_MSC_VER)
17 #define __attribute__(x)
18 #define inline __inline
19 #define __builtin_expect(x,n) x
20 #endif
21
22
23 /*********
24 * TYPES *
25 *********/
26
27 typedef void *(*hoedown_realloc_callback)(void *, size_t);
28 typedef void (*hoedown_free_callback)(void *);
29
30 struct hoedown_buffer {
31 uint8_t *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
36 hoedown_realloc_callback data_realloc;
37 hoedown_free_callback data_free;
38 hoedown_free_callback buffer_free;
39 };
40
41 typedef struct hoedown_buffer hoedown_buffer;
42
43
44 /*************
45 * FUNCTIONS *
46 *************/
47
48 /* allocation wrappers */
49 void *hoedown_malloc(size_t size) __attribute__ ((malloc));
50 void *hoedown_calloc(size_t nmemb, size_t size) __attribute__ ((malloc));
51 void *hoedown_realloc(void *ptr, size_t size) __attribute__ ((malloc));
52
53 /* hoedown_buffer_init: initialize a buffer with custom allocators */
54 void hoedown_buffer_init(
55 hoedown_buffer *buffer,
56 size_t unit,
57 hoedown_realloc_callback data_realloc,
58 hoedown_free_callback data_free,
59 hoedown_free_callback buffer_free
60 );
61
62 /* hoedown_buffer_uninit: uninitialize an existing buffer */
63 void hoedown_buffer_uninit(hoedown_buffer *buf);
64
65 /* hoedown_buffer_new: allocate a new buffer */
66 hoedown_buffer *hoedown_buffer_new(size_t unit) __attribute__ ((malloc));
67
68 /* hoedown_buffer_reset: free internal data of the buffer */
69 void hoedown_buffer_reset(hoedown_buffer *buf);
70
71 /* hoedown_buffer_grow: increase the allocated size to the given value */
72 void hoedown_buffer_grow(hoedown_buffer *buf, size_t neosz);
73
74 /* hoedown_buffer_put: append raw data to a buffer */
75 void hoedown_buffer_put(hoedown_buffer *buf, const uint8_t *data, size_t size);
76
77 /* hoedown_buffer_puts: append a NUL-terminated string to a buffer */
78 void hoedown_buffer_puts(hoedown_buffer *buf, const char *str);
79
80 /* hoedown_buffer_putc: append a single char to a buffer */
81 void hoedown_buffer_putc(hoedown_buffer *buf, uint8_t c);
82
83 /* hoedown_buffer_putf: read from a file and append to a buffer, until EOF or error */
84 int hoedown_buffer_putf(hoedown_buffer *buf, FILE* file);
85
86 /* hoedown_buffer_set: replace the buffer's contents with raw data */
87 void hoedown_buffer_set(hoedown_buffer *buf, const uint8_t *data, size_t size);
88
89 /* hoedown_buffer_sets: replace the buffer's contents with a NUL-terminated string */
90 void hoedown_buffer_sets(hoedown_buffer *buf, const char *str);
91
92 /* hoedown_buffer_eq: compare a buffer's data with other data for equality */
93 int hoedown_buffer_eq(const hoedown_buffer *buf, const uint8_t *data, size_t size);
94
95 /* hoedown_buffer_eq: compare a buffer's data with NUL-terminated string for equality */
96 int hoedown_buffer_eqs(const hoedown_buffer *buf, const char *str);
97
98 /* hoedown_buffer_prefix: compare the beginning of a buffer with a string */
99 int hoedown_buffer_prefix(const hoedown_buffer *buf, const char *prefix);
100
101 /* hoedown_buffer_slurp: remove a given number of bytes from the head of the buffer */
102 void hoedown_buffer_slurp(hoedown_buffer *buf, size_t size);
103
104 /* hoedown_buffer_cstr: NUL-termination of the string array (making a C-string) */
105 const char *hoedown_buffer_cstr(hoedown_buffer *buf);
106
107 /* hoedown_buffer_printf: formatted printing to a buffer */
108 void hoedown_buffer_printf(hoedown_buffer *buf, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
109
110 /* hoedown_buffer_put_utf8: put a Unicode character encoded as UTF-8 */
111 void hoedown_buffer_put_utf8(hoedown_buffer *buf, unsigned int codepoint);
112
113 /* hoedown_buffer_free: free the buffer */
114 void hoedown_buffer_free(hoedown_buffer *buf);
115
116
117 /* HOEDOWN_BUFPUTSL: optimized hoedown_buffer_puts of a string literal */
118 #define HOEDOWN_BUFPUTSL(output, literal) \
119 hoedown_buffer_put(output, (const uint8_t *)literal, sizeof(literal) - 1)
120
121 /* HOEDOWN_BUFSETSL: optimized hoedown_buffer_sets of a string literal */
122 #define HOEDOWN_BUFSETSL(output, literal) \
123 hoedown_buffer_set(output, (const uint8_t *)literal, sizeof(literal) - 1)
124
125 /* HOEDOWN_BUFEQSL: optimized hoedown_buffer_eqs of a string literal */
126 #define HOEDOWN_BUFEQSL(output, literal) \
127 hoedown_buffer_eq(output, (const uint8_t *)literal, sizeof(literal) - 1)
128
129
130 #ifdef __cplusplus
131 }
132 #endif
133
134 #endif /** HOEDOWN_BUFFER_H **/