1 /**************************************************************************
3 * Copyright 2008-2010 VMware, Inc.
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
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 **************************************************************************/
30 * Malloc string stream implementation.
33 #include "pipe/p_config.h"
35 #include "os_memory.h"
36 #include "os_stream.h"
41 struct os_stream base
;
50 static INLINE
struct os_str_stream
*
51 os_str_stream(struct os_stream
*stream
)
53 return (struct os_str_stream
*)stream
;
58 os_str_stream_close(struct os_stream
*_stream
)
60 struct os_str_stream
*stream
= os_str_stream(_stream
);
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
);
75 minimum_size
= stream
->written
+ size
+ 1;
76 if (stream
->size
< minimum_size
) {
77 size_t new_size
= stream
->size
;
82 } while (new_size
< minimum_size
);
84 new_str
= os_realloc(stream
->str
, stream
->size
, new_size
);
86 stream
->str
= new_str
;
87 stream
->size
= new_size
;
90 size
= stream
->size
- stream
->written
- 1;
95 memcpy(stream
->str
+ stream
->written
, data
, size
);
96 stream
->written
+= size
;
103 os_str_stream_flush(struct os_stream
*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
));
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
);
129 return &stream
->base
;
139 os_str_stream_get(struct os_stream
*_stream
)
141 struct os_str_stream
*stream
= os_str_stream(_stream
);
146 stream
->str
[stream
->written
] = 0;
152 os_str_stream_get_and_close(struct os_stream
*_stream
)
154 struct os_str_stream
*stream
= os_str_stream(_stream
);
162 str
[stream
->written
] = 0;