Tizen 2.0 Release
[profile/ivi/osmesa.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  * Malloc string stream implementation.
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    stream->base.vprintf = &os_default_stream_vprintf;
122
123    stream->str = os_malloc(size);
124    if(!stream->str)
125       goto no_str;
126
127    stream->size = size;
128
129    return &stream->base;
130
131 no_str:
132    os_free(stream);
133 no_stream:
134    return NULL;
135 }
136
137
138 const char *
139 os_str_stream_get(struct os_stream *_stream)
140 {
141    struct os_str_stream *stream = os_str_stream(_stream);
142
143    if (!stream)
144       return NULL;
145
146    stream->str[stream->written] = 0;
147    return stream->str;
148 }
149
150
151 char *
152 os_str_stream_get_and_close(struct os_stream *_stream)
153 {
154    struct os_str_stream *stream = os_str_stream(_stream);
155    char *str;
156
157    if (!stream)
158       return NULL;
159
160    str = stream->str;
161
162    str[stream->written] = 0;
163
164    os_free(stream);
165
166    return str;
167 }