/[jscoverage]/trunk/stream.c
ViewVC logotype

Contents of /trunk/stream.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 505 - (show annotations)
Sat Jan 2 04:55:54 2010 UTC (8 years, 11 months ago) by siliconforks
File MIME type: text/plain
File size: 3299 byte(s)
Update copyright year.

1 /*
2 stream.c - `Stream' object
3 Copyright (C) 2008, 2009, 2010 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 <config.h>
23
24 #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 size_t stream_length = stream->length;
45 size_t stream_capacity = stream->capacity;
46 if (stream_capacity - stream_length < size) {
47 if (SIZE_MAX - size < stream_length) {
48 fatal("out of memory");
49 }
50
51 if (SIZE_MAX / 2 < stream_capacity) {
52 stream_capacity = SIZE_MAX;
53 }
54 else {
55 stream_capacity *= 2;
56 }
57
58 size_t new_length = stream_length + size;
59 if (stream_capacity < new_length) {
60 stream_capacity = new_length;
61 }
62
63 stream->data = xrealloc(stream->data, stream_capacity);
64 stream->capacity = stream_capacity;
65 memcpy(stream->data + stream_length, p, size);
66 stream->length = new_length;
67 }
68 else {
69 memcpy(stream->data + stream_length, p, size);
70 stream->length = stream_length + size;
71 }
72 }
73
74 void Stream_write_string(Stream * stream, const char * s) {
75 Stream_write(stream, s, strlen(s));
76 }
77
78 void Stream_write_char(Stream * stream, char c) {
79 size_t stream_length = stream->length;
80 if (stream_length == stream->capacity) {
81 if (stream->capacity == SIZE_MAX) {
82 fatal("out of memory");
83 }
84
85 if (SIZE_MAX / 2 < stream->capacity) {
86 stream->capacity = SIZE_MAX;
87 }
88 else {
89 stream->capacity *= 2;
90 }
91
92 stream->data = xrealloc(stream->data, stream->capacity);
93 }
94 stream->data[stream_length] = c;
95 stream->length = stream_length + 1;
96 }
97
98 void Stream_printf(Stream * stream, const char * format, ...) {
99 va_list a;
100 va_start(a, format);
101
102 char * s = NULL;
103 /* note that size does not include the NUL character */
104 int size = vasprintf(&s, format, a);
105 if (size < 0) {
106 fatal("out of memory");
107 }
108 Stream_write(stream, s, size);
109 free(s);
110
111 va_end(a);
112 }
113
114 void Stream_write_file_contents(Stream * stream, FILE * f) {
115 char buffer[8192];
116 size_t bytes_read;
117 while ((bytes_read = fread(buffer, 1, 8192, f)) > 0) {
118 Stream_write(stream, buffer, bytes_read);
119 }
120 }
121
122 void Stream_delete(Stream * stream) {
123 free(stream->data);
124 free(stream);
125 }
126
127 void Stream_reset(Stream * stream) {
128 stream->length = 0;
129 }

  ViewVC Help
Powered by ViewVC 1.1.24