Mercurial > hg > xmms-sid
annotate src/xs_length.c @ 389:3c239e4160d5
Cleanups.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 29 May 2006 23:46:04 +0000 |
parents | 7dc7e7dbbf90 |
children | b09d74eb71e6 |
rev | line source |
---|---|
4 | 1 /* |
2 XMMS-SID - SIDPlay input plugin for X MultiMedia System (XMMS) | |
3 | |
332 | 4 Get song length from SLDB for PSID/RSID files |
4 | 5 |
324 | 6 Programmed and designed by Matti 'ccr' Hamalainen <ccr@tnsp.org> |
7 (C) Copyright 1999-2005 Tecnic Software productions (TNSP) | |
4 | 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 | |
20 along with this program; if not, write to the Free Software | |
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 */ | |
39 | 23 #include "xs_length.h" |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
24 #include "xs_support.h" |
230 | 25 #include "xs_config.h" |
223
16e3b2446a73
On some systems stdlib.h has prerequisite of stdio.h, corrected order of
Matti Hamalainen <ccr@tnsp.org>
parents:
178
diff
changeset
|
26 #include <stdio.h> |
4 | 27 #include <stdlib.h> |
28 #include <ctype.h> | |
29 #include <string.h> | |
30 | |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
31 |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
32 /* Database handling functions |
4 | 33 */ |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
34 static t_xs_sldb_node *xs_sldb_node_new(void) |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
35 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
36 t_xs_sldb_node *pResult; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
37 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
38 /* Allocate memory for new node */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
39 pResult = (t_xs_sldb_node *) g_malloc0(sizeof(t_xs_sldb_node)); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
40 if (!pResult) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
41 return NULL; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
42 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
43 return pResult; |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
44 } |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
45 |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
46 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
47 static void xs_sldb_node_free(t_xs_sldb_node * pNode) |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
48 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
49 if (pNode) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
50 /* Nothing much to do here ... */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
51 g_free(pNode); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
52 } |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
53 } |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
54 |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
55 |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
56 /* Insert given node to db linked list |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
57 */ |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
58 static void xs_sldb_node_insert(t_xs_sldb * db, t_xs_sldb_node * pNode) |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
59 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
60 assert(db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
61 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
62 if (db->pNodes) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
63 /* The first node's pPrev points to last node */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
64 LPREV = db->pNodes->pPrev; /* New node's prev = Previous last node */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
65 db->pNodes->pPrev->pNext = pNode; /* Previous last node's next = New node */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
66 db->pNodes->pPrev = pNode; /* New last node = New node */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
67 LNEXT = NULL; /* But next is NULL! */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
68 } else { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
69 db->pNodes = pNode; /* First node ... */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
70 LPREV = pNode; /* ... it's also last */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
71 LNEXT = NULL; /* But next is NULL! */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
72 } |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
73 } |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
74 |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
75 |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
76 /* Parses a time-entry in SLDB format |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
77 */ |
389 | 78 static gint xs_sldb_gettime(gchar * pcStr, size_t * piPos) |
4 | 79 { |
389 | 80 gint iResult, iTemp; |
4 | 81 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
82 /* Check if it starts with a digit */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
83 if (isdigit(pcStr[*piPos])) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
84 /* Get minutes-field */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
85 iResult = 0; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
86 while (isdigit(pcStr[*piPos])) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
87 iResult = (iResult * 10) + (pcStr[(*piPos)++] - '0'); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
88 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
89 iResult *= 60; |
4 | 90 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
91 /* Check the field separator char */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
92 if (pcStr[*piPos] == ':') { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
93 /* Get seconds-field */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
94 (*piPos)++; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
95 iTemp = 0; |
385 | 96 while (isdigit(pcStr[*piPos])) { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
97 iTemp = (iTemp * 10) + (pcStr[(*piPos)++] - '0'); |
385 | 98 } |
4 | 99 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
100 iResult += iTemp; |
4 | 101 } else |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
102 iResult = -2; |
4 | 103 } else |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
104 iResult = -1; |
4 | 105 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
106 /* Ignore and skip the possible attributes */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
107 while (pcStr[*piPos] && !isspace(pcStr[*piPos])) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
108 (*piPos)++; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
109 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
110 return iResult; |
4 | 111 } |
112 | |
113 | |
389 | 114 t_xs_sldb_node * xs_sldb_read_entry(gchar *inLine) |
115 { | |
116 size_t linePos, savePos, tmpI, tmpLen; | |
117 t_xs_sldb_node *tmpNode; | |
118 | |
119 /* Allocate new node */ | |
120 if ((tmpNode = xs_sldb_node_new()) == NULL) { | |
121 XSERR("Error allocating new node. Fatal error.\n"); | |
122 return NULL; | |
123 } | |
124 | |
125 /* Get hash value */ | |
126 #if (XS_MD5HASH_LENGTH != 16) | |
127 #error Mismatch in hashcode length. Check against xs_md5.h and fix here. | |
128 #endif | |
129 sscanf(&inLine[0], "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x", | |
130 (guint *) & (tmpNode->md5Hash[0]), (guint *) & (tmpNode->md5Hash[1]), | |
131 (guint *) & (tmpNode->md5Hash[2]), (guint *) & (tmpNode->md5Hash[3]), | |
132 (guint *) & (tmpNode->md5Hash[4]), (guint *) & (tmpNode->md5Hash[5]), | |
133 (guint *) & (tmpNode->md5Hash[6]), (guint *) & (tmpNode->md5Hash[7]), | |
134 (guint *) & (tmpNode->md5Hash[8]), (guint *) & (tmpNode->md5Hash[9]), | |
135 (guint *) & (tmpNode->md5Hash[10]), (guint *) & (tmpNode->md5Hash[11]), | |
136 (guint *) & (tmpNode->md5Hash[12]), (guint *) & (tmpNode->md5Hash[13]), | |
137 (guint *) & (tmpNode->md5Hash[14]), (guint *) & (tmpNode->md5Hash[15])); | |
138 | |
139 /* Get playtimes */ | |
140 if (inLine[linePos] != 0) { | |
141 if (inLine[linePos] != '=') { | |
142 XSERR("'=' expected in SongLengthDB file '%s' line #%d, column #%d\n", | |
143 dbFilename, lineNum, linePos); | |
144 | |
145 xs_sldb_node_free(tmpNode); | |
146 return NULL; | |
147 } else { | |
148 /* First playtime is after '=' */ | |
149 savePos = ++linePos; | |
150 tmpLen = strlen(inLine); | |
151 | |
152 /* Get number of sub-tune lengths */ | |
153 iOK = TRUE; | |
154 while ((linePos < tmpLen) && iOK) { | |
155 xs_findnext(inLine, &linePos); | |
156 if (xs_sldb_gettime(inLine, &linePos) >= 0) | |
157 tmpNode->nLengths++; | |
158 else | |
159 iOK = FALSE; | |
160 } | |
161 | |
162 if (!iOK) { | |
163 XSERR("Invalid plaaplaa\n"); | |
164 xs_sldb_node_free(tmpNode); | |
165 return NULL; | |
166 } | |
167 | |
168 /* Allocate memory for lengths */ | |
169 tmpNode->sLengths = (gint *) g_malloc0(tmpNode->nLengths * sizeof(gint)); | |
170 if (!tmpNode->sLengths) { | |
171 XSERR("Could not allocate memory for SongLengthDB entry.\n"); | |
172 xs_sldb_node_free(tmpNode); | |
173 return NULL; | |
174 } | |
175 | |
176 /* Read lengths in */ | |
177 tmpI = 0; linePos = savePos; iOK = TRUE; | |
178 while ((linePos < tmpLen) && iOK) { | |
179 xs_findnext(inLine, &linePos); | |
180 | |
181 if (tmpI < tmpNode->nLengths) { | |
182 tmpNode->sLengths[tmpI] = xs_sldb_gettime(inLine, &linePos); | |
183 tmpI++; | |
184 } else | |
185 iOK = FALSE; | |
186 } | |
187 | |
188 if (!iOK) { | |
189 xs_sldb_node_free(tmpNode); | |
190 return NULL; | |
191 } else | |
192 return tmpNode; | |
193 } | |
194 } | |
195 | |
196 return NULL; | |
197 } | |
198 | |
199 | |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
200 /* Read database to memory |
4 | 201 */ |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
202 gint xs_sldb_read(t_xs_sldb * db, gchar * dbFilename) |
4 | 203 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
204 FILE *inFile; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
205 gchar inLine[XS_BUF_SIZE]; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
206 size_t lineNum, linePos; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
207 gboolean iOK; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
208 t_xs_sldb_node *tmpNode; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
209 assert(db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
210 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
211 /* Try to open the file */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
212 if ((inFile = fopen(dbFilename, "ra")) == NULL) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
213 XSERR("Could not open SongLengthDB '%s'\n", dbFilename); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
214 return -1; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
215 } |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
216 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
217 /* Read and parse the data */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
218 lineNum = 0; |
4 | 219 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
220 while (!feof(inFile)) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
221 fgets(inLine, XS_BUF_SIZE, inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
222 inLine[XS_BUF_SIZE - 1] = 0; |
389 | 223 linePos = 0; |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
224 lineNum++; |
4 | 225 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
226 /* Check if it is datafield */ |
389 | 227 if (isxdigit(inLine[linePos])) { |
228 size_t tmpI; | |
229 | |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
230 /* Check the length of the hash */ |
389 | 231 for (tmpI = 0; isxdigit(inLine[linePos]); linePos++, tmpI++); |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
232 |
389 | 233 if (tmpI != XS_MD5HASH_LENGTH_CH) { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
234 XSERR("Invalid hash in SongLengthDB file '%s' line #%d!\n", dbFilename, lineNum); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
235 } else { |
389 | 236 /* Parse and add node to db */ |
237 if ((tmpNode = xs_sldb_read_entry(inLine)) != NULL) | |
238 xs_sldb_node_insert(db, tmpNode); | |
4 | 239 } |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
240 } else if ((inLine[0] != ';') && (inLine[0] != '[')) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
241 XSERR("Invalid line in SongLengthDB file '%s' line #%d\n", dbFilename, lineNum); |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
242 } |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
243 |
389 | 244 } |
4 | 245 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
246 /* Close the file */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
247 fclose(inFile); |
4 | 248 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
249 return 0; |
4 | 250 } |
251 | |
252 | |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
253 /* Compare two given MD5-hashes. |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
254 * Return: 0 if equal |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
255 * negative if testHash1 < testHash2 |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
256 * positive if testHash1 > testHash2 |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
257 */ |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
258 static gint xs_sldb_cmphash(t_xs_md5hash testHash1, t_xs_md5hash testHash2) |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
259 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
260 register gint i, res = 0; |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
261 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
262 /* Compute difference of hashes */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
263 for (i = 0; (i < XS_MD5HASH_LENGTH) && (!res); i++) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
264 res = (testHash1[i] - testHash2[i]); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
265 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
266 return res; |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
267 } |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
268 |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
269 |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
270 /* Get node from db index via binary search |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
271 */ |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
272 static t_xs_sldb_node *xs_sldb_get_node(t_xs_sldb * db, t_xs_md5hash pHash) |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
273 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
274 gint iStartNode, iEndNode, iQNode, r, i; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
275 gboolean iFound; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
276 t_xs_sldb_node *pResult; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
277 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
278 /* Check the database pointers */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
279 if (!db || !db->pNodes || !db->ppIndex) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
280 return NULL; |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
281 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
282 /* Look-up via index using binary search */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
283 pResult = NULL; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
284 iStartNode = 0; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
285 iEndNode = (db->n - 1); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
286 iQNode = (iEndNode / 2); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
287 iFound = FALSE; |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
288 |
369
4611f1194941
Make binary search bailout value a #define in xmms-sid.h
Matti Hamalainen <ccr@tnsp.org>
parents:
359
diff
changeset
|
289 while ((!iFound) && ((iEndNode - iStartNode) > XS_BIN_BAILOUT)) { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
290 r = xs_sldb_cmphash(pHash, db->ppIndex[iQNode]->md5Hash); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
291 if (r < 0) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
292 /* Hash was in the <- LEFT side */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
293 iEndNode = iQNode; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
294 iQNode = iStartNode + ((iEndNode - iStartNode) / 2); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
295 } else if (r > 0) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
296 /* Hash was in the RIGHT -> side */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
297 iStartNode = iQNode; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
298 iQNode = iStartNode + ((iEndNode - iStartNode) / 2); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
299 } else |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
300 iFound = TRUE; |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
301 } |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
302 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
303 /* If not found already */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
304 if (!iFound) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
305 /* Search the are linearly */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
306 iFound = FALSE; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
307 i = iStartNode; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
308 while ((i <= iEndNode) && (!iFound)) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
309 if (xs_sldb_cmphash(pHash, db->ppIndex[i]->md5Hash) == 0) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
310 iFound = TRUE; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
311 else |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
312 i++; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
313 } |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
314 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
315 /* Check the result */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
316 if (iFound) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
317 pResult = db->ppIndex[i]; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
318 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
319 } else { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
320 /* Found via binary search */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
321 pResult = db->ppIndex[iQNode]; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
322 } |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
323 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
324 return pResult; |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
325 } |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
326 |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
327 |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
328 /* Compare two nodes |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
329 */ |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
330 static gint xs_sldb_cmp(const void *pNode1, const void *pNode2) |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
331 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
332 /* We assume here that we never ever get NULL-pointers or similar */ |
385 | 333 return xs_sldb_cmphash( |
334 (*(t_xs_sldb_node **) pNode1)->md5Hash, | |
335 (*(t_xs_sldb_node **) pNode2)->md5Hash); | |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
336 } |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
337 |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
338 |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
339 /* (Re)create index |
4 | 340 */ |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
341 gint xs_sldb_index(t_xs_sldb * db) |
4 | 342 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
343 t_xs_sldb_node *pCurr; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
344 gint i; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
345 assert(db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
346 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
347 /* Free old index */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
348 if (db->ppIndex) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
349 g_free(db->ppIndex); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
350 db->ppIndex = NULL; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
351 } |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
352 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
353 /* Get size of db */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
354 pCurr = db->pNodes; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
355 db->n = 0; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
356 while (pCurr) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
357 db->n++; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
358 pCurr = pCurr->pNext; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
359 } |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
360 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
361 /* Check number of nodes */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
362 if (db->n > 0) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
363 /* Allocate memory for index-table */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
364 db->ppIndex = (t_xs_sldb_node **) g_malloc(sizeof(t_xs_sldb_node *) * db->n); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
365 if (!db->ppIndex) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
366 return -1; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
367 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
368 /* Get node-pointers to table */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
369 i = 0; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
370 pCurr = db->pNodes; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
371 while (pCurr && (i < db->n)) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
372 db->ppIndex[i++] = pCurr; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
373 pCurr = pCurr->pNext; |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
374 } |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
375 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
376 /* Sort the indexes */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
377 qsort(db->ppIndex, db->n, sizeof(t_xs_sldb_node *), xs_sldb_cmp); |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
378 } |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
379 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
380 return 0; |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
381 } |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
382 |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
383 |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
384 /* Free a given song-length database |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
385 */ |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
386 void xs_sldb_free(t_xs_sldb * db) |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
387 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
388 t_xs_sldb_node *pCurr, *pNext; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
389 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
390 if (!db) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
391 return; |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
392 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
393 /* Free the memory allocated for nodes */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
394 pCurr = db->pNodes; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
395 while (pCurr) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
396 pNext = pCurr->pNext; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
397 xs_sldb_node_free(pCurr); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
398 pCurr = pNext; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
399 } |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
400 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
401 db->pNodes = NULL; |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
402 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
403 /* Free memory allocated for index */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
404 if (db->ppIndex) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
405 g_free(db->ppIndex); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
406 db->ppIndex = NULL; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
407 } |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
408 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
409 /* Free structure */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
410 db->n = 0; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
411 g_free(db); |
4 | 412 } |
413 | |
414 | |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
415 /* Compute md5hash of given SID-file |
4 | 416 */ |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
417 typedef struct |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
418 { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
419 gchar magicID[4]; /* "PSID" / "RSID" magic identifier */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
420 guint16 version, /* Version number */ |
385 | 421 dataOffset, /* Start of actual c64 data in file */ |
422 loadAddress, /* Loading address */ | |
423 initAddress, /* Initialization address */ | |
424 playAddress, /* Play one frame */ | |
425 nSongs, /* Number of subsongs */ | |
426 startSong; /* Default starting song */ | |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
427 guint32 speed; /* Speed */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
428 gchar sidName[32]; /* Descriptive text-fields, ASCIIZ */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
429 gchar sidAuthor[32]; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
430 gchar sidCopyright[32]; |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
431 } t_xs_psidv1_header; |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
432 |
4 | 433 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
434 typedef struct |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
435 { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
436 guint16 flags; /* Flags */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
437 guint8 startPage, pageLength; |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
438 guint16 reserved; |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
439 } t_xs_psidv2_header; |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
440 |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
441 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
442 static gint xs_get_sid_hash(gchar * pcFilename, t_xs_md5hash hash) |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
443 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
444 FILE *inFile; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
445 t_xs_md5state inState; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
446 t_xs_psidv1_header psidH; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
447 t_xs_psidv2_header psidH2; |
241
291715a519e2
Compile-time setting to enable/disable use of dynamically allocated buffers.
Matti Hamalainen <ccr@tnsp.org>
parents:
230
diff
changeset
|
448 #ifdef XS_BUF_DYNAMIC |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
449 guint8 *songData; |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
450 #else |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
451 guint8 songData[XS_SIDBUF_SIZE]; |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
452 #endif |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
453 guint8 ib8[2], i8; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
454 gint iIndex, iRes; |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
455 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
456 /* Try to open the file */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
457 if ((inFile = fopen(pcFilename, "rb")) == NULL) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
458 return -1; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
459 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
460 /* Read PSID header in */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
461 xs_rd_str(inFile, psidH.magicID, sizeof(psidH.magicID)); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
462 if ((psidH.magicID[0] != 'P' && psidH.magicID[0] != 'R') || |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
463 (psidH.magicID[1] != 'S') || (psidH.magicID[2] != 'I') || (psidH.magicID[3] != 'D')) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
464 fclose(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
465 return -2; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
466 } |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
467 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
468 psidH.version = xs_rd_be16(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
469 psidH.dataOffset = xs_rd_be16(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
470 psidH.loadAddress = xs_rd_be16(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
471 psidH.initAddress = xs_rd_be16(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
472 psidH.playAddress = xs_rd_be16(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
473 psidH.nSongs = xs_rd_be16(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
474 psidH.startSong = xs_rd_be16(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
475 psidH.speed = xs_rd_be32(inFile); |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
476 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
477 xs_rd_str(inFile, psidH.sidName, sizeof(psidH.sidName)); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
478 xs_rd_str(inFile, psidH.sidAuthor, sizeof(psidH.sidAuthor)); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
479 xs_rd_str(inFile, psidH.sidCopyright, sizeof(psidH.sidCopyright)); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
480 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
481 /* Check if we need to load PSIDv2NG header ... */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
482 if (psidH.version == 2) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
483 /* Yes, we need to */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
484 psidH2.flags = xs_rd_be16(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
485 psidH2.startPage = fgetc(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
486 psidH2.pageLength = fgetc(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
487 psidH2.reserved = xs_rd_be16(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
488 } |
385 | 489 |
241
291715a519e2
Compile-time setting to enable/disable use of dynamically allocated buffers.
Matti Hamalainen <ccr@tnsp.org>
parents:
230
diff
changeset
|
490 #ifdef XS_BUF_DYNAMIC |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
491 /* Allocate buffer */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
492 songData = (guint8 *) g_malloc(XS_SIDBUF_SIZE * sizeof(guint8)); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
493 if (!songData) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
494 fclose(inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
495 return -3; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
496 } |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
497 #endif |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
498 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
499 /* Read data to buffer */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
500 iRes = fread(songData, sizeof(guint8), XS_SIDBUF_SIZE, inFile); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
501 fclose(inFile); |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
502 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
503 /* Initialize and start MD5-hash calculation */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
504 xs_md5_init(&inState); |
385 | 505 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
506 if (psidH.loadAddress == 0) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
507 /* Strip load address (2 first bytes) */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
508 xs_md5_append(&inState, &songData[2], iRes - 2); |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
509 } else { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
510 /* Append "as is" */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
511 xs_md5_append(&inState, songData, iRes); |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
512 } |
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
513 |
241
291715a519e2
Compile-time setting to enable/disable use of dynamically allocated buffers.
Matti Hamalainen <ccr@tnsp.org>
parents:
230
diff
changeset
|
514 #ifdef XS_BUF_DYNAMIC |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
515 /* Free buffer */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
516 g_free(songData); |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
517 #endif |
87
94497283affa
Various fixes and improvements
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
518 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
519 /* Append header data to hash */ |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
520 #define XSADDHASH(QDATAB) { ib8[0] = (QDATAB & 0xff); ib8[1] = (QDATAB >> 8); xs_md5_append(&inState, (guint8 *) &ib8, sizeof(ib8)); } |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
521 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
522 XSADDHASH(psidH.initAddress) |
385 | 523 XSADDHASH(psidH.playAddress) |
524 XSADDHASH(psidH.nSongs) | |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
525 #undef XSADDHASH |
385 | 526 |
527 /* Append song speed data to hash */ | |
528 i8 = 0; | |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
529 for (iIndex = 0; (iIndex < psidH.nSongs) && (iIndex < 32); iIndex++) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
530 i8 = (psidH.speed & (1 << iIndex)) ? 60 : 0; |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
531 xs_md5_append(&inState, &i8, sizeof(i8)); |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
532 } |
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
533 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
534 /* Rest of songs (more than 32) */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
535 for (iIndex = 32; iIndex < psidH.nSongs; iIndex++) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
536 xs_md5_append(&inState, &i8, sizeof(i8)); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
537 } |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
538 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
539 /* PSIDv2NG specific */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
540 if (psidH.version == 2) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
541 /* SEE SIDPLAY HEADERS FOR INFO */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
542 i8 = (psidH2.flags >> 2) & 3; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
543 if (i8 == 2) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
544 xs_md5_append(&inState, &i8, sizeof(i8)); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
545 } |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
546 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
547 /* Calculate the hash */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
548 xs_md5_finish(&inState, hash); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
549 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
550 return 0; |
14
f5d82424b0ed
Made song-length database support FINALLY work!
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
551 } |
4 | 552 |
553 | |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
554 /* Get song lengths |
4 | 555 */ |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
556 t_xs_sldb_node *xs_sldb_get(t_xs_sldb * db, gchar * pcFilename) |
4 | 557 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
558 t_xs_sldb_node *pResult; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
559 t_xs_md5hash dbHash; |
4 | 560 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
561 /* Get the hash and then look up from db */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
562 if (xs_get_sid_hash(pcFilename, dbHash) == 0) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
563 pResult = xs_sldb_get_node(db, dbHash); |
227
92bad4c7b998
Improved modularization of STIL and song-length database subsystems.
Matti Hamalainen <ccr@tnsp.org>
parents:
223
diff
changeset
|
564 else |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
565 pResult = NULL; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
566 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
567 return pResult; |
4 | 568 } |
230 | 569 |
570 | |
571 /* | |
572 * These should be moved out of this module some day ... | |
573 */ | |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
574 static t_xs_sldb *xs_sldb_db = NULL; |
230 | 575 XS_MUTEX(xs_sldb_db); |
576 | |
577 gint xs_songlen_init(void) | |
578 { | |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
579 XS_MUTEX_LOCK(xs_cfg); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
580 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
581 if (!xs_cfg.songlenDBPath) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
582 XS_MUTEX_UNLOCK(xs_cfg); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
583 return -1; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
584 } |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
585 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
586 XS_MUTEX_LOCK(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
587 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
588 /* Check if already initialized */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
589 if (xs_sldb_db) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
590 xs_sldb_free(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
591 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
592 /* Allocate database */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
593 xs_sldb_db = (t_xs_sldb *) g_malloc0(sizeof(t_xs_sldb)); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
594 if (!xs_sldb_db) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
595 XS_MUTEX_UNLOCK(xs_cfg); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
596 XS_MUTEX_UNLOCK(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
597 return -2; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
598 } |
230 | 599 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
600 /* Read the database */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
601 if (xs_sldb_read(xs_sldb_db, xs_cfg.songlenDBPath) != 0) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
602 xs_sldb_free(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
603 xs_sldb_db = NULL; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
604 XS_MUTEX_UNLOCK(xs_cfg); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
605 XS_MUTEX_UNLOCK(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
606 return -3; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
607 } |
230 | 608 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
609 /* Create index */ |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
610 if (xs_sldb_index(xs_sldb_db) != 0) { |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
611 xs_sldb_free(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
612 xs_sldb_db = NULL; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
613 XS_MUTEX_UNLOCK(xs_cfg); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
614 XS_MUTEX_UNLOCK(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
615 return -4; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
616 } |
230 | 617 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
618 XS_MUTEX_UNLOCK(xs_cfg); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
619 XS_MUTEX_UNLOCK(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
620 return 0; |
230 | 621 } |
622 | |
623 | |
624 void xs_songlen_close(void) | |
625 { | |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
626 XS_MUTEX_LOCK(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
627 xs_sldb_free(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
628 xs_sldb_db = NULL; |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
629 XS_MUTEX_UNLOCK(xs_sldb_db); |
230 | 630 } |
631 | |
632 | |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
633 t_xs_sldb_node *xs_songlen_get(gchar * pcFilename) |
230 | 634 { |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
635 t_xs_sldb_node *pResult; |
230 | 636 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
637 XS_MUTEX_LOCK(xs_sldb_db); |
230 | 638 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
639 if (xs_cfg.songlenDBEnable && xs_sldb_db) |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
640 pResult = xs_sldb_get(xs_sldb_db, pcFilename); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
641 else |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
642 pResult = NULL; |
230 | 643 |
359
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
644 XS_MUTEX_UNLOCK(xs_sldb_db); |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
645 |
b1a858b8cb1a
Re-indentation all (non-generated) code.
Matti Hamalainen <ccr@tnsp.org>
parents:
332
diff
changeset
|
646 return pResult; |
230 | 647 } |