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
33 #ifdef BABELTRACE_HAVE_FMEMOPEN
37 FILE *babeltrace_fmemopen(void *buf
, size_t size
, const char *mode
)
39 return fmemopen(buf
, size
, mode
);
42 #else /* BABELTRACE_HAVE_FMEMOPEN */
48 * Fallback for systems which don't have fmemopen. Copy buffer to a
49 * temporary file, and use that file as FILE * input.
52 FILE *babeltrace_fmemopen(void *buf
, size_t size
, const char *mode
)
54 char tmpname
[PATH_MAX
];
60 * Support reading only.
62 if (strcmp(mode
, "rb") != 0) {
65 strncpy(tmpname
, "/tmp/babeltrace-tmp-XXXXXX", PATH_MAX
);
66 ret
= mkstemp(tmpname
);
71 * We need to write to the file.
73 fp
= fdopen(ret
, "w+");
77 /* Copy the entire buffer to the file */
78 len
= fwrite(buf
, sizeof(char), size
, fp
);
82 ret
= fseek(fp
, 0L, SEEK_SET
);
87 /* We keep the handle open, but can unlink the file on the VFS. */
88 ret
= unlink(tmpname
);
100 ret
= unlink(tmpname
);
107 #endif /* BABELTRACE_HAVE_FMEMOPEN */
109 #ifdef BABELTRACE_HAVE_OPEN_MEMSTREAM
114 FILE *babeltrace_open_memstream(char **ptr
, size_t *sizeloc
)
116 return open_memstream(ptr
, sizeloc
);
120 int babeltrace_close_memstream(char **buf
, size_t *size
, FILE *fp
)
125 #else /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
131 * Fallback for systems which don't have open_memstream. Create FILE *
132 * with babeltrace_open_memstream, but require call to
133 * babeltrace_close_memstream to flush all data written to the FILE *
134 * into the buffer (which we allocate).
137 FILE *babeltrace_open_memstream(char **ptr
, size_t *sizeloc
)
139 char tmpname
[PATH_MAX
];
143 strncpy(tmpname
, "/tmp/babeltrace-tmp-XXXXXX", PATH_MAX
);
144 ret
= mkstemp(tmpname
);
148 fp
= fdopen(ret
, "w+");
153 * babeltrace_flush_memstream will update the buffer content
154 * with read from fp. No need to keep the file around, just the
157 ret
= unlink(tmpname
);
164 ret
= unlink(tmpname
);
171 /* Get file size, allocate buffer, copy. */
173 int babeltrace_close_memstream(char **buf
, size_t *size
, FILE *fp
)
184 ret
= fseek(fp
, 0L, SEEK_END
);
196 *buf
= calloc(pos
+ 1, sizeof(char));
200 ret
= fseek(fp
, 0L, SEEK_SET
);
205 /* Copy the entire file into the buffer */
208 while (!feof(fp
) && !ferror(fp
) && (*size
- n
> 0)) {
209 len
= fread(*buf
, sizeof(char), *size
- n
, fp
);
234 #endif /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
236 #endif /* _BABELTRACE_FORMAT_CTF_MEMSTREAM_H */
This page took 0.072424 seconds and 5 git commands to generate.