Revert "contrib: Allow a minimum version to be required"
[vlc.git] / src / text / memstream.c
blob093a419cf875a848d146d5c9a1e3d35d2e530534
1 /*****************************************************************************
2 * memstream.c: heap-based output streams
3 *****************************************************************************
4 * Copyright (C) 2016 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <vlc_common.h>
26 #include <vlc_memstream.h>
28 #ifdef HAVE_OPEN_MEMSTREAM
29 int vlc_memstream_open(struct vlc_memstream *ms)
31 ms->stream = open_memstream(&ms->ptr, &ms->length);
32 return likely(ms->stream != NULL) ? 0 : EOF;
35 int vlc_memstream_flush(struct vlc_memstream *ms)
37 if (unlikely(ms->stream == NULL))
38 return EOF;
40 if (ferror(ms->stream))
41 return EOF;
43 return fflush(ms->stream);
46 int vlc_memstream_close(struct vlc_memstream *ms)
48 FILE *stream = ms->stream;
49 int ret;
51 if (unlikely(stream == NULL))
52 return EOF;
54 ms->stream = NULL;
55 ret = ferror(stream);
57 if (fclose(stream))
58 return EOF;
60 if (unlikely(ret))
62 free(ms->ptr);
63 return EOF;
65 return 0;
68 size_t vlc_memstream_write(struct vlc_memstream *ms, const void *ptr,
69 size_t len)
71 if (unlikely(ms->stream == NULL))
72 return 0;
74 return fwrite(ptr, 1, len, ms->stream);
77 int vlc_memstream_putc(struct vlc_memstream *ms, int c)
79 if (unlikely(ms->stream == NULL))
80 return EOF;
82 return fputc(c, ms->stream);
85 int (vlc_memstream_puts)(struct vlc_memstream *ms, const char *str)
87 if (unlikely(ms->stream == NULL))
88 return EOF;
90 return fputs(str, ms->stream);
93 int vlc_memstream_vprintf(struct vlc_memstream *ms, const char *fmt,
94 va_list args)
96 if (unlikely(ms->stream == NULL))
97 return EOF;
99 return vfprintf(ms->stream, fmt, args);
102 #else
103 #include <stdlib.h>
105 int vlc_memstream_open(struct vlc_memstream *ms)
107 ms->error = 0;
108 ms->ptr = calloc(1, 1);
109 if (unlikely(ms->ptr == NULL))
110 ms->error = EOF;
111 ms->length = 0;
112 return ms->error;
115 int vlc_memstream_flush(struct vlc_memstream *ms)
117 return ms->error;
120 int vlc_memstream_close(struct vlc_memstream *ms)
122 if (ms->error)
123 free(ms->ptr);
124 return ms->error;
127 size_t vlc_memstream_write(struct vlc_memstream *ms, const void *ptr,
128 size_t len)
130 char *base = realloc(ms->ptr, ms->length + len + 1u);
131 if (unlikely(base == NULL))
132 goto error;
134 memcpy(base + ms->length, ptr, len);
135 ms->ptr = base;
136 ms->length += len;
137 base[ms->length] = '\0';
138 return len;
140 error:
141 ms->error = EOF;
142 return 0;
145 int vlc_memstream_putc(struct vlc_memstream *ms, int c)
147 return (vlc_memstream_write(ms, &(unsigned char){ c }, 1u) == 1) ? c : EOF;
150 int (vlc_memstream_puts)(struct vlc_memstream *ms, const char *str)
152 size_t len = strlen(str);
153 return (vlc_memstream_write(ms, str, len) == len) ? 0 : EOF;
156 int vlc_memstream_vprintf(struct vlc_memstream *ms, const char *fmt,
157 va_list args)
159 va_list ap;
160 char *ptr;
161 int len;
163 va_copy(ap, args);
164 len = vsnprintf(NULL, 0, fmt, ap);
165 va_end(ap);
167 if (len < 0)
168 goto error;
170 ptr = realloc(ms->ptr, ms->length + len + 1);
171 if (ptr == NULL)
172 goto error;
174 vsnprintf(ptr + ms->length, len + 1, fmt, args);
175 ms->ptr = ptr;
176 ms->length += len;
177 return len;
179 error:
180 ms->error = EOF;
181 return EOF;
183 #endif
185 int vlc_memstream_printf(struct vlc_memstream *ms, const char *fmt, ...)
187 va_list ap;
188 int ret;
190 va_start(ap, fmt);
191 ret = vlc_memstream_vprintf(ms, fmt, ap);
192 va_end(ap);
193 return ret;