1 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
2
3 #if !IS_CPLUSPLUS
4 #define memory_ostream_representation any_ostream_representation
5 #endif
6 #line 1 "memory-ostream.oo.c"
7 /* Output stream that accumulates the output in memory.
8 Copyright (C) 2006-2007, 2019-2020, 2023 Free Software Foundation, Inc.
9 Written by Bruno Haible <bruno@clisp.org>, 2006.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <https://www.gnu.org/licenses/>. */
23
24 #include <config.h>
25
26 /* Specification. */
27 #include "memory-ostream.h"
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "error.h"
33 #include "xalloc.h"
34 #include "xsize.h"
35 #include "gettext.h"
36
37 #define _(str) gettext (str)
38
39 #line 40 "memory-ostream.c"
40 #include "memory_ostream.priv.h"
41
42 const typeinfo_t memory_ostream_typeinfo = { "memory_ostream" };
43
44 static const typeinfo_t * const memory_ostream_superclasses[] =
45 { memory_ostream_SUPERCLASSES };
46
47 #define super ostream_vtable
48
49 #line 40 "memory-ostream.oo.c"
50
51 /* Implementation of ostream_t methods. */
52
53 static void
54 memory_ostream__write_mem (memory_ostream_t stream,
55 const void *data, size_t len)
56 {
57 if (len > 0)
58 {
59 if (len > stream->allocated - stream->buflen)
60 {
61 size_t new_allocated =
62 xmax (xsum (stream->buflen, len),
63 xsum (stream->allocated, stream->allocated));
64 if (size_overflow_p (new_allocated))
65 error (EXIT_FAILURE, 0,
66 _("%s: too much output, buffer size overflow"),
67 "memory_ostream");
68 stream->buffer = (char *) xrealloc (stream->buffer, new_allocated);
69 stream->allocated = new_allocated;
70 }
71 memcpy (stream->buffer + stream->buflen, data, len);
72 stream->buflen += len;
73 }
74 }
75
76 static void
77 memory_ostream__flush (memory_ostream_t stream, ostream_flush_scope_t scope)
78 {
79 }
80
81 static void
82 memory_ostream__free (memory_ostream_t stream)
83 {
84 free (stream->buffer);
85 free (stream);
86 }
87
88 /* Implementation of memory_ostream_t methods. */
89
90 static void
91 memory_ostream__contents (memory_ostream_t stream,
92 const void **bufp, size_t *buflenp)
93 {
94 *bufp = stream->buffer;
95 *buflenp = stream->buflen;
96 }
97
98 /* Constructor. */
99
100 memory_ostream_t
101 memory_ostream_create (void)
102 {
103 memory_ostream_t stream = XMALLOC (struct memory_ostream_representation);
104
105 stream->base.vtable = &memory_ostream_vtable;
106 stream->allocated = 250;
107 stream->buffer = XNMALLOC (stream->allocated, char);
108 stream->buflen = 0;
109
110 return stream;
111 }
112
113 /* Instanceof test. */
114
115 bool
116 is_instance_of_memory_ostream (ostream_t stream)
117 {
118 return IS_INSTANCE (stream, ostream, memory_ostream);
119 }
120
121 #line 122 "memory-ostream.c"
122
123 const struct memory_ostream_implementation memory_ostream_vtable =
124 {
125 memory_ostream_superclasses,
126 sizeof (memory_ostream_superclasses) / sizeof (memory_ostream_superclasses[0]),
127 sizeof (struct memory_ostream_representation),
128 memory_ostream__write_mem,
129 memory_ostream__flush,
130 memory_ostream__free,
131 memory_ostream__contents,
132 };
133
134 #if !HAVE_INLINE
135
136 /* Define the functions that invoke the methods. */
137
138 void
139 memory_ostream_write_mem (memory_ostream_t first_arg, const void *data, size_t len)
140 {
141 const struct memory_ostream_implementation *vtable =
142 ((struct memory_ostream_representation_header *) (struct memory_ostream_representation *) first_arg)->vtable;
143 vtable->write_mem (first_arg,data,len);
144 }
145
146 void
147 memory_ostream_flush (memory_ostream_t first_arg, ostream_flush_scope_t scope)
148 {
149 const struct memory_ostream_implementation *vtable =
150 ((struct memory_ostream_representation_header *) (struct memory_ostream_representation *) first_arg)->vtable;
151 vtable->flush (first_arg,scope);
152 }
153
154 void
155 memory_ostream_free (memory_ostream_t first_arg)
156 {
157 const struct memory_ostream_implementation *vtable =
158 ((struct memory_ostream_representation_header *) (struct memory_ostream_representation *) first_arg)->vtable;
159 vtable->free (first_arg);
160 }
161
162 void
163 memory_ostream_contents (memory_ostream_t first_arg, const void **bufp, size_t *buflenp)
164 {
165 const struct memory_ostream_implementation *vtable =
166 ((struct memory_ostream_representation_header *) (struct memory_ostream_representation *) first_arg)->vtable;
167 vtable->contents (first_arg,bufp,buflenp);
168 }
169
170 #endif