3d52553a4bad434fb57e0d557d1a0637f7af0d15
[ardour.git] / libs / ardour / rdff.h
1 /*
2   RDFF - RDF in RIFF
3   Copyright 2011 David Robillard <http://drobilla.net>
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7
8   1. Redistributions of source code must retain the above copyright notice,
9      this list of conditions and the following disclaimer.
10
11   2. Redistributions in binary form must reproduce the above copyright
12      notice, this list of conditions and the following disclaimer in the
13      documentation and/or other materials provided with the distribution.
14
15   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18   AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19   OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24   THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #ifndef RDFF_RDFF_H
28 #define RDFF_RDFF_H
29
30 #include <stdbool.h>
31 #include <stdint.h>
32
33 #ifdef __GNUC__
34 #    define PACKED __attribute__((__packed__))
35 #else
36 #    define PACKED
37 #endif
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /**
44    RDFF file or stream.
45  */
46 typedef struct _RDFF* RDFF;
47
48 /**
49    Status codes for function returns.
50 */
51 typedef enum {
52         RDFF_STATUS_OK            = 0,  /**< Success. */
53         RDFF_STATUS_UNKNOWN_ERROR = 1,  /**< Unknown error. */
54         RDFF_STATUS_EOF           = 2,  /**< End of file. */
55         RDFF_STATUS_CORRUPT       = 3   /**< Corrupt data. */
56 } RDFFStatus;
57
58 /**
59    Generic RIFF chunk header.
60 */
61 typedef struct {
62         char     type[4];  /**< Chunk type ID. */
63         uint32_t size;     /**< Size of chunk body (not including header). */
64         char     data[];   /**< Chunk body. */
65 } PACKED RDFFChunk;
66
67 /**
68    Body of a RDFF "urid" chunk.
69 */
70 typedef struct {
71         uint32_t id;     /**< Numeric ID of URI in this RDFF. */
72         char     uri[];  /**< URI string. */
73 } PACKED RDFFURIChunk;
74
75 /**
76    Body of a RDFF "trip" chunk.
77 */
78 typedef struct {
79         uint32_t subject;      /**< Subject URI ID. */
80         uint32_t predicate;    /**< Predicate URI ID. */
81         uint32_t object_type;  /**< Object type URI ID. */
82         uint32_t object_size;  /**< Size of object data. */
83         char     object[];     /**< Object data. */
84 } PACKED RDFFTripleChunk;
85
86 /**
87    Open/Create a new RDFF file.
88 */
89 RDFF
90 rdff_open(const char* path, bool write);
91
92 /**
93    Write a URI ID to @a file.
94 */
95 RDFFStatus
96 rdff_write_uri(RDFF        file,
97                uint32_t    id,
98                uint32_t    len,
99                const char* uri);
100
101 /**
102    Write a key/value record to @a file.
103 */
104 RDFFStatus
105 rdff_write_triple(RDFF        file,
106                   uint32_t    subject,
107                   uint32_t    predicate,
108                   uint32_t    object_type,
109                   uint32_t    object_size,
110                   const void* object);
111
112 /**
113    Read a chunk from @a file.
114
115    @param buf MUST point to an RDFFChunk dynamically allocated with malloc.
116    The @a size field (i.e. (*buf)->size) MUST be set to the amount of available
117    memory in the chunk (not including the header). If this is insufficient,
118    *buf will be resized using realloc.
119 */
120 RDFFStatus
121 rdff_read_chunk(RDFF        file,
122                 RDFFChunk** buf);
123
124 /**
125    Close @a file.
126    After this call, @a file is invalid.
127 */
128 void
129 rdff_close(RDFF file);
130
131 #ifdef __cplusplus
132 } /* extern "C" */
133 #endif
134
135 #endif /* RDFF_RDFF_H */