2 * SPDX-License-Identifier: MIT
4 * Copyright 2012 (c) Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * memstream compatibility layer.
9 #ifndef _BABELTRACE_FORMAT_CTF_MEMSTREAM_H
10 #define _BABELTRACE_FORMAT_CTF_MEMSTREAM_H
12 #ifdef BABELTRACE_HAVE_FMEMOPEN
16 FILE *bt_fmemopen(void *buf
, size_t size
, const char *mode
)
18 return fmemopen(buf
, size
, mode
);
21 #else /* BABELTRACE_HAVE_FMEMOPEN */
28 #include "compat/endian.h"
36 * Fallback for systems which don't have fmemopen. Copy buffer to a
37 * temporary file, and use that file as FILE * input.
40 FILE *bt_fmemopen(void *buf
, size_t size
, const char *mode
)
48 * Support reading only.
50 if (strcmp(mode
, "rb") != 0) {
54 /* Build a temporary filename */
55 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
56 if (!_mktemp(tmpname
)) {
61 * Open as a read/write binary temporary deleted on close file.
62 * Will be deleted when the last file pointer is closed.
64 fp
= fopen(tmpname
, "w+bTD");
69 /* Copy the entire buffer to the file */
70 len
= fwrite(buf
, sizeof(char), size
, fp
);
75 /* Set the file pointer to the start of file */
76 ret
= fseek(fp
, 0L, SEEK_SET
);
95 #else /* __MINGW32__ */
98 * Fallback for systems which don't have fmemopen. Copy buffer to a
99 * temporary file, and use that file as FILE * input.
102 FILE *bt_fmemopen(void *buf
, size_t size
, const char *mode
)
110 * Support reading only.
112 if (strcmp(mode
, "rb") != 0) {
116 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
117 ret
= mkstemp(tmpname
);
123 * We need to write to the file.
125 fp
= fdopen(ret
, "wb+");
129 /* Copy the entire buffer to the file */
130 len
= fwrite(buf
, sizeof(char), size
, fp
);
134 ret
= fseek(fp
, 0L, SEEK_SET
);
139 /* We keep the handle open, but can unlink the file on the VFS. */
140 ret
= unlink(tmpname
);
153 ret
= unlink(tmpname
);
161 #endif /* __MINGW32__ */
163 #endif /* BABELTRACE_HAVE_FMEMOPEN */
166 #ifdef BABELTRACE_HAVE_OPEN_MEMSTREAM
171 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
173 return open_memstream(ptr
, sizeloc
);
177 int bt_close_memstream(char **buf
, size_t *size
, FILE *fp
)
182 #else /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
191 * Fallback for systems which don't have open_memstream. Create FILE *
192 * with bt_open_memstream, but require call to
193 * bt_close_memstream to flush all data written to the FILE *
194 * into the buffer (which we allocate).
197 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
202 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
204 if (!_mktemp(tmpname
)) {
209 * Open as a read/write binary temporary deleted on close file.
210 * Will be deleted when the last file pointer is closed.
212 fp
= fopen(tmpname
, "w+bTD");
225 #else /* __MINGW32__ */
228 * Fallback for systems which don't have open_memstream. Create FILE *
229 * with bt_open_memstream, but require call to
230 * bt_close_memstream to flush all data written to the FILE *
231 * into the buffer (which we allocate).
234 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
240 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
242 ret
= mkstemp(tmpname
);
248 fp
= fdopen(ret
, "wb+");
253 * babeltrace_flush_memstream will update the buffer content
254 * with read from fp. No need to keep the file around, just the
257 ret
= unlink(tmpname
);
265 ret
= unlink(tmpname
);
273 #endif /* __MINGW32__ */
275 /* Get file size, allocate buffer, copy. */
277 int bt_close_memstream(char **buf
, size_t *size
, FILE *fp
)
288 ret
= fseek(fp
, 0L, SEEK_END
);
300 *buf
= calloc(pos
+ 1, sizeof(char));
304 ret
= fseek(fp
, 0L, SEEK_SET
);
309 /* Copy the entire file into the buffer */
312 while (!feof(fp
) && !ferror(fp
) && (*size
- n
> 0)) {
313 len
= fread(*buf
, sizeof(char), *size
- n
, fp
);
338 #endif /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
340 #endif /* _BABELTRACE_FORMAT_CTF_MEMSTREAM_H */
This page took 0.060373 seconds and 4 git commands to generate.