revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / gallium / auxiliary / os / os_stream_str.c
blobbe9478b2a170ba099fb112fbad6d05dcce201d11
1 /**************************************************************************
3 * Copyright 2008-2010 VMware, Inc.
4 * All Rights Reserved.
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:
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.
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.
26 **************************************************************************/
28 /**
29 * @file
30 * Malloc string stream implementation.
33 #include "pipe/p_config.h"
35 #include "os_memory.h"
36 #include "os_stream.h"
39 struct os_str_stream
41 struct os_stream base;
43 char *str;
45 size_t size;
46 size_t written;
50 static INLINE struct os_str_stream *
51 os_str_stream(struct os_stream *stream)
53 return (struct os_str_stream *)stream;
57 static void
58 os_str_stream_close(struct os_stream *_stream)
60 struct os_str_stream *stream = os_str_stream(_stream);
62 os_free(stream->str);
64 os_free(stream);
68 static boolean
69 os_str_stream_write(struct os_stream *_stream, const void *data, size_t size)
71 struct os_str_stream *stream = os_str_stream(_stream);
72 size_t minimum_size;
73 boolean ret = TRUE;
75 minimum_size = stream->written + size + 1;
76 if (stream->size < minimum_size) {
77 size_t new_size = stream->size;
78 char * new_str;
80 do {
81 new_size *= 2;
82 } while (new_size < minimum_size);
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;
89 else {
90 size = stream->size - stream->written - 1;
91 ret = FALSE;
95 memcpy(stream->str + stream->written, data, size);
96 stream->written += size;
98 return ret;
102 static void
103 os_str_stream_flush(struct os_stream *stream)
105 (void)stream;
109 struct os_stream *
110 os_str_stream_create(size_t size)
112 struct os_str_stream *stream;
114 stream = (struct os_str_stream *)os_calloc(1, sizeof(*stream));
115 if(!stream)
116 goto no_stream;
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 stream->base.vprintf = &os_default_stream_vprintf;
123 stream->str = os_malloc(size);
124 if(!stream->str)
125 goto no_str;
127 stream->size = size;
129 return &stream->base;
131 no_str:
132 os_free(stream);
133 no_stream:
134 return NULL;
138 const char *
139 os_str_stream_get(struct os_stream *_stream)
141 struct os_str_stream *stream = os_str_stream(_stream);
143 if (!stream)
144 return NULL;
146 stream->str[stream->written] = 0;
147 return stream->str;
151 char *
152 os_str_stream_get_and_close(struct os_stream *_stream)
154 struct os_str_stream *stream = os_str_stream(_stream);
155 char *str;
157 if (!stream)
158 return NULL;
160 str = stream->str;
162 str[stream->written] = 0;
164 os_free(stream);
166 return str;