File: id3.c

package info (click to toggle)
liblicense 0.8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 6,428 kB
  • ctags: 868
  • sloc: sh: 9,739; ansic: 8,315; makefile: 859; xml: 178; cpp: 111; ruby: 41; python: 39
file content (123 lines) | stat: -rw-r--r-- 2,744 bytes parent folder | download | duplicates (2)
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
/* Creative Commons has made the contents of this file
 * available under a CC-GNU-LGPL license:
 *
 * http://creativecommons.org/licenses/LGPL/2.1/
 *
 * A copy of the full license can be found as part of this
 * distribution in the file COPYING.
 * 
 * You may use the liblicense software in accordance with the
 * terms of that license. You agree that you are solely 
 * responsible for your use of the liblicense software and you
 * represent and warrant to Creative Commons that your use
 * of the liblicense software will comply with the CC-GNU-LGPL.
 *
 * Copyright 2007, Creative Commons, www.creativecommons.org.
 * Copyright 2007, Jason Kivlighn.
 */

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

#include <liblicense.h>

#include <id3.h>

#define MAX_URI_LENGTH 50

static int id3_predicate2field(const char * predicate) {
	if (strcmp(predicate, LL_LICENSE) == 0) {
		return ID3FID_WWWCOPYRIGHT /* wcop */;
	} else if (strcmp(predicate, LL_WEBSTATEMENT) == 0) {
		return ID3FID_WWWAUDIOFILE /* woaf */;
	} else {
		return -LL_E_MODULE_WRITE_FAIL;
	}
}


void id3_init()
{
}

char* id3_read( const char* filename, const ll_uri_t predicate )
{
	int woaf_or_wcop;
	char *buffer = NULL;
	ID3Frame *frame;
	ID3Tag *tag;

	woaf_or_wcop = id3_predicate2field(predicate);
	if (woaf_or_wcop < 0) {
		return NULL;
	}

	tag = ID3Tag_New();
	ID3Tag_Link(tag,filename);

	frame = ID3Tag_FindFrameWithID(tag, woaf_or_wcop);
	if (frame) {
		ID3Field *field = ID3Frame_GetField(frame,ID3FN_URL);

		buffer = malloc(sizeof(char)*(MAX_URI_LENGTH+1));
		ID3Field_GetASCII(field,buffer,MAX_URI_LENGTH);
	}

	ID3Tag_Delete(tag);
	return buffer;
}

int id3_write( const char* filename, const char * predicate, const char* uri )
{
	/* Figure out which frame to use */
	int woaf_or_wcop;
	int err;
	ID3Frame *frame;
	ID3Tag *tag;

	woaf_or_wcop = id3_predicate2field(predicate);
	if (woaf_or_wcop < 0) {
		return -LL_E_MODULE_WRITE_FAIL;
	}

	tag = ID3Tag_New();
	ID3Tag_Link(tag,filename);

	frame = ID3Tag_FindFrameWithID(tag, woaf_or_wcop);

	if ( uri ) {
		ID3Field * field;
		if (!frame) {
			frame = ID3Frame_NewID(woaf_or_wcop);
			ID3Tag_AttachFrame(tag, frame);
		}
	
		field = ID3Frame_GetField(frame, ID3FN_URL);
		ID3Field_SetASCII(field, uri);
	} else {
		if (frame) {
			ID3Tag_RemoveFrame(tag,frame);
		}
	}

	err = !ID3Tag_Update(tag);

	ID3Tag_Delete(tag);
	return err;
}

const char * id3_supported_predicates[] = {LL_LICENSE, LL_WEBSTATEMENT, NULL};
const char * id3_mime_types[] = {"audio/mpeg",
				 NULL};

LL_MODULE_DEFINE("id3.so",
		 "Write licenses within ID3 tags.",
		 "0.1",
		 LL_FEATURES_EMBED,
		 id3_supported_predicates,
		 id3_mime_types,
		 id3_init,
		 id3_read,
		 id3_write,
		 NULL /* no shutdown */);