1 |
/* |
2 |
stream.c - `Stream' object |
3 |
Copyright (C) 2008 siliconforks.com |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#define _GNU_SOURCE |
21 |
|
22 |
#include "stream.h" |
23 |
|
24 |
#include <stdarg.h> |
25 |
#include <stdint.h> |
26 |
#include <string.h> |
27 |
|
28 |
#include "util.h" |
29 |
|
30 |
Stream * Stream_new(size_t capacity) { |
31 |
Stream * result = xmalloc(sizeof(Stream)); |
32 |
result->length = 0; |
33 |
if (capacity == 0) { |
34 |
capacity = 8192; |
35 |
} |
36 |
result->capacity = capacity; |
37 |
result->data = xmalloc(capacity); |
38 |
return result; |
39 |
} |
40 |
|
41 |
void Stream_write(Stream * stream, const void * p, size_t size) { |
42 |
if (SIZE_MAX - size < stream->length) { |
43 |
fatal("out of memory"); |
44 |
} |
45 |
|
46 |
size_t new_length = stream->length + size; |
47 |
size_t new_capacity = stream->capacity; |
48 |
if (new_capacity < new_length) { |
49 |
if (SIZE_MAX / 2 < new_capacity) { |
50 |
new_capacity = SIZE_MAX; |
51 |
} |
52 |
else { |
53 |
new_capacity *= 2; |
54 |
} |
55 |
|
56 |
if (new_capacity < new_length) { |
57 |
new_capacity = new_length; |
58 |
} |
59 |
} |
60 |
|
61 |
if (new_capacity != stream->capacity) { |
62 |
stream->data = xrealloc(stream->data, stream->capacity); |
63 |
stream->capacity = new_capacity; |
64 |
} |
65 |
|
66 |
memcpy(stream->data + stream->length, p, size); |
67 |
stream->length = new_length; |
68 |
} |
69 |
|
70 |
void Stream_write_string(Stream * stream, const char * s) { |
71 |
Stream_write(stream, s, strlen(s)); |
72 |
} |
73 |
|
74 |
void Stream_write_char(Stream * stream, char c) { |
75 |
Stream_write(stream, &c, 1); |
76 |
} |
77 |
|
78 |
void Stream_printf(Stream * stream, const char * format, ...) { |
79 |
va_list a; |
80 |
va_start(a, format); |
81 |
|
82 |
char * s = NULL; |
83 |
/* note that size does not include the NUL character */ |
84 |
int size = vasprintf(&s, format, a); |
85 |
if (size < 0) { |
86 |
fatal("out of memory"); |
87 |
} |
88 |
Stream_write(stream, s, size); |
89 |
free(s); |
90 |
|
91 |
va_end(a); |
92 |
} |
93 |
|
94 |
void Stream_write_file_contents(Stream * stream, FILE * f) { |
95 |
char buffer[8192]; |
96 |
size_t bytes_read; |
97 |
while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) { |
98 |
Stream_write(stream, buffer, bytes_read); |
99 |
} |
100 |
} |
101 |
|
102 |
void Stream_delete(Stream * stream) { |
103 |
free(stream->data); |
104 |
free(stream); |
105 |
} |