~moody/mpl

mpl/index.c -rw-r--r-- 4.2 KiB
7309e0f2 — Jacob Moody Archive and point to zuke 6 months ago
                                                                                
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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>

#include "dat.h"
#include "fncs.h"

const uchar none[] = {0};

/*
 * Rune and str marshal functions do not write null.
 */

void
marshalrune(int fd, Rune *r)
{
	static char buf[128];
	uchar n;

	if(r == nil || r[0] == '\0')
		write(fd, none, sizeof none);
	else{
		n = snprint(buf, 128, "%S", r);
		write(fd, &n, sizeof n);
		write(fd, buf, n);
	}
}

Rune*
unmarshalrune(int fd)
{
	static char buf[128];
	Rune *r;
	uchar n;
	read(fd, &n, sizeof n);
	if(n == 0)
		return nil;
	read(fd, buf, n);
	buf[n] = '\0';
	r = runesmprint("%s", buf);
	setmalloctag(r, getcallerpc(&fd));
	return r;
}

void
marshalstr(int fd, char *s)
{
	uint n = strlen(s);
	write(fd, &n, sizeof n);
	write(fd, s, n);
}

char*
unmarshalstr(int fd)
{
	uint n;
	char *s;
	read(fd, &n, sizeof n);
	s = emalloc(n+1);
	setmalloctag(s, getcallerpc(&fd));
	s[n] = '\0';
	read(fd, s, n);
	return s;
}

void
marshalvorbis(int fd, VorbisMeta *v)
{
	//TODO: store whole key-value pair
	marshalrune(fd, v->title);
	marshalrune(fd, v->artist);
	marshalrune(fd, v->album);
	write(fd, &(v->year), sizeof v->year);
	write(fd, &(v->tracknumber), sizeof v->tracknumber);
}

void
unmarshalvorbis(int fd, VorbisMeta *v)
{
	v->title = unmarshalrune(fd);
	v->artist = unmarshalrune(fd);
	v->album = unmarshalrune(fd);
	read(fd, &(v->year), sizeof v->year);
	read(fd, &(v->tracknumber), sizeof v->tracknumber);
}

void
marshalflacpic(int fd, FlacPic *p)
{
	//TODO store other fields
	marshalstr(fd, p->mime);
	write(fd, &(p->size), sizeof p->size);
	write(fd, p->data, p->size);
}

void
unmarshalflacpic(int fd, FlacPic **p)
{
	*p = emalloc(sizeof(FlacPic));
	(*p)->mime = unmarshalstr(fd);
	read(fd, &((*p)->size), sizeof (*p)->size);
	read(fd, (*p)->data, (*p)->size);
}

void
marshalflacmeta(int fd, FlacMeta *f)
{
	marshalvorbis(fd, f);
//	marshalflacpic(fd, f->pic);
}

void
unmarshalflacmeta(int fd, FlacMeta **f)
{
	*f = emalloc(sizeof(FlacMeta));
	unmarshalvorbis(fd, &((*f)->VorbisMeta));
//	unmarshalflacpic(fd, &((*f)->pic));
}

void
marshalid3(int fd, ID3v1 *id)
{
	marshalrune(fd, id->title);
	marshalrune(fd, id->artist);
	marshalrune(fd, id->album);
	write(fd, &(id->year), sizeof id->year);
	marshalrune(fd, id->comment);
	write(fd, &(id->genre), sizeof id->genre);
}

void
unmarshalid3(int fd, ID3v1 **id)
{
	*id = emalloc(sizeof(ID3v1));
	(*id)->title = unmarshalrune(fd);
	(*id)->artist = unmarshalrune(fd);
	(*id)->album = unmarshalrune(fd);
	read(fd, &((*id)->year), sizeof (*id)->year);
	(*id)->comment	= unmarshalrune(fd);
	read(fd, &((*id)->genre), sizeof (*id)->genre);
}

void
marshalsong(int fd, Song *s)
{
	write(fd, &(s->type), sizeof s->type);
	switch(s->type){
	case FLAC:
		marshalflacmeta(fd, s->fmeta);
		break;
	case MP3:
		marshalid3(fd, s->idmeta);
		break;
	default:
		sysfatal("not recognized or unsupported format");
	}
	marshalstr(fd, s->path);
}

void
unmarshalsong(int fd, Song *s)
{
	read(fd, &(s->type), sizeof s->type);
	switch(s->type){
	case FLAC:
		unmarshalflacmeta(fd, &(s->fmeta));
		break;
	case MP3:
		unmarshalid3(fd, &(s->idmeta));
		break;
	default:
		sysfatal("not recognized or unsupported format");
	}
	s->path = unmarshalstr(fd);
}

void
marshalalbum(int fd, Album *a)
{
	int i;
	int havepic = a->cover == nil ? 0 : 1;
	marshalrune(fd, a->name);
	write(fd, &(a->nsong), sizeof a->nsong);
	write(fd, &havepic, sizeof havepic);
	if(havepic)
		writeimage(fd, a->cover, 0);
	for(i=0;i<a->nsong;i++)
		marshalsong(fd, a->songs+i);
}

void
unmarshalalbum(int fd, Album *a)
{
	int i, havepic;
	a->name = unmarshalrune(fd);
	read(fd, &(a->nsong), sizeof a->nsong);
	read(fd, &havepic, sizeof havepic);
	if(havepic)
		a->cover = readimage(display, fd, 0);
	a->songs = emalloc(sizeof(Song)*a->nsong);
	setmalloctag(a->songs, getcallerpc(&fd));
	for(i=0;i<a->nsong;i++)
		unmarshalsong(fd, a->songs+i);
}

void
marshallib(int fd, Lib *l)
{
	int i;
	write(fd, &(l->nalbum), sizeof l->nalbum);
	for(i=0;i<l->nalbum;i++)
		marshalalbum(fd, l->start+i);
}

void
unmarshallib(int fd, Lib *l)
{
	int i;
	l->cursong = 0;
	read(fd, &(l->nalbum), sizeof l->nalbum);
	l->start = emalloc(sizeof(Album)*l->nalbum);
	setmalloctag(l->start, getcallerpc(&fd));
	for(i=0;i<l->nalbum;i++)
		unmarshalalbum(fd, l->start+i);
	l->stop = l->start+i;
	l->cur = l->start;
}