Mercurial > audlegacy-plugins
comparison src/cdaudio/cdinfo.c @ 12:3da1b8942b8b trunk
[svn] - remove src/Input src/Output src/Effect src/General src/Visualization src/Container
author | nenolod |
---|---|
date | Mon, 18 Sep 2006 03:14:20 -0700 |
parents | src/Input/cdaudio/cdinfo.c@6303e3a8a6b8 |
children | d127b22eb601 |
comparison
equal
deleted
inserted
replaced
11:cff1d04026ae | 12:3da1b8942b8b |
---|---|
1 /* | |
2 * cdinfo.c Copyright 1999 Espen Skoglund <esk@ira.uka.de> | |
3 * Copyright 1999 Håvard Kvålen <havardk@sol.no> | |
4 * | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify | |
7 * it under the terms of the GNU General Public License as published by | |
8 * the Free Software Foundation; either version 2 of the License, or | |
9 * (at your option) any later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, | |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 * GNU General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program; if not, write to the Free Software | |
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
19 */ | |
20 | |
21 | |
22 #include "cdinfo.h" | |
23 | |
24 #include <glib.h> | |
25 #include <glib/gi18n.h> | |
26 #include <glib/gprintf.h> | |
27 | |
28 #include <audacious/rcfile.h> | |
29 | |
30 #include "cdaudio.h" | |
31 | |
32 | |
33 /* | |
34 * Function cdda_cdinfo_flush (cdinfo) | |
35 * | |
36 * Free all information stored about the CD. | |
37 * | |
38 */ | |
39 void | |
40 cdda_cdinfo_flush(cdinfo_t * cdinfo) | |
41 { | |
42 trackinfo_t *t; | |
43 gint i; | |
44 | |
45 if (cdinfo->albname) | |
46 g_free(cdinfo->albname); | |
47 if (cdinfo->artname) | |
48 g_free(cdinfo->artname); | |
49 | |
50 cdinfo->albname = cdinfo->artname = NULL; | |
51 | |
52 for (t = cdinfo->tracks, i = 0; i < 100; i++, t++) { | |
53 if (t->artist) | |
54 g_free(t->artist); | |
55 if (t->title) | |
56 g_free(t->title); | |
57 | |
58 t->artist = t->title = NULL; | |
59 t->num = -1; | |
60 } | |
61 cdinfo->is_valid = FALSE; | |
62 } | |
63 | |
64 | |
65 /* | |
66 * Function cdda_cdinfo_delete (cdinfo) | |
67 * | |
68 * Free the indicated `cdinfo' structure. | |
69 * | |
70 */ | |
71 void | |
72 cdda_cdinfo_delete(cdinfo_t * cdinfo) | |
73 { | |
74 cdda_cdinfo_flush(cdinfo); | |
75 g_free(cdinfo); | |
76 } | |
77 | |
78 | |
79 /* | |
80 * Function cdda_cdinfo_new () | |
81 * | |
82 * Allocate a new `cdinfo' structure and return it. | |
83 * | |
84 */ | |
85 cdinfo_t * | |
86 cdda_cdinfo_new(void) | |
87 { | |
88 cdinfo_t *ret; | |
89 ret = g_malloc0(sizeof(cdinfo_t)); | |
90 cdda_cdinfo_flush(ret); | |
91 | |
92 return ret; | |
93 } | |
94 | |
95 | |
96 /* | |
97 * Function cdda_cdinfo_track_set (cdinfo, num, artist, title) | |
98 * | |
99 * Set `artist', and `title' for a track `num'. If the CD is a | |
100 * singleartist disc, the `artist' on each track should be set to | |
101 * NULL. | |
102 * | |
103 */ | |
104 void | |
105 cdda_cdinfo_track_set(cdinfo_t * cdinfo, gint num, gchar * artist, | |
106 gchar * title) | |
107 { | |
108 trackinfo_t *track = cdinfo->tracks + num; | |
109 | |
110 /* Check bounds */ | |
111 if (num < 1 || num >= 100) | |
112 return; | |
113 | |
114 track->artist = artist; | |
115 track->title = title; | |
116 track->num = num; | |
117 cdinfo->is_valid = TRUE; | |
118 } | |
119 | |
120 | |
121 /* | |
122 * Function cdda_cdinfo_cd_set (cdinfo, cdname, cdartist) | |
123 * | |
124 * Set name and artist for a cd. If CD is a multiartist disc, the | |
125 * `artist' should be set to NULL. | |
126 * | |
127 */ | |
128 void | |
129 cdda_cdinfo_cd_set(cdinfo_t * cdinfo, gchar * cdname, gchar * cdartist) | |
130 { | |
131 cdinfo->albname = cdname; | |
132 cdinfo->artname = cdartist; | |
133 cdinfo->is_valid = TRUE; | |
134 } | |
135 | |
136 | |
137 /* | |
138 * Function cdda_cdinfo_get (cdinfo, num, artist, album, title) | |
139 * | |
140 * Get artist, album, and title of the indicated track (i.e. store | |
141 * them in the specified pointers). Return 0 upon success, or -1 | |
142 * of track did not exist. The returned name must be subsequently | |
143 * freed using g_free(). | |
144 * | |
145 */ | |
146 gint | |
147 cdda_cdinfo_get(cdinfo_t * cdinfo, gint num, gchar ** artist, | |
148 gchar ** album, gchar ** title) | |
149 { | |
150 trackinfo_t *track = cdinfo->tracks + num; | |
151 | |
152 /* Check validity */ | |
153 if (!cdinfo->is_valid || num < 1 || num >= 100) | |
154 return -1; | |
155 | |
156 *artist = track->artist ? track->artist : | |
157 cdinfo->artname ? cdinfo->artname : _("(unknown)"); | |
158 *album = cdinfo->albname ? cdinfo->albname : _("(unknown)"); | |
159 *title = track->title ? track->title : _("(unknown)"); | |
160 | |
161 return track->num == -1 ? -1 : 0; | |
162 } | |
163 | |
164 | |
165 /* | |
166 * Function cdda_cdinfo_write_file | |
167 * | |
168 * Writes the cdinfo_t structure to disk. | |
169 */ | |
170 | |
171 | |
172 void | |
173 cdda_cdinfo_write_file(guint32 cddb_discid, cdinfo_t * cdinfo) | |
174 { | |
175 /* | |
176 * We currently identify cdinfo on disk with the CDDB-discid. | |
177 * Maybe it would be smarter to use the cdindex id instead? | |
178 */ | |
179 | |
180 gchar *filename; | |
181 RcFile *rcfile; | |
182 gchar sectionname[10], trackstr[16]; | |
183 gint i, numtracks = cddb_discid & 0xff; | |
184 | |
185 sprintf(sectionname, "%08x", cddb_discid); | |
186 | |
187 filename = | |
188 g_strconcat(g_get_home_dir(), "/", BMP_RCPATH, "/cdinfo", NULL); | |
189 if ((rcfile = bmp_rcfile_open(filename)) == NULL) | |
190 rcfile = bmp_rcfile_new(); | |
191 | |
192 if (cdinfo->albname) | |
193 bmp_rcfile_write_string(rcfile, sectionname, "Albumname", | |
194 cdinfo->albname); | |
195 else | |
196 bmp_rcfile_write_string(rcfile, sectionname, "Albumname", ""); | |
197 if (cdinfo->artname) | |
198 bmp_rcfile_write_string(rcfile, sectionname, "Artistname", | |
199 cdinfo->artname); | |
200 for (i = 1; i <= numtracks; i++) { | |
201 if (cdinfo->tracks[i].artist) { | |
202 sprintf(trackstr, "track_artist%d", i); | |
203 bmp_rcfile_write_string(rcfile, sectionname, trackstr, | |
204 cdinfo->tracks[i].artist); | |
205 } | |
206 if (cdinfo->tracks[i].title) { | |
207 sprintf(trackstr, "track_title%d", i); | |
208 bmp_rcfile_write_string(rcfile, sectionname, trackstr, | |
209 cdinfo->tracks[i].title); | |
210 } | |
211 } | |
212 bmp_rcfile_write(rcfile, filename); | |
213 bmp_rcfile_free(rcfile); | |
214 g_free(filename); | |
215 } | |
216 | |
217 /* | |
218 * Function cdda_cdinfo_read_file | |
219 * | |
220 * Tries to find and read a album from the disk-cache. | |
221 * | |
222 * Returns true if the album is found. | |
223 */ | |
224 | |
225 gboolean | |
226 cdda_cdinfo_read_file(guint32 cddb_discid, cdinfo_t * cdinfo) | |
227 { | |
228 gchar *filename; | |
229 RcFile *rcfile; | |
230 gchar sectionname[10], trackstr[16]; | |
231 gint i, numtracks = cddb_discid & 0xff; | |
232 gboolean track_found; | |
233 | |
234 sprintf(sectionname, "%08x", cddb_discid); | |
235 | |
236 // filename = g_strconcat(g_get_home_dir(), "/.audacious/cdinfo", NULL); | |
237 | |
238 filename = | |
239 g_strconcat(g_get_home_dir(), "/", BMP_RCPATH, "/cdinfo", NULL); | |
240 if ((rcfile = bmp_rcfile_open(filename)) == NULL) { | |
241 g_free(filename); | |
242 return FALSE; | |
243 } | |
244 g_free(filename); | |
245 | |
246 if (!bmp_rcfile_read_string | |
247 (rcfile, sectionname, "Albumname", &cdinfo->albname)) | |
248 return FALSE; | |
249 | |
250 bmp_rcfile_read_string(rcfile, sectionname, "Artistname", | |
251 &cdinfo->artname); | |
252 | |
253 for (i = 1; i <= numtracks; i++) { | |
254 track_found = FALSE; | |
255 sprintf(trackstr, "track_artist%d", i); | |
256 if (bmp_rcfile_read_string | |
257 (rcfile, sectionname, trackstr, &cdinfo->tracks[i].artist)) | |
258 track_found = TRUE; | |
259 sprintf(trackstr, "track_title%d", i); | |
260 if (bmp_rcfile_read_string | |
261 (rcfile, sectionname, trackstr, &cdinfo->tracks[i].title)) | |
262 track_found = TRUE; | |
263 if (track_found) | |
264 cdinfo->tracks[i].num = i; | |
265 } | |
266 cdinfo->is_valid = TRUE; | |
267 bmp_rcfile_free(rcfile); | |
268 return TRUE; | |
269 } |