Use O_RDWR for open write mode (for mmap)
[babeltrace.git] / formats / ctf-text / ctf-text.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * CTF Text Format registration.
5 *
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19 #include <babeltrace/format.h>
20 #include <babeltrace/ctf-text/types.h>
21 #include <babeltrace/babeltrace.h>
22 #include <inttypes.h>
23 #include <uuid/uuid.h>
24 #include <sys/mman.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <dirent.h>
30 #include <glib.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33
34 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags);
35 void ctf_text_close_trace(struct trace_descriptor *descriptor);
36
37 static
38 rw_dispatch write_dispatch_table[] = {
39 [ CTF_TYPE_INTEGER ] = ctf_text_integer_write,
40 [ CTF_TYPE_FLOAT ] = ctf_text_float_write,
41 [ CTF_TYPE_ENUM ] = ctf_text_enum_write,
42 [ CTF_TYPE_STRING ] = ctf_text_string_write,
43 [ CTF_TYPE_STRUCT ] = ctf_text_struct_write,
44 [ CTF_TYPE_VARIANT ] = ctf_text_variant_write,
45 [ CTF_TYPE_ARRAY ] = ctf_text_array_write,
46 [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write,
47 };
48
49 static
50 struct format ctf_text_format = {
51 .open_trace = ctf_text_open_trace,
52 .close_trace = ctf_text_close_trace,
53 };
54
55 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags)
56 {
57 struct ctf_text_stream_pos *pos;
58 FILE *fp;
59
60 pos = g_new0(struct ctf_text_stream_pos, 1);
61
62 switch (flags & O_ACCMODE) {
63 case O_RDWR:
64 if (!path)
65 fp = stdout;
66 else
67 fp = fopen(path, "w");
68 if (!fp)
69 goto error;
70 pos->fp = fp;
71 pos->parent.rw_table = write_dispatch_table;
72 break;
73 case O_RDONLY:
74 default:
75 fprintf(stdout, "[error] Incorrect open flags.\n");
76 goto error;
77 }
78
79 return &pos->trace_descriptor;
80 error:
81 g_free(pos);
82 return NULL;
83 }
84
85 void ctf_text_close_trace(struct trace_descriptor *td)
86 {
87 struct ctf_text_stream_pos *pos =
88 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
89 fclose(pos->fp);
90 g_free(pos);
91 }
92
93 void __attribute__((constructor)) ctf_text_init(void)
94 {
95 int ret;
96
97 ctf_text_format.name = g_quark_from_static_string("text");
98 ret = bt_register_format(&ctf_text_format);
99 assert(!ret);
100 }
101
102 /* TODO: finalize */
This page took 0.032557 seconds and 5 git commands to generate.