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>
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)
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
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.
26 #include "lv2_pfile.h"
33 lv2_pfile_open(const char* path, bool write)
35 FILE* fd = fopen(path, (write ? "w" : "r"));
37 fprintf(stderr, "%s", strerror(errno));
41 static const char* const magic = "LV2PFILE";
42 static const size_t magic_len = 8;
44 fwrite(magic, magic_len, 1, fd);
46 char file_magic[magic_len];
47 if (fread(file_magic, magic_len, 1, fd) != 1
48 || strncmp(file_magic, magic, magic_len)) {
54 LV2PFile ret = (LV2PFile)malloc(sizeof(LV2PFile));
60 lv2_pfile_write(LV2PFile file,
66 #define WRITE(ptr, size, nmemb, stream) \
67 if (fwrite(ptr, size, nmemb, stream) != nmemb) { \
68 return LV2_PFILE_UNKNOWN_ERROR; \
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);
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);
79 WRITE(&size, sizeof(size), 1, file->fd);
80 WRITE(value, size, 1, file->fd);
86 lv2_pfile_read(LV2PFile file,
97 #define READ(ptr, size, nmemb, stream) \
98 if (fread(ptr, size, nmemb, stream) != nmemb) { \
100 return LV2_PFILE_CORRUPT; \
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);
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);
111 READ(size, sizeof(*size), 1, file->fd);
112 *value = malloc(*size);
113 READ(*value, *size, 1, file->fd);
119 lv2_pfile_close(LV2PFile file)
130 main(int argc, char** argv)
133 fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
137 const char* const filename = argv[1];
139 LV2PFile file = lv2_pfile_open(filename, true);
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);
153 lv2_pfile_close(file);
155 file = lv2_pfile_open(filename, false);
165 for (int i = 0; i < NUM_RECORDS; ++i) {
166 if (lv2_pfile_read(file, &rkey, &rkey_len, &rtype, &rtype_len, &rval, &rsize))
169 printf("%s = %s :: %s\n", rkey, (char*)rval, rtype);
175 lv2_pfile_close(file);
179 lv2_pfile_close(file);
180 fprintf(stderr, "Test failed\n");