30d974ca4aaf748fba791d7f19c476f0b6721e66
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__ */
230 * Fallback for systems which don't have open_memstream. Create FILE *
231 * with bt_open_memstream, but require call to
232 * bt_close_memstream to flush all data written to the FILE *
233 * into the buffer (which we allocate).
236 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
242 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
244 ret
= mkstemp(tmpname
);
250 fp
= fdopen(ret
, "wb+");
255 * babeltrace_flush_memstream will update the buffer content
256 * with read from fp. No need to keep the file around, just the
259 ret
= unlink(tmpname
);
267 ret
= unlink(tmpname
);
275 #endif /* __MINGW32__ */
277 /* Get file size, allocate buffer, copy. */
279 int bt_close_memstream(char **buf
, size_t *size
, FILE *fp
)
290 ret
= fseek(fp
, 0L, SEEK_END
);
302 *buf
= calloc(pos
+ 1, sizeof(char));
306 ret
= fseek(fp
, 0L, SEEK_SET
);
311 /* Copy the entire file into the buffer */
314 while (!feof(fp
) && !ferror(fp
) && (*size
- n
> 0)) {
315 len
= fread(*buf
, sizeof(char), *size
- n
, fp
);
340 #endif /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
342 #endif /* _BABELTRACE_FORMAT_CTF_MEMSTREAM_H */
This page took 0.037208 seconds and 4 git commands to generate.