1 |
siliconforks |
92 |
/* |
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 |
siliconforks |
102 |
#include <config.h> |
23 |
|
|
|
24 |
siliconforks |
92 |
#include "stream.h" |
25 |
|
|
|
26 |
|
|
#include <stdarg.h> |
27 |
|
|
#include <stdint.h> |
28 |
|
|
#include <string.h> |
29 |
|
|
|
30 |
|
|
#include "util.h" |
31 |
|
|
|
32 |
|
|
Stream * Stream_new(size_t capacity) { |
33 |
|
|
Stream * result = xmalloc(sizeof(Stream)); |
34 |
|
|
result->length = 0; |
35 |
|
|
if (capacity == 0) { |
36 |
|
|
capacity = 8192; |
37 |
|
|
} |
38 |
|
|
result->capacity = capacity; |
39 |
|
|
result->data = xmalloc(capacity); |
40 |
|
|
return result; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
void Stream_write(Stream * stream, const void * p, size_t size) { |
44 |
|
|
if (SIZE_MAX - size < stream->length) { |
45 |
|
|
fatal("out of memory"); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
size_t new_length = stream->length + size; |
49 |
|
|
size_t new_capacity = stream->capacity; |
50 |
|
|
if (new_capacity < new_length) { |
51 |
|
|
if (SIZE_MAX / 2 < new_capacity) { |
52 |
|
|
new_capacity = SIZE_MAX; |
53 |
|
|
} |
54 |
|
|
else { |
55 |
|
|
new_capacity *= 2; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
if (new_capacity < new_length) { |
59 |
|
|
new_capacity = new_length; |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
if (new_capacity != stream->capacity) { |
64 |
siliconforks |
100 |
stream->data = xrealloc(stream->data, new_capacity); |
65 |
siliconforks |
92 |
stream->capacity = new_capacity; |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
memcpy(stream->data + stream->length, p, size); |
69 |
|
|
stream->length = new_length; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
void Stream_write_string(Stream * stream, const char * s) { |
73 |
|
|
Stream_write(stream, s, strlen(s)); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
void Stream_write_char(Stream * stream, char c) { |
77 |
|
|
Stream_write(stream, &c, 1); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
void Stream_printf(Stream * stream, const char * format, ...) { |
81 |
|
|
va_list a; |
82 |
|
|
va_start(a, format); |
83 |
|
|
|
84 |
|
|
char * s = NULL; |
85 |
|
|
/* note that size does not include the NUL character */ |
86 |
|
|
int size = vasprintf(&s, format, a); |
87 |
|
|
if (size < 0) { |
88 |
|
|
fatal("out of memory"); |
89 |
|
|
} |
90 |
|
|
Stream_write(stream, s, size); |
91 |
|
|
free(s); |
92 |
|
|
|
93 |
|
|
va_end(a); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
void Stream_write_file_contents(Stream * stream, FILE * f) { |
97 |
|
|
char buffer[8192]; |
98 |
|
|
size_t bytes_read; |
99 |
|
|
while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) { |
100 |
|
|
Stream_write(stream, buffer, bytes_read); |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
void Stream_delete(Stream * stream) { |
105 |
|
|
free(stream->data); |
106 |
|
|
free(stream); |
107 |
|
|
} |
108 |
siliconforks |
102 |
|
109 |
|
|
void Stream_reset(Stream * stream) { |
110 |
|
|
stream->length = 0; |
111 |
|
|
} |