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 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
12 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
14 * Permission is hereby granted to use or copy this program
15 * for any purpose, provided the above notices are retained on all copies.
16 * Permission to modify the code and to distribute modified code is granted,
17 * provided the above notices are retained, and a notice that the code was
18 * modified is included with the above copyright notice.
24 #ifdef BABELTRACE_HAVE_FMEMOPEN
28 FILE *babeltrace_fmemopen(void *buf
, size_t size
, const char *mode
)
30 return fmemopen(buf
, size
, mode
);
33 #else /* BABELTRACE_HAVE_FMEMOPEN */
39 * Fallback for systems which don't have fmemopen. Copy buffer to a
40 * temporary file, and use that file as FILE * input.
43 FILE *babeltrace_fmemopen(void *buf
, size_t size
, const char *mode
)
45 char tmpname
[PATH_MAX
];
51 * Support reading only.
53 if (strcmp(mode
, "rb") != 0) {
56 strncpy(tmpname
, "/tmp/babeltrace-tmp-XXXXXX", PATH_MAX
);
57 ret
= mkstemp(tmpname
);
62 * We need to write to the file.
64 fp
= fdopen(ret
, "w+");
68 /* Copy the entire buffer to the file */
69 len
= fwrite(buf
, sizeof(char), size
, fp
);
73 ret
= fseek(fp
, 0L, SEEK_SET
);
78 /* We keep the handle open, but can unlink the file on the VFS. */
79 ret
= unlink(tmpname
);
91 ret
= unlink(tmpname
);
98 #endif /* BABELTRACE_HAVE_FMEMOPEN */
100 #ifdef BABELTRACE_HAVE_OPEN_MEMSTREAM
105 FILE *babeltrace_open_memstream(char **ptr
, size_t *sizeloc
)
107 return open_memstream(ptr
, sizeloc
);
111 int babeltrace_close_memstream(char **buf
, size_t *size
, FILE *fp
)
116 #else /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
122 * Fallback for systems which don't have open_memstream. Create FILE *
123 * with babeltrace_open_memstream, but require call to
124 * babeltrace_close_memstream to flush all data written to the FILE *
125 * into the buffer (which we allocate).
128 FILE *babeltrace_open_memstream(char **ptr
, size_t *sizeloc
)
130 char tmpname
[PATH_MAX
];
134 strncpy(tmpname
, "/tmp/babeltrace-tmp-XXXXXX", PATH_MAX
);
135 ret
= mkstemp(tmpname
);
139 fp
= fdopen(ret
, "w+");
144 * babeltrace_flush_memstream will update the buffer content
145 * with read from fp. No need to keep the file around, just the
148 ret
= unlink(tmpname
);
155 ret
= unlink(tmpname
);
162 /* Get file size, allocate buffer, copy. */
164 int babeltrace_close_memstream(char **buf
, size_t *size
, FILE *fp
)
175 ret
= fseek(fp
, 0L, SEEK_END
);
187 *buf
= calloc(pos
+ 1, sizeof(char));
191 ret
= fseek(fp
, 0L, SEEK_SET
);
196 /* Copy the entire file into the buffer */
199 while (!feof(fp
) && !ferror(fp
) && (*size
- n
> 0)) {
200 len
= fread(*buf
, sizeof(char), *size
- n
, fp
);
225 #endif /* BABELTRACE_HAVE_OPEN_MEMSTREAM */
227 #endif /* _BABELTRACE_FORMAT_CTF_MEMSTREAM_H */
This page took 0.034205 seconds and 4 git commands to generate.