VLC 4.0.0-dev
Loading...
Searching...
No Matches
vlc_memstream.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_memstream.h:
3 *****************************************************************************
4 * Copyright (C) 2016 RĂ©mi Denis-Courmont
5 *
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.
10 *
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.
15 *
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 *****************************************************************************/
20
21#ifndef VLC_MEMSTREAM_H
22# define VLC_MEMSTREAM_H 1
23
24# include <stdarg.h>
25# include <stdio.h>
26
27/**
28 * \defgroup memstream In-memory byte streams
29 * \ingroup cext
30 *
31 * In-memory byte stream are a portable wrapper for in-memory formatted output
32 * byte streams. Compare with POSIX @c open_memstream().
33 *
34 * @{
35 */
36
37/**
38 * In-memory stream object.
39 */
40struct vlc_memstream
42 union
43 {
44 FILE *stream;
45 int error;
46 };
47 char *ptr; /**< Buffer start address */
48 size_t length; /**< Buffer length in bytes */
49};
50
51/**
52 * Initializes a byte stream object.
53 *
54 * @note Even when this function fails, the stream object is initialized and
55 * can be used safely. It is sufficient to check for errors from
56 * vlc_memstream_flush() and vlc_memstream_close().
57 *
58 * Compare with POSIX @c open_memstream().
59 *
60 * @param ms byte stream object
61 *
62 * @retval 0 on success
63 * @retval EOF on error
64 */
66int vlc_memstream_open(struct vlc_memstream *ms);
67
68/**
69 * Flushes a byte stream object.
70 *
71 * This function ensures that any previous write to the byte stream is flushed
72 * and the in-memory buffer is synchronized. It can be used observe the content
73 * of the buffer before the final vlc_memstream_close().
74 *
75 * Compare with @c fflush().
76 *
77 * @note vlc_memstream_close() implicitly flushes the object.
78 * Calling vlc_memstream_flush() before closing is thus superfluous.
79 *
80 * @warning @c ms->ptr must <b>not</b> be freed. It can only be freed after
81 * a successful call to vlc_memstream_close().
82 *
83 * @retval 0 success, i.e., @c ms->ptr and @c ms->length are valid
84 * @retval EOF failure (@c ms->ptr and @c ms->length are unspecified)
85 */
88
89/**
90 * Closes a byte stream object.
91 *
92 * This function flushes the stream object, releases any underlying
93 * resource, except for the heap-allocated formatted buffer @c ms->ptr,
94 * and deinitializes the object.
95 *
96 * On success, the caller is responsible for freeing the buffer with @c free().
97 *
98 * Compare with @c fclose().
99 *
100 * \retval 0 success
101 * \retval EOF failure (@c ms->ptr and @c ms->length are unspecified)
102 */
105
106/**
107 * Appends a binary blob to a byte stream.
108 *
109 * Compare with @c fwrite().
110 *
111 * @param ms the VLC memstream to write to
112 * @param ptr start address of the blob
113 * @param len byte length of the blob
114 */
116size_t vlc_memstream_write(struct vlc_memstream *ms,
117 const void *ptr, size_t len);
118
119/**
120 * Appends a single byte to a byte stream.
121 *
122 * Compare with @c putc() or @c fputc().
123 *
124 * @param ms the VLC memstream to write to
125 * @param c Unsigned byte value converted to int.
126 */
128int vlc_memstream_putc(struct vlc_memstream *ms, int c);
129
130/**
131 * Appends a nul-terminated string to a byte stream.
132 *
133 * Compare with @c fputs().
134 */
136int vlc_memstream_puts(struct vlc_memstream *ms, const char *str);
137
138/**
139 * Appends a formatted string to a byte stream.
140 *
141 * Compare with @c vfprintf().
142 */
144int vlc_memstream_vprintf(struct vlc_memstream *ms, const char *fmt,
145 va_list args);
146
147/**
148 * Appends a formatted string to a byte stream.
149 *
150 * Compare with @c fprintf().
151 */
153int vlc_memstream_printf(struct vlc_memstream *s, const char *fmt,
154 ...) VLC_FORMAT(2,3);
155
156# ifdef __GNUC__
157static inline int vlc_memstream_puts_len(struct vlc_memstream *ms,
158 const char *str, size_t len)
159{
160 return (vlc_memstream_write(ms, str, len) == len) ? (int)len : EOF;
161}
162# define vlc_memstream_puts(ms,s) \
163 (__builtin_constant_p(__builtin_strlen(s)) ? \
164 vlc_memstream_puts_len(ms,s,__builtin_strlen(s)) : \
165 vlc_memstream_puts(ms,s))
166# endif
167
168/** @} */
169
170#endif /* VLC_MEMSTREAM_H */
#define VLC_USED
Definition fourcc_gen.c:32
#define VLC_API
Definition fourcc_gen.c:31
#define VLC_FORMAT(x, y)
String format function annotation.
Definition vlc_common.h:193
int vlc_memstream_open(struct vlc_memstream *ms)
Initializes a byte stream object.
Definition memstream.c:119
int vlc_memstream_flush(struct vlc_memstream *ms)
Flushes a byte stream object.
Definition memstream.c:129
int vlc_memstream_printf(struct vlc_memstream *s, const char *fmt,...)
Appends a formatted string to a byte stream.
Definition memstream.c:214
int vlc_memstream_putc(struct vlc_memstream *ms, int c)
Appends a single byte to a byte stream.
Definition memstream.c:171
size_t vlc_memstream_write(struct vlc_memstream *ms, const void *ptr, size_t len)
Appends a binary blob to a byte stream.
Definition memstream.c:144
int vlc_memstream_puts(struct vlc_memstream *ms, const char *str)
Appends a nul-terminated string to a byte stream.
Definition memstream.c:176
int vlc_memstream_vprintf(struct vlc_memstream *ms, const char *fmt, va_list args)
Appends a formatted string to a byte stream.
Definition memstream.c:182
int vlc_memstream_close(struct vlc_memstream *ms)
Closes a byte stream object.
Definition memstream.c:134
In-memory stream object.
Definition vlc_memstream.h:42
char * ptr
Buffer start address.
Definition vlc_memstream.h:48
FILE * stream
Definition vlc_memstream.h:45
size_t length
Buffer length in bytes.
Definition vlc_memstream.h:49
int error
Definition vlc_memstream.h:46
This file is a collection of common definitions and types.