os: Add a growable string stream.
[mesa.git] / src / gallium / auxiliary / os / os_stream_str.c
1 /**************************************************************************
2 *
3 * Copyright 2008-2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Stream implementation based on the Standard C Library.
31 */
32
33 #include "pipe/p_config.h"
34
35 #include "os_memory.h"
36 #include "os_stream.h"
37
38
39 struct os_str_stream
40 {
41 struct os_stream base;
42
43 char *str;
44
45 size_t size;
46 size_t written;
47 };
48
49
50 static INLINE struct os_str_stream *
51 os_str_stream(struct os_stream *stream)
52 {
53 return (struct os_str_stream *)stream;
54 }
55
56
57 static void
58 os_str_stream_close(struct os_stream *_stream)
59 {
60 struct os_str_stream *stream = os_str_stream(_stream);
61
62 os_free(stream->str);
63
64 os_free(stream);
65 }
66
67
68 static boolean
69 os_str_stream_write(struct os_stream *_stream, const void *data, size_t size)
70 {
71 struct os_str_stream *stream = os_str_stream(_stream);
72 size_t minimum_size;
73 boolean ret = TRUE;
74
75 minimum_size = stream->written + size + 1;
76 if (stream->size < minimum_size) {
77 size_t new_size = stream->size;
78 char * new_str;
79
80 do {
81 new_size *= 2;
82 } while (new_size < minimum_size);
83
84 new_str = os_realloc(stream->str, stream->size, new_size);
85 if (new_str) {
86 stream->str = new_str;
87 stream->size = new_size;
88 }
89 else {
90 size = stream->size - stream->written - 1;
91 ret = FALSE;
92 }
93 }
94
95 memcpy(stream->str + stream->written, data, size);
96 stream->written += size;
97
98 return ret;
99 }
100
101
102 static void
103 os_str_stream_flush(struct os_stream *stream)
104 {
105 (void)stream;
106 }
107
108
109 struct os_stream *
110 os_str_stream_create(size_t size)
111 {
112 struct os_str_stream *stream;
113
114 stream = (struct os_str_stream *)os_calloc(1, sizeof(*stream));
115 if(!stream)
116 goto no_stream;
117
118 stream->base.close = &os_str_stream_close;
119 stream->base.write = &os_str_stream_write;
120 stream->base.flush = &os_str_stream_flush;
121
122 stream->str = os_malloc(size);
123 if(!stream->str)
124 goto no_str;
125
126 stream->size = size;
127
128 return &stream->base;
129
130 no_str:
131 os_free(stream);
132 no_stream:
133 return NULL;
134 }
135
136
137 const char *
138 os_str_stream_get(struct os_stream *_stream)
139 {
140 struct os_str_stream *stream = os_str_stream(_stream);
141
142 if (!stream)
143 return NULL;
144
145 stream->str[stream->written] = 0;
146 return stream->str;
147 }
148
149
150 char *
151 os_str_stream_get_and_close(struct os_stream *_stream)
152 {
153 struct os_str_stream *stream = os_str_stream(_stream);
154 char *str;
155
156 if (!stream)
157 return NULL;
158
159 str = stream->str;
160
161 str[stream->written] = 0;
162
163 os_free(stream);
164
165 return str;
166 }