fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / rdff.h
1 /*
2   RDFF - RDF in RIFF
3   Copyright 2011 David Robillard <http://drobilla.net>
4
5   Permission to use, copy, modify, and/or distribute this software for any
6   purpose with or without fee is hereby granted, provided that the above
7   copyright notice and this permission notice appear in all copies.
8
9   THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #ifndef RDFF_RDFF_H
19 #define RDFF_RDFF_H
20
21 #include <stdbool.h>
22 #include <stdint.h>
23
24 #ifdef __GNUC__
25 #    define PACKED __attribute__((__packed__))
26 #else
27 #    define PACKED
28 #endif
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 /**
35    RDFF file or stream.
36  */
37 typedef struct _RDFF* RDFF;
38
39 /**
40    Status codes for function returns.
41 */
42 typedef enum {
43         RDFF_STATUS_OK            = 0,  /**< Success. */
44         RDFF_STATUS_UNKNOWN_ERROR = 1,  /**< Unknown error. */
45         RDFF_STATUS_EOF           = 2,  /**< End of file. */
46         RDFF_STATUS_CORRUPT       = 3   /**< Corrupt data. */
47 } RDFFStatus;
48
49 /**
50    Generic RIFF chunk header.
51 */
52 typedef struct {
53         char     type[4];  /**< Chunk type ID. */
54         uint32_t size;     /**< Size of chunk body (not including header). */
55         char     data[];   /**< Chunk body. */
56 } PACKED RDFFChunk;
57
58 /**
59    Body of a RDFF "urid" chunk.
60 */
61 typedef struct {
62         uint32_t id;     /**< Numeric ID of URI in this RDFF. */
63         char     uri[];  /**< URI string. */
64 } PACKED RDFFURIChunk;
65
66 /**
67    Body of a RDFF "trip" chunk.
68 */
69 typedef struct {
70         uint32_t subject;      /**< Subject URI ID. */
71         uint32_t predicate;    /**< Predicate URI ID. */
72         uint32_t object_type;  /**< Object type URI ID. */
73         uint32_t object_size;  /**< Size of object data. */
74         char     object[];     /**< Object data. */
75 } PACKED RDFFTripleChunk;
76
77 /**
78    Open/Create a new RDFF file.
79 */
80 RDFF
81 rdff_open(const char* path, bool write);
82
83 /**
84    Write a URI ID to @a file.
85 */
86 RDFFStatus
87 rdff_write_uri(RDFF        file,
88                uint32_t    id,
89                uint32_t    len,
90                const char* uri);
91
92 /**
93    Write a key/value record to @a file.
94 */
95 RDFFStatus
96 rdff_write_triple(RDFF        file,
97                   uint32_t    subject,
98                   uint32_t    predicate,
99                   uint32_t    object_type,
100                   uint32_t    object_size,
101                   const void* object);
102
103 /**
104    Read a chunk from @a file.
105
106    @param buf MUST point to an RDFFChunk dynamically allocated with malloc.
107    The @a size field (i.e. (*buf)->size) MUST be set to the amount of available
108    memory in the chunk (not including the header). If this is insufficient,
109    *buf will be resized using realloc.
110 */
111 RDFFStatus
112 rdff_read_chunk(RDFF        file,
113                 RDFFChunk** buf);
114
115 /**
116    Return true iff @a chunk is a URI chunk.
117 */
118 bool
119 rdff_chunk_is_uri(RDFFChunk* chunk);
120
121 /**
122    Return true iff @a chunk is a Triple chunk.
123 */
124 bool
125 rdff_chunk_is_triple(RDFFChunk* chunk);
126
127 /**
128    Close @a file.
129    After this call, @a file is invalid.
130 */
131 void
132 rdff_close(RDFF file);
133
134 #ifdef __cplusplus
135 } /* extern "C" */
136 #endif
137
138 #endif /* RDFF_RDFF_H */