comparison src/xs_length.c @ 402:f997b79a7251

More work on merging of improved STIL/SLDB handling code with completely dynamic memory allocation.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 01 Jun 2006 02:18:02 +0000
parents b09d74eb71e6
children 7f694e5a1493
comparison
equal deleted inserted replaced
401:30da794755f7 402:f997b79a7251
101 101
102 /* Parse one SLDB definition line, return SLDB node 102 /* Parse one SLDB definition line, return SLDB node
103 */ 103 */
104 t_xs_sldb_node * xs_sldb_read_entry(gchar *inLine) 104 t_xs_sldb_node * xs_sldb_read_entry(gchar *inLine)
105 { 105 {
106 gint linePos, savePos, i, tmpLen; 106 gint linePos, savePos, i, tmpLen, l;
107 gboolean iOK; 107 gboolean iOK;
108 t_xs_sldb_node *tmpNode; 108 t_xs_sldb_node *tmpNode;
109 109
110 /* Allocate new node */ 110 /* Allocate new node */
111 tmpNode = (t_xs_sldb_node *) g_malloc0(sizeof(t_xs_sldb_node)); 111 tmpNode = (t_xs_sldb_node *) g_malloc0(sizeof(t_xs_sldb_node));
134 134
135 /* Get number of sub-tune lengths */ 135 /* Get number of sub-tune lengths */
136 iOK = TRUE; 136 iOK = TRUE;
137 while ((linePos < tmpLen) && iOK) { 137 while ((linePos < tmpLen) && iOK) {
138 xs_findnext(inLine, &linePos); 138 xs_findnext(inLine, &linePos);
139
139 if (xs_sldb_gettime(inLine, &linePos) >= 0) 140 if (xs_sldb_gettime(inLine, &linePos) >= 0)
140 tmpNode->nLengths++; 141 tmpNode->nLengths++;
141 else 142 else
142 iOK = FALSE; 143 iOK = FALSE;
143 } 144 }
144 145
145 if (!iOK) {
146 xs_error("Invalid sub-tune length.\n");
147 xs_sldb_node_free(tmpNode);
148 return NULL;
149 }
150
151 /* Allocate memory for lengths */ 146 /* Allocate memory for lengths */
152 tmpNode->sLengths = (gint *) g_malloc0(tmpNode->nLengths * sizeof(gint)); 147 tmpNode->sLengths = (gint *) g_malloc0(tmpNode->nLengths * sizeof(gint));
153 if (!tmpNode->sLengths) { 148 if (!tmpNode->sLengths) {
154 xs_error("Could not allocate memory for node.\n"); 149 xs_error("Could not allocate memory for node.\n");
155 xs_sldb_node_free(tmpNode); 150 xs_sldb_node_free(tmpNode);
158 153
159 /* Read lengths in */ 154 /* Read lengths in */
160 i = 0; 155 i = 0;
161 linePos = savePos; 156 linePos = savePos;
162 iOK = TRUE; 157 iOK = TRUE;
163 while ((linePos < tmpLen) && iOK) { 158 while ((linePos < tmpLen) && (i < tmpNode->nLengths) && iOK) {
164 xs_findnext(inLine, &linePos); 159 xs_findnext(inLine, &linePos);
165 160
166 if (i < tmpNode->nLengths) { 161 l = xs_sldb_gettime(inLine, &linePos);
167 tmpNode->sLengths[i] = xs_sldb_gettime(inLine, &linePos); 162 if (l >= 0)
168 i++; 163 tmpNode->sLengths[i] = l;
169 } else 164 else
170 iOK = FALSE; 165 iOK = FALSE;
166
167 i++;
171 } 168 }
172 169
173 if (!iOK) { 170 if (!iOK) {
174 xs_sldb_node_free(tmpNode); 171 xs_sldb_node_free(tmpNode);
175 return NULL; 172 return NULL;
182 } 179 }
183 180
184 181
185 /* Read database to memory 182 /* Read database to memory
186 */ 183 */
187 gint xs_sldb_read(t_xs_sldb *db, gchar *dbFilename) 184 gint xs_sldb_read(t_xs_sldb *db, const gchar *dbFilename)
188 { 185 {
189 FILE *inFile; 186 FILE *inFile;
190 gchar inLine[XS_BUF_SIZE]; 187 gchar inLine[XS_BUF_SIZE];
191 gint lineNum; 188 gint lineNum;
192 t_xs_sldb_node *tmpNode; 189 t_xs_sldb_node *tmpNode;
427 guint8 startPage, pageLength; 424 guint8 startPage, pageLength;
428 guint16 reserved; 425 guint16 reserved;
429 } t_xs_psidv2_header; 426 } t_xs_psidv2_header;
430 427
431 428
432 static gint xs_get_sid_hash(gchar * pcFilename, t_xs_md5hash hash) 429 static gint xs_get_sid_hash(const gchar *pcFilename, t_xs_md5hash hash)
433 { 430 {
434 FILE *inFile; 431 FILE *inFile;
435 t_xs_md5state inState; 432 t_xs_md5state inState;
436 t_xs_psidv1_header psidH; 433 t_xs_psidv1_header psidH;
437 t_xs_psidv2_header psidH2; 434 t_xs_psidv2_header psidH2;
533 } 530 }
534 531
535 532
536 /* Get song lengths 533 /* Get song lengths
537 */ 534 */
538 t_xs_sldb_node *xs_sldb_get(t_xs_sldb *db, gchar *pcFilename) 535 t_xs_sldb_node *xs_sldb_get(t_xs_sldb *db, const gchar *pcFilename)
539 { 536 {
540 t_xs_sldb_node *pResult; 537 t_xs_sldb_node *pResult;
541 t_xs_md5hash dbHash; 538 t_xs_md5hash dbHash;
542 539
543 /* Get the hash and then look up from db */ 540 /* Get the hash and then look up from db */
610 xs_sldb_db = NULL; 607 xs_sldb_db = NULL;
611 XS_MUTEX_UNLOCK(xs_sldb_db); 608 XS_MUTEX_UNLOCK(xs_sldb_db);
612 } 609 }
613 610
614 611
615 t_xs_sldb_node *xs_songlen_get(gchar * pcFilename) 612 t_xs_sldb_node *xs_songlen_get(const gchar * pcFilename)
616 { 613 {
617 t_xs_sldb_node *pResult; 614 t_xs_sldb_node *pResult;
618 615
619 XS_MUTEX_LOCK(xs_sldb_db); 616 XS_MUTEX_LOCK(xs_sldb_db);
620 617