996
|
1 /*
|
|
2 * Copyright (C) 2009 Gregor Richards
|
|
3 *
|
|
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 * of this software and associated documentation files (the "Software"), to deal
|
|
6 * in the Software without restriction, including without limitation the rights
|
|
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 * copies of the Software, and to permit persons to whom the Software is
|
|
9 * furnished to do so, subject to the following conditions:
|
|
10 *
|
|
11 * The above copyright notice and this permission notice shall be included in
|
|
12 * all copies or substantial portions of the Software.
|
|
13 *
|
|
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 * THE SOFTWARE.
|
|
21 */
|
|
22
|
|
23 #ifndef BUFFER_H
|
|
24 #define BUFFER_H
|
|
25
|
|
26 #include "helpers.h"
|
|
27
|
|
28 #define BUFFER_DEFAULT_SIZE 1024
|
|
29
|
|
30 /* auto-expanding buffer */
|
|
31 #define BUFFER(name, type) \
|
|
32 struct Buffer_ ## name { \
|
|
33 size_t bufsz, bufused; \
|
|
34 type *buf; \
|
|
35 }
|
|
36
|
|
37 BUFFER(char, char);
|
|
38 BUFFER(int, int);
|
|
39
|
|
40 /* initialize a buffer */
|
|
41 #define INIT_BUFFER(buffer) \
|
|
42 { \
|
|
43 (buffer).bufsz = BUFFER_DEFAULT_SIZE; \
|
|
44 (buffer).bufused = 0; \
|
|
45 SF((buffer).buf, malloc, NULL, (BUFFER_DEFAULT_SIZE * sizeof(*(buffer).buf))); \
|
|
46 }
|
|
47
|
|
48 /* free a buffer */
|
|
49 #define FREE_BUFFER(buffer) \
|
|
50 { \
|
|
51 if ((buffer).buf) free((buffer).buf); \
|
|
52 (buffer).buf = NULL; \
|
|
53 }
|
|
54
|
|
55 /* the address of the free space in the buffer */
|
|
56 #define BUFFER_END(buffer) ((buffer).buf + (buffer).bufused)
|
|
57
|
|
58 /* mark new bytes in a buffer */
|
|
59 #define STEP_BUFFER(buffer, by) ((buffer).bufused += by)
|
|
60
|
|
61 /* the amount of space left in the buffer */
|
|
62 #define BUFFER_SPACE(buffer) ((buffer).bufsz - (buffer).bufused)
|
|
63
|
|
64 /* expand a buffer */
|
|
65 #define EXPAND_BUFFER(buffer) \
|
|
66 { \
|
|
67 (buffer).bufsz *= 2; \
|
|
68 SF((buffer).buf, realloc, NULL, ((buffer).buf, (buffer).bufsz * sizeof(*(buffer).buf))); \
|
|
69 }
|
|
70
|
|
71 /* write a string to a buffer */
|
|
72 #define WRITE_BUFFER(buffer, string, len) \
|
|
73 { \
|
|
74 size_t _len = (len); \
|
|
75 while (BUFFER_SPACE(buffer) < _len) { \
|
|
76 EXPAND_BUFFER(buffer); \
|
|
77 } \
|
|
78 memcpy(BUFFER_END(buffer), string, _len * sizeof(*(buffer).buf)); \
|
|
79 STEP_BUFFER(buffer, _len); \
|
|
80 }
|
|
81
|
|
82 /* read a file into a buffer */
|
|
83 #define READ_FILE_BUFFER(buffer, fh) \
|
|
84 { \
|
|
85 size_t _rd; \
|
|
86 while ((_rd = fread(BUFFER_END(buffer), 1, BUFFER_SPACE(buffer), (fh))) > 0) { \
|
|
87 STEP_BUFFER(buffer, _rd); \
|
|
88 if (BUFFER_SPACE(buffer) <= 0) { \
|
|
89 EXPAND_BUFFER(buffer); \
|
|
90 } \
|
|
91 } \
|
|
92 }
|
|
93
|
|
94 #endif
|