1 /*****************************************************************************
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 #ifndef VLC_MEMSTREAM_H
22 # define VLC_MEMSTREAM_H 1
28 * \defgroup memstream In-memory byte streams
31 * In-memory byte stream are a portable wrapper for in-memory formatted output
32 * byte streams. Compare with POSIX @c open_memstream().
38 * In-memory stream object.
47 char *ptr
; /**< Buffer start address */
48 size_t length
; /**< Buffer length in bytes */
52 * Initializes a byte stream object.
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().
58 * Compare with POSIX @c open_memstream().
60 * @param ms byte stream object
62 * @retval 0 on success
63 * @retval EOF on error
66 int vlc_memstream_open(struct vlc_memstream
*ms
);
69 * Flushes a byte stream object.
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().
75 * Compare with @c fflush().
77 * @note vlc_memstream_close() implicitly flushes the object.
78 * Calling vlc_memstream_flush() before closing is thus superfluous.
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().
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)
87 int vlc_memstream_flush(struct vlc_memstream
*ms
) VLC_USED
;
90 * Closes a byte stream object.
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.
96 * On success, the caller is responsible for freeing the buffer with @c free().
98 * Compare with @c fclose().
101 * \retval EOF failure (@c ms->ptr and @c ms->length are unspecified)
104 int vlc_memstream_close(struct vlc_memstream
*ms
) VLC_USED
;
107 * Appends a binary blob to a byte stream.
109 * Compare with @c fwrite().
111 * @param ptr start address of the blob
112 * @param length byte length of the blob
115 size_t vlc_memstream_write(struct vlc_memstream
*ms
,
116 const void *ptr
, size_t len
);
119 * Appends a single byte to a byte stream.
121 * Compare with @c putc() or @c fputc().
123 * @param Unsigned byte value converted to int.
126 int vlc_memstream_putc(struct vlc_memstream
*ms
, int c
);
129 * Appends a nul-terminated string to a byte stream.
131 * Compare with @c fputs().
134 int vlc_memstream_puts(struct vlc_memstream
*ms
, const char *str
);
137 * Appends a formatted string to a byte stream.
139 * Compare with @c vfprintf().
142 int vlc_memstream_vprintf(struct vlc_memstream
*ms
, const char *fmt
,
146 * Appends a formatted string to a byte stream.
148 * Compare with @c fprintf().
151 int vlc_memstream_printf(struct vlc_memstream
*s
, const char *fmt
,
152 ...) VLC_FORMAT(2,3);
155 static inline int vlc_memstream_puts_len(struct vlc_memstream
*ms
,
156 const char *str
, size_t len
)
158 return (vlc_memstream_write(ms
, str
, len
) == len
) ? (int)len
: EOF
;
160 # define vlc_memstream_puts(ms,s) \
161 (__builtin_constant_p(__builtin_strlen(s)) ? \
162 vlc_memstream_puts_len(ms,s,__builtin_strlen(s)) : \
163 vlc_memstream_puts(ms,s))
168 #endif /* VLC_MEMSTREAM_H */