1 #ifndef _BABELTRACE_FORMAT_CTF_MEMSTREAM_H
2 #define _BABELTRACE_FORMAT_CTF_MEMSTREAM_H
5 * format/ctf/memstream.h
7 * Copyright 2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * memstream compatibility layer.
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 #ifdef BABELTRACE_HAVE_FMEMOPEN
34 FILE *bt_fmemopen(void *buf
, size_t size
, const char *mode
)
36 return fmemopen(buf
, size
, mode
);
39 #else /* BABELTRACE_HAVE_FMEMOPEN */
45 #include <babeltrace/endian-internal.h>
53 * Fallback for systems which don't have fmemopen. Copy buffer to a
54 * temporary file, and use that file as FILE * input.
57 FILE *bt_fmemopen(void *buf
, size_t size
, const char *mode
)
65 * Support reading only.
67 if (strcmp(mode
, "rb") != 0) {
71 /* Build a temporary filename */
72 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
73 if (_mktemp(tmpname
) == NULL
) {
78 * Open as a read/write binary temporary deleted on close file.
79 * Will be deleted when the last file pointer is closed.
81 fp
= fopen(tmpname
, "w+bTD");
86 /* Copy the entire buffer to the file */
87 len
= fwrite(buf
, sizeof(char), size
, fp
);
92 /* Set the file pointer to the start of file */
93 ret
= fseek(fp
, 0L, SEEK_SET
);
112 #else /* __MINGW32__ */
115 * Fallback for systems which don't have fmemopen. Copy buffer to a
116 * temporary file, and use that file as FILE * input.
119 FILE *bt_fmemopen(void *buf
, size_t size
, const char *mode
)
127 * Support reading only.
129 if (strcmp(mode
, "rb") != 0) {
133 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
134 ret
= mkstemp(tmpname
);
140 * We need to write to the file.
142 fp
= fdopen(ret
, "w+");
146 /* Copy the entire buffer to the file */
147 len
= fwrite(buf
, sizeof(char), size
, fp
);
151 ret
= fseek(fp
, 0L, SEEK_SET
);
156 /* We keep the handle open, but can unlink the file on the VFS. */
157 ret
= unlink(tmpname
);
170 ret
= unlink(tmpname
);
178 #endif /* __MINGW32__ */
180 #endif /* BABELTRACE_HAVE_FMEMOPEN */
183 #ifdef BABELTRACE_HAVE_OPEN_MEMSTREAM
188 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
190 return open_memstream(ptr
, sizeloc
);
194 int bt_close_memstream(char **buf
, size_t *size
, FILE *fp
)
199 #else /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
208 * Fallback for systems which don't have open_memstream. Create FILE *
209 * with bt_open_memstream, but require call to
210 * bt_close_memstream to flush all data written to the FILE *
211 * into the buffer (which we allocate).
214 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
219 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
221 if (_mktemp(tmpname
) == NULL
) {
226 * Open as a read/write binary temporary deleted on close file.
227 * Will be deleted when the last file pointer is closed.
229 fp
= fopen(tmpname
, "w+bTD");
242 #else /* __MINGW32__ */
245 * Fallback for systems which don't have open_memstream. Create FILE *
246 * with bt_open_memstream, but require call to
247 * bt_close_memstream to flush all data written to the FILE *
248 * into the buffer (which we allocate).
251 FILE *bt_open_memstream(char **ptr
, size_t *sizeloc
)
257 tmpname
= g_build_filename(g_get_tmp_dir(), "babeltrace-tmp-XXXXXX", NULL
);
259 ret
= mkstemp(tmpname
);
265 fp
= fdopen(ret
, "w+");
270 * babeltrace_flush_memstream will update the buffer content
271 * with read from fp. No need to keep the file around, just the
274 ret
= unlink(tmpname
);
282 ret
= unlink(tmpname
);
290 #endif /* __MINGW32__ */
292 /* Get file size, allocate buffer, copy. */
294 int bt_close_memstream(char **buf
, size_t *size
, FILE *fp
)
305 ret
= fseek(fp
, 0L, SEEK_END
);
317 *buf
= calloc(pos
+ 1, sizeof(char));
321 ret
= fseek(fp
, 0L, SEEK_SET
);
326 /* Copy the entire file into the buffer */
329 while (!feof(fp
) && !ferror(fp
) && (*size
- n
> 0)) {
330 len
= fread(*buf
, sizeof(char), *size
- n
, fp
);
355 #endif /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
357 #endif /* _BABELTRACE_FORMAT_CTF_MEMSTREAM_H */
This page took 0.038024 seconds and 4 git commands to generate.