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