Fix warning.
[ardour.git] / libs / ardour / lv2_pfile.c
1 /* Portable file-based implementation of LV2 Persist.
2  * See <http://lv2plug.in/ns/ext/persist> for details.
3  * Copyright (C) 2010 David Robillard <http://drobilla.net>
4  *
5  * This file is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This file is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this file; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <assert.h>
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "lv2_pfile.h"
27
28 struct _LV2PFile {
29         FILE* fd;
30 };
31
32 LV2PFile
33 lv2_pfile_open(const char* path, bool write)
34 {
35         FILE* fd = fopen(path, (write ? "w" : "r"));
36         if (!fd) {
37                 fprintf(stderr, "%s", strerror(errno));
38                 return NULL;
39         }
40
41         static const char* const magic     = "LV2PFILE";
42         static const size_t      magic_len = 8;
43         if (write) {
44                 fwrite(magic, magic_len, 1, fd);
45         } else {
46                 char file_magic[magic_len];
47                 if (fread(file_magic, magic_len, 1, fd) != 1
48                     || strncmp(file_magic, magic, magic_len)) {
49                         fclose(fd);
50                         return NULL;
51                 }
52         }
53
54         LV2PFile ret = (LV2PFile)malloc(sizeof(LV2PFile));
55         ret->fd = fd;
56         return ret;
57 }
58
59 LV2PFileStatus
60 lv2_pfile_write(LV2PFile    file,
61                 const char* key,
62                 const void* value,
63                 uint64_t    size,
64                 const char* type)
65 {
66 #define WRITE(ptr, size, nmemb, stream) \
67         if (fwrite(ptr, size, nmemb, stream) != nmemb) { \
68                 return LV2_PFILE_UNKNOWN_ERROR; \
69         }
70
71         const uint32_t key_len = strlen(key);
72         WRITE(&key_len, sizeof(key_len), 1, file->fd);
73         WRITE(key, key_len + 1, 1, file->fd);
74
75         const uint32_t type_len = strlen(type);
76         WRITE(&type_len, sizeof(type_len), 1, file->fd);
77         WRITE(type, type_len + 1, 1, file->fd);
78
79         WRITE(&size, sizeof(size), 1, file->fd);
80         WRITE(value, size, 1, file->fd);
81
82         return LV2_PFILE_OK;
83 }
84
85 LV2PFileStatus
86 lv2_pfile_read(LV2PFile  file,
87                char**    key,
88                uint32_t* key_len,
89                char**    type,
90                uint32_t* type_len,
91                void**    value,
92                uint64_t* size)
93 {
94         if (feof(file->fd))
95                 return LV2_PFILE_EOF;
96
97 #define READ(ptr, size, nmemb, stream) \
98         if (fread(ptr, size, nmemb, stream) != nmemb) { \
99                 assert(false); \
100                 return LV2_PFILE_CORRUPT; \
101         }
102
103         READ(key_len, sizeof(*key_len), 1, file->fd);
104         *key = (char*)malloc(*key_len + 1);
105         READ(*key, *key_len + 1, 1, file->fd);
106
107         READ(type_len, sizeof(*type_len), 1, file->fd);
108         *type = (char*)malloc(*type_len + 1);
109         READ(*type, *type_len + 1, 1, file->fd);
110
111         READ(size, sizeof(*size), 1, file->fd);
112         *value = malloc(*size);
113         READ(*value, *size, 1, file->fd);
114
115         return LV2_PFILE_OK;
116 }
117
118 void
119 lv2_pfile_close(LV2PFile file)
120 {
121         if (file)
122                 fclose(file->fd);
123
124         free(file);
125 }
126
127 #ifdef STANDALONE
128 // Test program
129 int
130 main(int argc, char** argv)
131 {
132         if (argc != 2) {
133                 fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
134                 return 1;
135         }
136
137         const char* const filename = argv[1];
138
139         LV2PFile file = lv2_pfile_open(filename, true);
140         if (!file)
141                 goto fail;
142
143         char wkey[6];
144         char wval[6];
145         const char* wtype = "http://example.org/type";
146 #define NUM_RECORDS 32
147         for (int i = 0; i < NUM_RECORDS; ++i) {
148                 snprintf(wkey, sizeof(wkey), "KEY%02d", i);
149                 snprintf(wval, sizeof(wval), "VAL%02d", i);
150                 lv2_pfile_write(file, wkey, wval, strlen(wval) + 1, wtype);
151         }
152
153         lv2_pfile_close(file);
154
155         file = lv2_pfile_open(filename, false);
156         if (!file)
157                 goto fail;
158
159         char*    rkey;
160         uint32_t rkey_len;
161         char*    rtype;
162         uint32_t rtype_len;
163         uint64_t rsize;
164         void*    rval;
165         for (int i = 0; i < NUM_RECORDS; ++i) {
166                 if (lv2_pfile_read(file, &rkey, &rkey_len, &rtype, &rtype_len, &rval, &rsize))
167                         goto fail;
168
169                 printf("%s = %s :: %s\n", rkey, (char*)rval, rtype);
170                 free(rkey);
171                 free(rtype);
172                 free(rval);
173         }
174
175         lv2_pfile_close(file);
176         return 0;
177
178 fail:
179         lv2_pfile_close(file);
180         fprintf(stderr, "Test failed\n");
181         return 1;
182 }
183 #endif // STANDALONE