aboutsummaryrefslogtreecommitdiff
path: root/src/idop.c
blob: fe9c6f5e63e993681215c81da7483635e274ebf4 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* This file is part of Idest.
   Copyright (C) 2009-2011 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"
#include <file.h>
#include <signal.h>

gl_list_t input_list, output_list;

void
input_list_add_assignment(const char *name, const char *value)
{
	struct ed_item *itm;

	if (!input_list)
		input_list = ed_list_create();
	itm = ed_item_from_frame_spec(name, strlen(name));
	itm->value = xstrdup(value);
	gl_list_add_last(input_list, itm);
}

struct ed_item const *
input_list_locate(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(input_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;
}

void
parse_ed_items(const char *arg)
{
	if (!input_list)
		input_list = ed_list_create();
	while (*arg) {
		struct ed_item *itm;
 		size_t len = strcspn(arg, " \t,");

		itm = ed_item_from_frame_spec(arg, len);
		gl_list_add_last(input_list, itm);
		arg += len;
		if (!*arg)
			break;
		arg += strspn(arg, " \t,");
	}
}

void
input_list_init()
{
	if (all_frames)
		input_list = NULL;
	else if (!input_list)
		parse_ed_items(DEFAULT_ED_LIST);
}	

void
output_list_append(struct ed_item const *item, struct ed_item const *ref)
{
	struct ed_item *elt = ed_item_dup(item);
	if (!output_list)
		output_list = ed_list_create();
	elt->ref = ref;
	gl_list_add_last(output_list, elt);
}

void
output_list_print()
{
	gl_list_iterator_t itr;
	const void *p;

	if (all_frames) {
		itr = gl_list_iterator(output_list);
		while (gl_list_iterator_next(&itr, &p, NULL)) {
			const struct ed_item *item = p;
			ed_item_print(item);
		}
		gl_list_iterator_free(&itr);
	} else {
		itr = gl_list_iterator(input_list);
		while (gl_list_iterator_next(&itr, &p, NULL)) {
			int printed = 0;
			const struct ed_item *input_item = p;
			if (output_list) {
				gl_list_iterator_t oitr;
				oitr = gl_list_iterator(output_list);
				while (gl_list_iterator_next(&oitr, &p, NULL)) {
					const struct ed_item *output_item = p;
					if (output_item->ref == input_item) {
						ed_item_print(output_item);
						printed = 1;
					}
				}
				gl_list_iterator_free(&oitr);
			}
			if (!printed)
				ed_item_print(input_item);
		}
		gl_list_iterator_free(&itr);
	}
}

void
output_list_free()
{
	if (output_list) {
		gl_list_free(output_list);
		output_list = NULL;
	}
}

void
safe_id3_file_update_and_close(struct id3_file *file)
{
        sigset_t set, oldset;

        sigemptyset(&set);
        sigaddset(&set, SIGINT);
        sigaddset(&set, SIGTERM);

        sigprocmask(SIG_BLOCK, &set, &oldset);
        id3_file_update(file);
        id3_file_close(file);
        sigprocmask(SIG_SETMASK, &oldset, NULL);
}

char *
idest_ucs4_cvt(id3_ucs4_t const *ucs4)
{
	if (latin1_option) 
		return (char*)id3_ucs4_latin1duplicate(ucs4);
	else
		return (char*)id3_ucs4_utf8duplicate(ucs4);
}

static void
add_stringlist(gl_list_t list, union id3_field *field, int isgenre,
	       size_t *psize)
{
	unsigned i, nstrings = id3_field_getnstrings(field);
	size_t size = 0;
	
	for (i = 0; i < nstrings; i++) {
		id3_ucs4_t const *ucs4;
		char *str;
		
		ucs4 = id3_field_getstrings(field, i);
		if (!ucs4)
			continue;
		if (isgenre)
			ucs4 = id3_genre_name(ucs4);
		str = idest_ucs4_cvt(ucs4);
		size += strlen(str);
		gl_list_add_last(list, str);
	}
	if (psize)
		*psize = size;
}

char *
field_to_string(union id3_field *field, int isgenre)
{
	id3_ucs4_t const *ucs4;
	char *ret = NULL;
	char buf[128];
	
	switch (id3_field_type(field)) {
	case ID3_FIELD_TYPE_TEXTENCODING:
	case ID3_FIELD_TYPE_INT8:
	case ID3_FIELD_TYPE_INT16:
	case ID3_FIELD_TYPE_INT24:
	case ID3_FIELD_TYPE_INT32:
		snprintf(buf, sizeof(buf), "%ld", field->number.value);
		ret = xstrdup(buf);
		break;
		
	case ID3_FIELD_TYPE_LATIN1:
	case ID3_FIELD_TYPE_LATIN1FULL:
		/* FIXME */
		ret = xstrdup(id3_field_getlatin1(field));
		break;
		
	case ID3_FIELD_TYPE_LATIN1LIST:
		/* FIXME */
		break;
		
	case ID3_FIELD_TYPE_STRING:
		ucs4 = id3_field_getstring(field);;
		if (ucs4)
			ret = idest_ucs4_cvt(ucs4);
		break;
		
	case ID3_FIELD_TYPE_STRINGFULL:
		ucs4 = id3_field_getfullstring(field);
		ret = idest_ucs4_cvt(ucs4);
		break;

	case ID3_FIELD_TYPE_STRINGLIST: {
		gl_list_t list;
		size_t sz;
		gl_list_iterator_t itr;
		const void *p;
		
		list = new_string_list(true);
		add_stringlist(list, field, isgenre, &sz);
		ret = xmalloc(sz + 1);
		ret[0] = 0;
		itr = gl_list_iterator(list);
		while (gl_list_iterator_next(&itr, &p, NULL))
			strcat(ret, p);
		gl_list_iterator_free(&itr);
		gl_list_free(list);
		break;
	}
		
	case ID3_FIELD_TYPE_LANGUAGE:
	case ID3_FIELD_TYPE_FRAMEID:
	case ID3_FIELD_TYPE_DATE:
		ret = xstrdup(field->immediate.value);
		break;
		
	case ID3_FIELD_TYPE_BINARYDATA:
		/* FIXME */
		break;

	case ID3_FIELD_TYPE_INT32PLUS:
		/* FIXME */
		break;
	}
	return ret;
}

int
set_frame_value(struct id3_frame *frame, const struct ed_item *item)
{
	const struct idest_frametab *ft = idest_frame_lookup(frame->id);
	if (!ft)
		return IDEST_ERR_BADTYPE;
	return ft->encode(frame, item);
}

static int
build_tag_options(struct id3_tag *tag, unsigned long location,
		  id3_length_t length, void *data)
{
	unsigned int ver = id3_tag_version(tag);

	if (ID3_TAG_VERSION_MAJOR(ver) == 1)
		*(unsigned int*)data |= IDEST_ID3V_1;
	else
		*(unsigned int*)data |= IDEST_ID3V_2;
	return 0;
}

struct id3_frame *
find_matching_frame(struct id3_tag *tag, const struct ed_item *item,
		    idest_frame_cmp_t cmp)
{
	struct id3_frame *frame;
	
	if (item->qc == 0 || cmp == NULL)
		frame = id3_tag_findframe(tag, item->id, 0);
	else {
		int i;
		for (i = 0; frame = id3_tag_findframe(tag, item->id, i); i++) {
			if (cmp(frame, item) == 0)
				break;
		}
	}
	return frame;
}


static int
update_frame(struct id3_tag *tag, const struct ed_item *item)
{
	const struct idest_frametab *ft = idest_frame_lookup(item->id);
	struct id3_frame *frame;

	if (!ft)
		return IDEST_ERR_BADTYPE;

	frame = find_matching_frame(tag, item, ft->cmp);
	if (!frame) {
		frame = id3_frame_new(item->id);
		if (id3_tag_attachframe(tag, frame))
			error(1, 0, "cannot attach new frame");
	}
	return ft->encode(frame, item);
}

void
set_tags(const char *name)
{
	struct id3_file *file;
	struct id3_tag *tag;
	int modified = 0;
	int vopt = version_option;
		
	file = id3_file_open(name, ID3_FILE_MODE_READWRITE);
	if (!file)
		error(1, errno, "cannot open file %s", name);
	tag = id3_file_tag(file);
	if (!tag) 
		abort(); /* FIXME */

	if (input_list) {
		gl_list_iterator_t itr;
		const void *p;
		
		itr = gl_list_iterator(input_list);
		while (gl_list_iterator_next(&itr, &p, NULL)) {
			const struct ed_item *item = p;
			int rc = update_frame(tag, item);
			if (rc)
				error(1, 0,
				      "cannot set frame %s: %s",
				      item->id, idest_strerror(rc));
			modified |= 1;
		}
		gl_list_iterator_free(&itr);
	}

	/* FIXME */
	modified |= guile_transform(name, tag);
	
	if (convert_version == 0) {
		if (modified && !vopt) {
			if (id3_file_struct_ntags(file)) {
				id3_file_struct_iterate(file,
							build_tag_options,
							&vopt);
			} else 
				vopt = default_version_option;
		}
	} else if (id3_file_struct_ntags(file)) {
		id3_file_struct_iterate(file, build_tag_options, &vopt);
		if (vopt != convert_version) {
			vopt = convert_version;
			modified |= 1;
		}
	}

	if (modified && vopt) {
		int opts = 0;

		if (vopt & IDEST_ID3V_1)
			opts |= ID3_TAG_OPTION_ID3V1;
		if (!(vopt & IDEST_ID3V_2))
			opts |= ID3_TAG_OPTION_NO_ID3V2;
		id3_tag_options(tag,
				ID3_TAG_OPTION_ID3V1|ID3_TAG_OPTION_NO_ID3V2,
				opts);
	}
	if (modified)
		safe_id3_file_update_and_close(file);
	else
		id3_file_close(file);
}

void
del_tags(const char *name)
{
	struct id3_file *file;
	struct id3_tag *tag;
	
	file = id3_file_open(name, ID3_FILE_MODE_READWRITE);
	if (!file)
		error(1, errno, "cannot open file %s", name);
	tag = id3_file_tag(file);
	if (!tag) 
		abort(); /* FIXME */
	if (input_list) {
		int i;
		struct id3_frame *frame;
		
		for (i = 0; (frame = id3_tag_findframe(tag, NULL, i)); ) {
			const struct idest_frametab *ft =
				idest_frame_lookup(frame->id);
			if (input_list_locate(frame, ft ? ft->cmp : NULL)) {
				id3_tag_detachframe(tag, frame);
				id3_frame_delete(frame);
			} else
				i++;
		}
	} else
		id3_tag_clearframes(tag);
	safe_id3_file_update_and_close(file);
}

static void
show_tags(struct id3_tag *tag)
{
	struct id3_frame *frame;
	unsigned i;

	for (i = 0; (frame = id3_tag_findframe(tag, NULL, i)); i++) {
		const struct idest_frametab *ft =
			idest_frame_lookup(frame->id);
		if (!ft) {
			if (verbose_option)
				error(0, 0,
				      "%s: unsupported frame", frame->id);
			continue;
		}

		if (all_frames) {
			struct ed_item outitm;
			ed_item_zero(&outitm);
			if (describe_option) {
				struct id3_frametype const *frametype;
				frametype = id3_frametype_lookup(frame->id,
								 4);
				outitm.name = xstrdup(frametype->description);
			} else
				outitm.name = xstrdup(frame->id);
			memcpy(outitm.id, frame->id, 4);
			if (ft->decode(&outitm, frame)) {
				error(0, 0, "%s: decoding failed", frame->id);
				continue;
			}
			output_list_append(&outitm, NULL);
			ed_item_free_content(&outitm);
		} else {
			struct ed_item const *ref;
			
			ref = input_list_locate(frame, ft->cmp);
			if (ref) {
				struct ed_item outitm;
				ed_item_zero(&outitm);
				outitm.name = xstrdup(ref->name);
				memcpy(outitm.id, ref->id, 4);
				if (ft->decode(&outitm, frame)) {
					error(0, 0,
					      "%s: decoding failed", frame->id);
					continue;
				}
				output_list_append(&outitm, ref);
				ed_item_free_content(&outitm);
			}
		}
	}
	output_list_print();
	output_list_free();
}

void
query_tags(const char *name)
{
	struct id3_file *file;
	struct id3_tag *tag;
	
	input_list_init();	
	
	file = id3_file_open(name, ID3_FILE_MODE_READONLY);
	if (!file)
		error(1, errno, "cannot open file %s", name);

	tag = id3_file_tag(file);
	if (tag) {
		if (guile_list(name, tag) == 0)
			show_tags(tag);
	}
	
	id3_file_close(file);
}

static int
prinfo(struct id3_tag *tag, unsigned long location,
       id3_length_t length, void *data)
{
	unsigned int ver = id3_tag_version(tag);
	unsigned int major = ID3_TAG_VERSION_MAJOR(ver);
	if (major > 1)
		printf("version: 2.%u.%u\n",
		       major, ID3_TAG_VERSION_MINOR(ver));
	else
		printf("version: %u.%u\n",
		       major, ID3_TAG_VERSION_MINOR(ver));
	printf("offset: %lu\n", location);
	printf("length: %lu\n", length);
	return 0;
}

void
info_id3(const char *name)
{
	struct id3_file *file;
	unsigned long ntags;
	
	file = id3_file_open(name, ID3_FILE_MODE_READONLY);
	if (!file)
		error(1, errno, "cannot open file %s", name);
	ntags = id3_file_struct_ntags(file);
	printf("file: %s\n", name);
	printf("ntags: %lu\n", ntags);
	id3_file_struct_iterate(file, prinfo, NULL);
	id3_file_close(file);
}


Return to:

Send suggestions and report system problems to the System administrator.