3 Copyright 2011 David Robillard <http://drobilla.net>
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.
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.
26 #define CHUNK_ID_LEN 4
28 static const char FILE_TYPE[CHUNK_ID_LEN] = "RDFF"; /* RDFF File ID */
29 static const char CHUNK_TRIP[CHUNK_ID_LEN] = "trip"; /* Triple Chunk ID */
30 static const char CHUNK_URID[CHUNK_ID_LEN] = "urid"; /* URI-ID Chunk ID*/
39 rdff_open(const char* path, bool write)
41 FILE* fd = fopen(path, (write ? "w" : "r"));
43 fprintf(stderr, "%s\n", strerror(errno));
50 fwrite("RIFF", CHUNK_ID_LEN, 1, fd); /* RIFF chunk ID */
51 fwrite(&size, sizeof(size), 1, fd); /* RIFF chunk size */
52 fwrite(FILE_TYPE, CHUNK_ID_LEN, 1, fd); /* File type */
54 char magic[CHUNK_ID_LEN];
55 if (fread(magic, CHUNK_ID_LEN, 1, fd) != 1
56 || strncmp(magic, "RIFF", CHUNK_ID_LEN)) {
58 fprintf(stderr, "%s: error: not a RIFF file\n", path);
62 if (fread(&size, sizeof(size), 1, fd) != 1) {
64 fprintf(stderr, "%s: error: missing RIFF chunk size\n", path);
68 if (fread(magic, CHUNK_ID_LEN, 1, fd) != 1
69 || strncmp(magic, FILE_TYPE, CHUNK_ID_LEN)) {
71 fprintf(stderr, "%s: error: not an %s RIFF file\n",
77 fcntl(fileno(fd), F_SETFD, fcntl(fileno(fd), F_GETFD) | FD_CLOEXEC);
79 RDFF ret = (RDFF)malloc(sizeof(struct _RDFF));
86 #define WRITE(ptr, size, nmemb, stream) \
87 if (fwrite(ptr, size, nmemb, stream) != nmemb) { \
88 return RDFF_STATUS_UNKNOWN_ERROR; \
92 rdff_write_uri(RDFF file,
97 const uint32_t chunk_size = sizeof(id) + len + 1;
98 WRITE(CHUNK_URID, CHUNK_ID_LEN, 1, file->fd);
99 WRITE(&chunk_size, sizeof(chunk_size), 1, file->fd);
100 WRITE(&id, sizeof(id), 1, file->fd);
101 WRITE(uri, len + 1, 1, file->fd);
102 if ((chunk_size % 2)) {
103 WRITE("", 1, 1, file->fd); /* pad */
105 file->size += 8 + chunk_size;
106 return RDFF_STATUS_OK;
110 rdff_write_triple(RDFF file,
113 uint32_t object_type,
114 uint32_t object_size,
117 const uint32_t chunk_size = sizeof(RDFFTripleChunk) + object_size;
118 WRITE(CHUNK_TRIP, CHUNK_ID_LEN, 1, file->fd);
119 WRITE(&chunk_size, sizeof(chunk_size), 1, file->fd);
120 WRITE(&subject, sizeof(subject), 1, file->fd);
121 WRITE(&predicate, sizeof(predicate), 1, file->fd);
122 WRITE(&object_type, sizeof(object_type), 1, file->fd);
123 WRITE(&object_size, sizeof(object_size), 1, file->fd);
124 WRITE(object, object_size, 1, file->fd);
125 if ((object_size % 2)) {
126 WRITE("", 1, 1, file->fd); /* write pad */
128 file->size += 8 + chunk_size;
129 return RDFF_STATUS_OK;
133 rdff_read_chunk(RDFF file,
137 return RDFF_STATUS_EOF;
139 #define READ(ptr, size, nmemb, stream) \
140 if (fread(ptr, size, nmemb, stream) != nmemb) { \
141 return RDFF_STATUS_CORRUPT; \
144 const uint32_t alloc_size = (*buf)->size;
146 READ((*buf)->type, sizeof((*buf)->type), 1, file->fd);
147 READ(&(*buf)->size, sizeof((*buf)->size), 1, file->fd);
148 if ((*buf)->size > alloc_size) {
149 *buf = realloc(*buf, sizeof(RDFFChunk) + (*buf)->size);
151 READ((*buf)->data, (*buf)->size, 1, file->fd);
152 if (((*buf)->size % 2)) {
154 READ(&pad, 1, 1, file->fd); /* skip pad */
156 return RDFF_STATUS_OK;
160 rdff_chunk_is_uri(RDFFChunk* chunk)
163 return !strncmp(chunk->type, CHUNK_URID, CHUNK_ID_LEN);
167 rdff_chunk_is_triple(RDFFChunk* chunk)
169 return !strncmp(chunk->type, CHUNK_TRIP, CHUNK_ID_LEN);
173 rdff_close(RDFF file)
177 fseek(file->fd, 4, SEEK_SET);
178 if (fwrite(&file->size, sizeof(file->size), 1, file->fd) != 1) {
179 fprintf(stderr, "failed to write RIFF header size\n");
191 main(int argc, char** argv)
194 fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
198 const char* const filename = argv[1];
200 RDFF file = rdff_open(filename, true);
204 static const int N_URIS = 16;
205 static const int N_RECORDS = 16;
208 for (int i = 0; i < N_URIS; ++i) {
209 snprintf(uri, sizeof(uri), "http://example.org/uri%02d", i + 1);
210 rdff_write_uri(file, i + 1, strlen(uri), uri);
214 for (int i = 0; i < N_RECORDS; ++i) {
215 snprintf(val, sizeof(val), "VAL%02d", i);
216 rdff_write_triple(file,
226 file = rdff_open(filename, false);
230 RDFFChunk* chunk = malloc(sizeof(RDFFChunk));
232 for (int i = 0; i < N_URIS; ++i) {
233 if (rdff_read_chunk(file, &chunk)
234 || strncmp(chunk->type, CHUNK_URID, 4)) {
235 fprintf(stderr, "error: expected %s chunk\n", CHUNK_URID);
238 RDFFURIChunk* body = (RDFFURIChunk*)chunk->data;
239 printf("URI: %s\n", body->uri);
242 for (int i = 0; i < N_RECORDS; ++i) {
243 if (rdff_read_chunk(file, &chunk)
244 || strncmp(chunk->type, CHUNK_TRIP, 4)) {
245 fprintf(stderr, "error: expected %s chunk\n", CHUNK_TRIP);
248 RDFFTripleChunk* body = (RDFFTripleChunk*)chunk->data;
249 printf("KEY %d = %s\n", body->predicate, body->object);
259 fprintf(stderr, "Test failed\n");