annotate src/xs_length.c @ 316:2b22c1f45097

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