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 *****************************************************************************/
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
))
40 if (ferror(ms
->stream
))
43 return fflush(ms
->stream
);
46 int vlc_memstream_close(struct vlc_memstream
*ms
)
48 FILE *stream
= ms
->stream
;
51 if (unlikely(stream
== NULL
))
68 size_t vlc_memstream_write(struct vlc_memstream
*ms
, const void *ptr
,
71 if (unlikely(ms
->stream
== NULL
))
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
))
82 return fputc(c
, ms
->stream
);
85 int (vlc_memstream_puts
)(struct vlc_memstream
*ms
, const char *str
)
87 if (unlikely(ms
->stream
== NULL
))
90 return fputs(str
, ms
->stream
);
93 int vlc_memstream_vprintf(struct vlc_memstream
*ms
, const char *fmt
,
96 if (unlikely(ms
->stream
== NULL
))
99 return vfprintf(ms
->stream
, fmt
, args
);
105 int vlc_memstream_open(struct vlc_memstream
*ms
)
108 ms
->ptr
= calloc(1, 1);
109 if (unlikely(ms
->ptr
== NULL
))
115 int vlc_memstream_flush(struct vlc_memstream
*ms
)
120 int vlc_memstream_close(struct vlc_memstream
*ms
)
127 size_t vlc_memstream_write(struct vlc_memstream
*ms
, const void *ptr
,
130 char *base
= realloc(ms
->ptr
, ms
->length
+ len
+ 1u);
131 if (unlikely(base
== NULL
))
134 memcpy(base
+ ms
->length
, ptr
, len
);
137 base
[ms
->length
] = '\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
,
164 len
= vsnprintf(NULL
, 0, fmt
, ap
);
170 ptr
= realloc(ms
->ptr
, ms
->length
+ len
+ 1);
174 vsnprintf(ptr
+ ms
->length
, len
+ 1, fmt
, args
);
185 int vlc_memstream_printf(struct vlc_memstream
*ms
, const char *fmt
, ...)
191 ret
= vlc_memstream_vprintf(ms
, fmt
, ap
);