summaryrefslogtreecommitdiff
path: root/libs/ardour/lv2_pfile.c
blob: f5c1c02a78043dd72afa538adeb9b1ea3df32ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Portable file-based implementation of LV2 Persist.
 * See <http://lv2plug.in/ns/ext/persist> for details.
 * Copyright (C) 2010 David Robillard <http://drobilla.net>
 *
 * This file is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this file; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "lv2_pfile.h"

struct _LV2PFile {
	FILE* fd;
};

LV2PFile
lv2_pfile_open(const char* path, bool write)
{
	FILE* fd = fopen(path, (write ? "w" : "r"));
	if (!fd) {
		fprintf(stderr, "%s\n", strerror(errno));
		return NULL;
	}

	static const char* const magic     = "LV2PFILE";
	static const size_t      magic_len = 8;
	if (write) {
		fwrite(magic, magic_len, 1, fd);
	} else {
		char file_magic[magic_len];
		if (fread(file_magic, magic_len, 1, fd) != 1
		    || strncmp(file_magic, magic, magic_len)) {
			fclose(fd);
			return NULL;
		}
	}

	LV2PFile ret = (LV2PFile)malloc(sizeof(LV2PFile));
	ret->fd = fd;
	return ret;
}

LV2PFileStatus
lv2_pfile_write(LV2PFile    file,
                const char* key,
                const void* value,
                uint64_t    size,
                const char* type)
{
#define WRITE(ptr, size, nmemb, stream) \
	if (fwrite(ptr, size, nmemb, stream) != nmemb) { \
		return LV2_PFILE_UNKNOWN_ERROR; \
	}

	const uint32_t key_len = strlen(key);
	WRITE(&key_len, sizeof(key_len), 1, file->fd);
	WRITE(key, key_len + 1, 1, file->fd);

	const uint32_t type_len = strlen(type);
	WRITE(&type_len, sizeof(type_len), 1, file->fd);
	WRITE(type, type_len + 1, 1, file->fd);

	WRITE(&size, sizeof(size), 1, file->fd);
	WRITE(value, size, 1, file->fd);

	return LV2_PFILE_OK;
}

LV2PFileStatus
lv2_pfile_read(LV2PFile  file,
               char**    key,
               uint32_t* key_len,
               char**    type,
               uint32_t* type_len,
               void**    value,
               uint64_t* size)
{
	if (feof(file->fd))
		return LV2_PFILE_EOF;

#define READ(ptr, size, nmemb, stream) \
	if (fread(ptr, size, nmemb, stream) != nmemb) { \
		assert(false); \
		return LV2_PFILE_CORRUPT; \
	}

	READ(key_len, sizeof(*key_len), 1, file->fd);
	*key = (char*)malloc(*key_len + 1);
	READ(*key, *key_len + 1, 1, file->fd);

	READ(type_len, sizeof(*type_len), 1, file->fd);
	*type = (char*)malloc(*type_len + 1);
	READ(*type, *type_len + 1, 1, file->fd);

	READ(size, sizeof(*size), 1, file->fd);
	*value = malloc(*size);
	READ(*value, *size, 1, file->fd);

	return LV2_PFILE_OK;
}

void
lv2_pfile_close(LV2PFile file)
{
	if (file)
		fclose(file->fd);

	free(file);
}

#ifdef STANDALONE
// Test program
int
main(int argc, char** argv)
{
	if (argc != 2) {
		fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
		return 1;
	}

	const char* const filename = argv[1];

	LV2PFile file = lv2_pfile_open(filename, true);
	if (!file)
		goto fail;

	char wkey[6];
	char wval[6];
	const char* wtype = "http://example.org/type";
#define NUM_RECORDS 32
	for (int i = 0; i < NUM_RECORDS; ++i) {
		snprintf(wkey, sizeof(wkey), "KEY%02d", i);
		snprintf(wval, sizeof(wval), "VAL%02d", i);
		lv2_pfile_write(file, wkey, wval, strlen(wval) + 1, wtype);
	}

	lv2_pfile_close(file);

	file = lv2_pfile_open(filename, false);
	if (!file)
		goto fail;

	char*    rkey;
	uint32_t rkey_len;
	char*    rtype;
	uint32_t rtype_len;
	uint64_t rsize;
	void*    rval;
	for (int i = 0; i < NUM_RECORDS; ++i) {
		if (lv2_pfile_read(file, &rkey, &rkey_len, &rtype, &rtype_len, &rval, &rsize))
			goto fail;

		printf("%s = %s :: %s\n", rkey, (char*)rval, rtype);
		free(rkey);
		free(rtype);
		free(rval);
	}

	lv2_pfile_close(file);
	return 0;

fail:
	lv2_pfile_close(file);
	fprintf(stderr, "Test failed\n");
	return 1;
}
#endif // STANDALONE