aboutsummaryrefslogtreecommitdiff
path: root/src/editem.c
blob: 25e3583596670148d96636f92c39e4277859f3a7 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* This file is part of Idest.
   Copyright (C) 2009-2011, 2017 Sergey Poznyakoff
 
   Idest 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 3, or (at your option)
   any later version.
 
   Idest 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 Idest.  If not, see <http://www.gnu.org/licenses/>. */

#include "idest.h"

void
ed_item_zero(struct ed_item *item)
{
	memset(item, 0, sizeof(*item));
}

void
ed_item_free_content(struct ed_item *item)
{
	qv_free(item->qc, item->qv);
	free(item->value);
	free(item->name);
}

void
ed_item_free(struct ed_item *item)
{
	qv_free(item->qc, item->qv);
	free(item->value);
	free(item->name);
	free(item);
}

#define ISWILD(s) ((s)[0] == '*' && (s)[1] == 0)

int
ed_item_qv_cmp(struct ed_item const *a, struct ed_item const *b)
{
	int i, count;

	count = a->qc < b->qc ? a->qc : b->qc;
	for (i = 0; i < count; i++) {
		if (!a->qv[i] || !b->qv[i] ||
		    ISWILD(a->qv[i]) || ISWILD(b->qv[i]))
			continue;
		if (strcmp(a->qv[i], b->qv[i]))
			return 1;
	}
	return 0;
}

struct ed_item *
ed_item_create0(char *name, const char *id)
{
	struct ed_item *itm = xzalloc(sizeof(itm[0]));
	itm->name = name;
	strncpy(itm->id, id, sizeof(itm->id));
	return itm;
}

struct ed_item *
ed_item_create(const char *name, const char *id)
{
	return ed_item_create0(xstrdup(name), id);
}

struct ed_item *
ed_item_dup(struct ed_item const *orig)
{
	int i;
	struct ed_item *copy;
	
	copy = ed_item_create(orig->name, orig->id);
	copy->qc = orig->qc;
	copy->qv = xcalloc(orig->qc, sizeof(orig->qv[0]));

	for (i = 0; i < orig->qc; i++)
		copy->qv[i] = orig->qv[i] ? xstrdup(orig->qv[i]) : NULL;
	copy->value = orig->value ? xstrdup(orig->value) : NULL;
	return copy;
}

const char *
str_split_col(const char *str, size_t *plen, size_t *pn)
{
	size_t n;
	size_t len = *plen;
	
	for (n = 0; len; len--, n++, str++) {
		if (*str == ':') {
			*pn = n;
			*plen = len - 1;
			return str + 1;
		}
	}
	*pn = n;
	*plen = len;
	return NULL;
}

static int
split_item_name(struct ed_item *itm, const char *arg, size_t len)
{
	const char *p;
	size_t n;

	p = str_split_col(arg, &len, &n);
	itm->name = xmalloc(n + 1);
	memcpy(itm->name, arg, n);
	itm->name[n] = 0;

	if (p) {
		size_t qc;
		int i;
		
		/* count qualifier elements */
		for (qc = 1, i = 0; i < len; i++)
			if (p[i] == ':')
				qc++;

		itm->qc = qc;
		itm->qv = xcalloc(qc, sizeof(itm->qv[0]));

		for (i = 0; len && i < qc; i++) {
			char *s;

			arg = p;
			p = str_split_col(arg, &len, &n);
			if (n == 0)
				s = NULL;
			else {
				s = xmalloc(n + 1);
				memcpy(s, arg, n);
				s[n] = 0;
			}
			itm->qv[i] = s;
		}
	} else {
		itm->qc = 0;
		itm->qv = NULL;
	}
	return 0;
}

struct ed_item *
ed_item_from_frame_spec(const char *arg, size_t len)
{
	struct ed_item itm;
	struct id3_frametype const *frametype;
	struct idest_frametab const *ft;
	size_t n;
	const char *id;
	char *idstr;
	
	ed_item_zero(&itm);
	if (split_item_name(&itm, arg, len))
		error(1, 0, "ivalid frame spec: %s", arg);

	n = strcspn(itm.name, "%");
	if (itm.name[n]) {
		char *s = xmalloc(n + 1);
		memcpy(s, itm.name, n);
		s[n] = 0;

		idstr = xstrdup(itm.name + n + 1);
		free(itm.name);
		itm.name = s;
	} else
		idstr = itm.name;
	
	id = name_to_frame_id(idstr);
	if (!id) {
		frametype = id3_frametype_lookup(idstr, strlen(idstr));
		if (!frametype) 
			error(1, 0, "%s: unknown frame name", idstr);
		id = idstr;
	} else {
		frametype = id3_frametype_lookup(id, 4);
		assert(frametype != NULL);
	}

	ft = idest_frame_lookup(id);
	if (!ft)
		error(1, 0, "don't know how to handle frame %s", id);

	memcpy(itm.id, id, sizeof(itm.id));

	if (idstr != itm.name)
		free(idstr);
	
	if (itm.qc > ft->qc)
		error(1, 0,
		      "too many field qualifiers for this frame: %s", arg);
	
	if (describe_option) {
		free(itm.name);
		itm.name = xstrdup(frametype->description);
	}

	return ed_item_dup(&itm);
}

void
ed_item_print(struct ed_item const *itm)
{
	printf("%s", itm->name);
	if (itm->qc) {
		int i;

		for (i = 0; i < itm->qc; i++)
			printf(":%s", itm->qv[i] ? itm->qv[i] : "");
	}
	printf(": %s\n", itm->value ? itm->value : "");
}

static bool
ed_item_eq(const void *elt1, const void *elt2)
{
	const struct ed_item *i1 = elt1;
	const struct ed_item *i2 = elt2;
	return strcmp(i1->id, i2->id) == 0;
}

static void
ed_item_dispose(const void *elt)
{
	struct ed_item *itm = (struct ed_item *) elt;
	ed_item_free(itm);
}

gl_list_t 
ed_list_create()
{
	return gl_list_create_empty(&gl_linked_list_implementation,
				    ed_item_eq,
				    NULL,
				    ed_item_dispose,
				    false);
}

struct ed_item const *
ed_list_locate(gl_list_t list, struct id3_frame *frame, idest_frame_cmp_t cmp)
{
	gl_list_iterator_t itr;
	const void *p;
	const struct ed_item *item = NULL;
	
	itr = gl_list_iterator(list);
	while (gl_list_iterator_next(&itr, &p, NULL)) {
		item = p;
		if (memcmp(item->id, frame->id, 4) == 0 &&
		    (!cmp || item->qc == 0 || cmp(frame, item) == 0))
			break;
		item = NULL;
	}
	gl_list_iterator_free(&itr);
	return item;
}










Return to:

Send suggestions and report system problems to the System administrator.