1 #ifndef _BABELTRACE_FORMAT_CTF_MEMSTREAM_H
2 #define _BABELTRACE_FORMAT_CTF_MEMSTREAM_H
5 * Copyright 2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * memstream compatibility layer.
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #ifdef BABELTRACE_HAVE_FMEMOPEN
32 FILE *bt_fmemopen(void *buf
, size_t size
, const char *mode
)
34 return fmemopen(buf
, size
, mode
);
37 #else /* BABELTRACE_HAVE_FMEMOPEN */
44 #include <babeltrace/endian-internal.h>
52 * Fallback for systems which don't have fmemopen. Copy buffer to a
53 * temporary file, and use that file as FILE * input.
56 FILE *bt_fmemopen(void *buf
, size_t size
, const char *mode
)
64 * Support reading only.
66 if (strcmp(mode
, "rb") != 0) {
70 /* Build a temporary filename */
71 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
72 if (_mktemp(tmpname
) == NULL
) {
77 * Open as a read/write binary temporary deleted on close file.
78 * Will be deleted when the last file pointer is closed.
80 fp
= fopen(tmpname
, "w+bTD");
85 /* Copy the entire buffer to the file */
86 len
= fwrite(buf
, sizeof(char), size
, fp
);
91 /* Set the file pointer to the start of file */
92 ret
= fseek(fp
, 0L, SEEK_SET
);
111 #else /* __MINGW32__ */
114 * Fallback for systems which don't have fmemopen. Copy buffer to a
115 * temporary file, and use that file as FILE * input.
118 FILE *bt_fmemopen(void *buf
, size_t size
, const char *mode
)
126 * Support reading only.
128 if (strcmp(mode
, "rb") != 0) {
132 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
133 ret
= mkstemp(tmpname
);
139 * We need to write to the file.
141 fp
= fdopen(ret
, "wb+");
145 /* Copy the entire buffer to the file */
146 len
= fwrite(buf
, sizeof(char), size
, fp
);
150 ret
= fseek(fp
, 0L, SEEK_SET
);
155 /* We keep the handle open, but can unlink the file on the VFS. */
156 ret
= unlink(tmpname
);
169 ret
= unlink(tmpname
);
177 #endif /* __MINGW32__ */
179 #endif /* BABELTRACE_HAVE_FMEMOPEN */
182 #ifdef BABELTRACE_HAVE_OPEN_MEMSTREAM
187 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
189 return open_memstream(ptr
, sizeloc
);
193 int bt_close_memstream(char **buf
, size_t *size
, FILE *fp
)
198 #else /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
207 * Fallback for systems which don't have open_memstream. Create FILE *
208 * with bt_open_memstream, but require call to
209 * bt_close_memstream to flush all data written to the FILE *
210 * into the buffer (which we allocate).
213 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
218 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
220 if (_mktemp(tmpname
) == NULL
) {
225 * Open as a read/write binary temporary deleted on close file.
226 * Will be deleted when the last file pointer is closed.
228 fp
= fopen(tmpname
, "w+bTD");
241 #else /* __MINGW32__ */
244 * Fallback for systems which don't have open_memstream. Create FILE *
245 * with bt_open_memstream, but require call to
246 * bt_close_memstream to flush all data written to the FILE *
247 * into the buffer (which we allocate).
250 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
256 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
258 ret
= mkstemp(tmpname
);
264 fp
= fdopen(ret
, "wb+");
269 * babeltrace_flush_memstream will update the buffer content
270 * with read from fp. No need to keep the file around, just the
273 ret
= unlink(tmpname
);
281 ret
= unlink(tmpname
);
289 #endif /* __MINGW32__ */
291 /* Get file size, allocate buffer, copy. */
293 int bt_close_memstream(char **buf
, size_t *size
, FILE *fp
)
304 ret
= fseek(fp
, 0L, SEEK_END
);
316 *buf
= calloc(pos
+ 1, sizeof(char));
320 ret
= fseek(fp
, 0L, SEEK_SET
);
325 /* Copy the entire file into the buffer */
328 while (!feof(fp
) && !ferror(fp
) && (*size
- n
> 0)) {
329 len
= fread(*buf
, sizeof(char), *size
- n
, fp
);
354 #endif /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
356 #endif /* _BABELTRACE_FORMAT_CTF_MEMSTREAM_H */
This page took 0.036257 seconds and 4 git commands to generate.