Mercurial > hg > xmms-sid
comparison src/xs_slsup.c @ 622:d7389ea52113
Move SLDB and STIL utility functions to xs_slsup.[ch] and amend some changes between XMMS-SID and Aud-SID via a macro mess.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 03 Sep 2007 00:22:04 +0000 |
parents | |
children | acaba070cf49 |
comparison
equal
deleted
inserted
replaced
621:11e24183ff23 | 622:d7389ea52113 |
---|---|
1 /* | |
2 XMMS-SID - SIDPlay input plugin for X MultiMedia System (XMMS) | |
3 | |
4 File information window | |
5 | |
6 Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org> | |
7 (C) Copyright 1999-2007 Tecnic Software productions (TNSP) | |
8 | |
9 This program is free software; you can redistribute it and/or modify | |
10 it under the terms of the GNU General Public License as published by | |
11 the Free Software Foundation; either version 2 of the License, or | |
12 (at your option) any later version. | |
13 | |
14 This program is distributed in the hope that it will be useful, | |
15 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 GNU General Public License for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License along | |
20 with this program; if not, write to the Free Software Foundation, Inc., | |
21 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
22 */ | |
23 #include "xs_slsup.h" | |
24 #include "xs_config.h" | |
25 | |
26 | |
27 static t_xs_sldb *xs_sldb_db = NULL; | |
28 XS_MUTEX(xs_sldb_db); | |
29 | |
30 static t_xs_stildb *xs_stildb_db = NULL; | |
31 XS_MUTEX(xs_stildb_db); | |
32 | |
33 | |
34 /* STIL-database handling | |
35 */ | |
36 gint xs_stil_init(void) | |
37 { | |
38 XS_MUTEX_LOCK(xs_cfg); | |
39 | |
40 if (!xs_cfg.stilDBPath) { | |
41 XS_MUTEX_UNLOCK(xs_cfg); | |
42 return -1; | |
43 } | |
44 | |
45 XS_MUTEX_LOCK(xs_stildb_db); | |
46 | |
47 /* Check if already initialized */ | |
48 if (xs_stildb_db) | |
49 xs_stildb_free(xs_stildb_db); | |
50 | |
51 /* Allocate database */ | |
52 xs_stildb_db = (t_xs_stildb *) g_malloc0(sizeof(t_xs_stildb)); | |
53 if (!xs_stildb_db) { | |
54 XS_MUTEX_UNLOCK(xs_cfg); | |
55 XS_MUTEX_UNLOCK(xs_stildb_db); | |
56 return -2; | |
57 } | |
58 | |
59 /* Read the database */ | |
60 if (xs_stildb_read(xs_stildb_db, xs_cfg.stilDBPath) != 0) { | |
61 xs_stildb_free(xs_stildb_db); | |
62 xs_stildb_db = NULL; | |
63 XS_MUTEX_UNLOCK(xs_cfg); | |
64 XS_MUTEX_UNLOCK(xs_stildb_db); | |
65 return -3; | |
66 } | |
67 | |
68 /* Create index */ | |
69 if (xs_stildb_index(xs_stildb_db) != 0) { | |
70 xs_stildb_free(xs_stildb_db); | |
71 xs_stildb_db = NULL; | |
72 XS_MUTEX_UNLOCK(xs_cfg); | |
73 XS_MUTEX_UNLOCK(xs_stildb_db); | |
74 return -4; | |
75 } | |
76 | |
77 XS_MUTEX_UNLOCK(xs_cfg); | |
78 XS_MUTEX_UNLOCK(xs_stildb_db); | |
79 return 0; | |
80 } | |
81 | |
82 | |
83 void xs_stil_close(void) | |
84 { | |
85 XS_MUTEX_LOCK(xs_stildb_db); | |
86 xs_stildb_free(xs_stildb_db); | |
87 xs_stildb_db = NULL; | |
88 XS_MUTEX_UNLOCK(xs_stildb_db); | |
89 } | |
90 | |
91 | |
92 t_xs_stil_node *xs_stil_get(gchar *pcFilename) | |
93 { | |
94 t_xs_stil_node *pResult; | |
95 gchar *tmpFilename; | |
96 | |
97 XS_MUTEX_LOCK(xs_stildb_db); | |
98 XS_MUTEX_LOCK(xs_cfg); | |
99 | |
100 if (xs_cfg.stilDBEnable && xs_stildb_db) { | |
101 if (xs_cfg.hvscPath) { | |
102 /* Remove postfixed directory separator from HVSC-path */ | |
103 tmpFilename = xs_strrchr(xs_cfg.hvscPath, '/'); | |
104 if (tmpFilename && (tmpFilename[1] == 0)) | |
105 tmpFilename[0] = 0; | |
106 | |
107 /* Remove HVSC location-prefix from filename */ | |
108 tmpFilename = strstr(pcFilename, xs_cfg.hvscPath); | |
109 if (tmpFilename) | |
110 tmpFilename += strlen(xs_cfg.hvscPath); | |
111 else | |
112 tmpFilename = pcFilename; | |
113 } else | |
114 tmpFilename = pcFilename; | |
115 | |
116 pResult = xs_stildb_get_node(xs_stildb_db, tmpFilename); | |
117 } else | |
118 pResult = NULL; | |
119 | |
120 XS_MUTEX_UNLOCK(xs_stildb_db); | |
121 XS_MUTEX_UNLOCK(xs_cfg); | |
122 | |
123 return pResult; | |
124 } | |
125 | |
126 | |
127 /* Song length database handling glue | |
128 */ | |
129 gint xs_songlen_init(void) | |
130 { | |
131 XS_MUTEX_LOCK(xs_cfg); | |
132 | |
133 if (!xs_cfg.songlenDBPath) { | |
134 XS_MUTEX_UNLOCK(xs_cfg); | |
135 return -1; | |
136 } | |
137 | |
138 XS_MUTEX_LOCK(xs_sldb_db); | |
139 | |
140 /* Check if already initialized */ | |
141 if (xs_sldb_db) | |
142 xs_sldb_free(xs_sldb_db); | |
143 | |
144 /* Allocate database */ | |
145 xs_sldb_db = (t_xs_sldb *) g_malloc0(sizeof(t_xs_sldb)); | |
146 if (!xs_sldb_db) { | |
147 XS_MUTEX_UNLOCK(xs_cfg); | |
148 XS_MUTEX_UNLOCK(xs_sldb_db); | |
149 return -2; | |
150 } | |
151 | |
152 /* Read the database */ | |
153 if (xs_sldb_read(xs_sldb_db, xs_cfg.songlenDBPath) != 0) { | |
154 xs_sldb_free(xs_sldb_db); | |
155 xs_sldb_db = NULL; | |
156 XS_MUTEX_UNLOCK(xs_cfg); | |
157 XS_MUTEX_UNLOCK(xs_sldb_db); | |
158 return -3; | |
159 } | |
160 | |
161 /* Create index */ | |
162 if (xs_sldb_index(xs_sldb_db) != 0) { | |
163 xs_sldb_free(xs_sldb_db); | |
164 xs_sldb_db = NULL; | |
165 XS_MUTEX_UNLOCK(xs_cfg); | |
166 XS_MUTEX_UNLOCK(xs_sldb_db); | |
167 return -4; | |
168 } | |
169 | |
170 XS_MUTEX_UNLOCK(xs_cfg); | |
171 XS_MUTEX_UNLOCK(xs_sldb_db); | |
172 return 0; | |
173 } | |
174 | |
175 | |
176 void xs_songlen_close(void) | |
177 { | |
178 XS_MUTEX_LOCK(xs_sldb_db); | |
179 xs_sldb_free(xs_sldb_db); | |
180 xs_sldb_db = NULL; | |
181 XS_MUTEX_UNLOCK(xs_sldb_db); | |
182 } | |
183 | |
184 | |
185 t_xs_sldb_node *xs_songlen_get(const gchar * pcFilename) | |
186 { | |
187 t_xs_sldb_node *pResult; | |
188 | |
189 XS_MUTEX_LOCK(xs_sldb_db); | |
190 | |
191 if (xs_cfg.songlenDBEnable && xs_sldb_db) | |
192 pResult = xs_sldb_get(xs_sldb_db, pcFilename); | |
193 else | |
194 pResult = NULL; | |
195 | |
196 XS_MUTEX_UNLOCK(xs_sldb_db); | |
197 | |
198 return pResult; | |
199 } | |
200 | |
201 | |
202 /* Allocate a new tune information structure | |
203 */ | |
204 t_xs_tuneinfo *xs_tuneinfo_new(const gchar * pcFilename, | |
205 gint nsubTunes, gint startTune, const gchar * sidName, | |
206 const gchar * sidComposer, const gchar * sidCopyright, | |
207 gint loadAddr, gint initAddr, gint playAddr, | |
208 gint dataFileLen, const gchar *sidFormat, gint sidModel) | |
209 { | |
210 t_xs_tuneinfo *pResult; | |
211 t_xs_sldb_node *tmpLength; | |
212 gint i; | |
213 | |
214 /* Allocate structure */ | |
215 pResult = (t_xs_tuneinfo *) g_malloc0(sizeof(t_xs_tuneinfo)); | |
216 if (!pResult) { | |
217 xs_error(_("Could not allocate memory for t_xs_tuneinfo ('%s')\n"), | |
218 pcFilename); | |
219 return NULL; | |
220 } | |
221 | |
222 pResult->sidFilename = XS_CS_FILENAME(pcFilename); | |
223 if (!pResult->sidFilename) { | |
224 xs_error(_("Could not allocate sidFilename ('%s')\n"), | |
225 pcFilename); | |
226 g_free(pResult); | |
227 return NULL; | |
228 } | |
229 | |
230 /* Allocate space for subtune information */ | |
231 pResult->subTunes = g_malloc0(sizeof(t_xs_subtuneinfo) * (nsubTunes + 1)); | |
232 if (!pResult->subTunes) { | |
233 xs_error(_("Could not allocate memory for t_xs_subtuneinfo ('%s', %i)\n"), | |
234 pcFilename, nsubTunes); | |
235 | |
236 g_free(pResult->sidFilename); | |
237 g_free(pResult); | |
238 return NULL; | |
239 } | |
240 | |
241 /* The following allocations don't matter if they fail */ | |
242 pResult->sidName = XS_CS_SID(sidName); | |
243 pResult->sidComposer = XS_CS_SID(sidComposer); | |
244 pResult->sidCopyright = XS_CS_SID(sidCopyright); | |
245 | |
246 pResult->nsubTunes = nsubTunes; | |
247 pResult->startTune = startTune; | |
248 | |
249 pResult->loadAddr = loadAddr; | |
250 pResult->initAddr = initAddr; | |
251 pResult->playAddr = playAddr; | |
252 pResult->dataFileLen = dataFileLen; | |
253 pResult->sidFormat = XS_CS_SID(sidFormat); | |
254 | |
255 pResult->sidModel = sidModel; | |
256 | |
257 /* Get length information (NOTE: Do not free this!) */ | |
258 tmpLength = xs_songlen_get(pcFilename); | |
259 | |
260 /* Fill in sub-tune information */ | |
261 for (i = 0; i < pResult->nsubTunes; i++) { | |
262 if (tmpLength && (i < tmpLength->nLengths)) | |
263 pResult->subTunes[i].tuneLength = tmpLength->sLengths[i]; | |
264 else | |
265 pResult->subTunes[i].tuneLength = -1; | |
266 | |
267 pResult->subTunes[i].tuneSpeed = -1; | |
268 } | |
269 | |
270 return pResult; | |
271 } | |
272 | |
273 | |
274 /* Free given tune information structure | |
275 */ | |
276 void xs_tuneinfo_free(t_xs_tuneinfo * pTune) | |
277 { | |
278 if (!pTune) return; | |
279 | |
280 g_free(pTune->subTunes); | |
281 g_free(pTune->sidFilename); | |
282 g_free(pTune->sidName); | |
283 g_free(pTune->sidComposer); | |
284 g_free(pTune->sidCopyright); | |
285 g_free(pTune->sidFormat); | |
286 g_free(pTune); | |
287 } |