Mercurial > hg > xmms-sid
comparison src/xs_length.c @ 65:bf7b647b3239
The fabled 0.8-rewrite now works to some extent
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 29 Jun 2003 22:16:04 +0000 |
parents | 0d1df20745dd |
children | bf6a524cf7ca |
comparison
equal
deleted
inserted
replaced
64:d3bb914e3af3 | 65:bf7b647b3239 |
---|---|
95 /* | 95 /* |
96 * Get song length from database | 96 * Get song length from database |
97 */ | 97 */ |
98 t_xs_sldb_node * xs_db_get(t_xs_md5hash pHash) | 98 t_xs_sldb_node * xs_db_get(t_xs_md5hash pHash) |
99 { | 99 { |
100 gint iStartNode, iEndNode, iQNode, iFound, r, i; | 100 gint iStartNode, iEndNode, iQNode, r, i; |
101 gboolean iFound; | |
101 t_xs_sldb_node *pResult; | 102 t_xs_sldb_node *pResult; |
102 | 103 |
103 /* Check the database pointers */ | 104 /* Check the database pointers */ |
104 if ((xs_database == NULL) || (xs_dbindex == NULL)) | 105 if ((xs_database == NULL) || (xs_dbindex == NULL)) |
105 return NULL; | 106 return NULL; |
107 /* Look-up via index using binary search */ | 108 /* Look-up via index using binary search */ |
108 pResult = NULL; | 109 pResult = NULL; |
109 iStartNode = 0; | 110 iStartNode = 0; |
110 iEndNode = (xs_dbnodes - 1); | 111 iEndNode = (xs_dbnodes - 1); |
111 iQNode = (iEndNode / 2); | 112 iQNode = (iEndNode / 2); |
112 iFound = 0; | 113 iFound = FALSE; |
113 | 114 |
114 while ((!iFound) && ((iEndNode - iStartNode) > 128)) | 115 while ((!iFound) && ((iEndNode - iStartNode) > 128)) |
115 { | 116 { |
116 r = xs_db_cmphash(pHash, xs_dbindex[iQNode]->md5Hash); | 117 r = xs_db_cmphash(pHash, xs_dbindex[iQNode]->md5Hash); |
117 if (r < 0) | 118 if (r < 0) |
124 { | 125 { |
125 /* Hash was in the RIGHT -> side */ | 126 /* Hash was in the RIGHT -> side */ |
126 iStartNode = iQNode; | 127 iStartNode = iQNode; |
127 iQNode = iStartNode + ((iEndNode - iStartNode) / 2); | 128 iQNode = iStartNode + ((iEndNode - iStartNode) / 2); |
128 } else | 129 } else |
129 iFound = 1; | 130 iFound = TRUE; |
130 } | 131 } |
131 | 132 |
132 /* If not found already */ | 133 /* If not found already */ |
133 if (!iFound) | 134 if (!iFound) |
134 { | 135 { |
135 /* Search the are linearly */ | 136 /* Search the are linearly */ |
136 iFound = 0; | 137 iFound = FALSE; |
137 i = iStartNode; | 138 i = iStartNode; |
138 while ((i <= iEndNode) && (!iFound)) | 139 while ((i <= iEndNode) && (!iFound)) |
139 { | 140 { |
140 if (xs_db_cmphash(pHash, xs_dbindex[i]->md5Hash) == 0) | 141 if (xs_db_cmphash(pHash, xs_dbindex[i]->md5Hash) == 0) |
141 iFound = 1; | 142 iFound = TRUE; |
142 else | 143 else |
143 i++; | 144 i++; |
144 } | 145 } |
145 | 146 |
146 /* Check the result */ | 147 /* Check the result */ |
147 if (iFound) | 148 if (iFound) |
148 pResult = xs_dbindex[i]; | 149 pResult = xs_dbindex[i]; |
149 | 150 |
150 } else { | 151 } else { |
151 /* Found via binary search */ | 152 /* Found via binary search */ |
152 pResult = xs_dbindex[iEndNode]; | 153 pResult = xs_dbindex[iQNode]; |
153 } | 154 } |
154 | 155 |
155 | 156 |
156 return pResult; | 157 return pResult; |
157 } | 158 } |
319 */ | 320 */ |
320 gint xs_songlen_init(void) | 321 gint xs_songlen_init(void) |
321 { | 322 { |
322 t_xs_sldb_node *pCurr; | 323 t_xs_sldb_node *pCurr; |
323 gint i; | 324 gint i; |
325 | |
326 XSDEBUG("sldb_init()\n"); | |
324 | 327 |
325 /* Read the database */ | 328 /* Read the database */ |
326 if (xs_cfg.songlenDBPath == NULL) | 329 if (xs_cfg.songlenDBPath == NULL) |
327 return -10; | 330 return -10; |
328 | 331 |
329 if (xs_db_read(xs_cfg.songlenDBPath) < 0) | 332 if (xs_db_read(xs_cfg.songlenDBPath) < 0) |
330 return -9; | 333 return -9; |
334 | |
335 XSDEBUG("indexing...\n"); | |
331 | 336 |
332 /* Get size of db */ | 337 /* Get size of db */ |
333 pCurr = xs_database; | 338 pCurr = xs_database; |
334 xs_dbnodes = 0; | 339 xs_dbnodes = 0; |
335 while (pCurr) | 340 while (pCurr) |
357 /* Sort the indexes */ | 362 /* Sort the indexes */ |
358 qsort(xs_dbindex, xs_dbnodes, sizeof(t_xs_sldb_node *), xs_db_cmp); | 363 qsort(xs_dbindex, xs_dbnodes, sizeof(t_xs_sldb_node *), xs_db_cmp); |
359 } | 364 } |
360 | 365 |
361 /* OK */ | 366 /* OK */ |
367 XSDEBUG("init ok.\n"); | |
362 return 0; | 368 return 0; |
363 } | 369 } |
364 | 370 |
365 | 371 |
366 /* | 372 /* |
369 void xs_songlen_close(void) | 375 void xs_songlen_close(void) |
370 { | 376 { |
371 t_xs_sldb_node *pCurr, *pNext; | 377 t_xs_sldb_node *pCurr, *pNext; |
372 | 378 |
373 /* Free the memory allocated for database */ | 379 /* Free the memory allocated for database */ |
380 XSDEBUG("sldb_close()\n"); | |
374 pCurr = xs_database; | 381 pCurr = xs_database; |
375 while (pCurr) | 382 while (pCurr) |
376 { | 383 { |
377 pNext = pCurr->pNext; | 384 pNext = pCurr->pNext; |
378 xs_db_node_free(pCurr); | 385 xs_db_node_free(pCurr); |
561 /* Get the hash and then look up from db */ | 568 /* Get the hash and then look up from db */ |
562 if (xs_get_sid_hash(fileName, dbHash) == 0) | 569 if (xs_get_sid_hash(fileName, dbHash) == 0) |
563 { | 570 { |
564 dbNode = xs_db_get(dbHash); | 571 dbNode = xs_db_get(dbHash); |
565 | 572 |
566 if (dbNode && (subTune >= 0) && (subTune < dbNode->nLengths)) | 573 if (dbNode && (subTune >= 1) && (subTune <= dbNode->nLengths)) |
574 { | |
575 /* Get the length */ | |
567 iResult = dbNode->sLengths[subTune - 1]; | 576 iResult = dbNode->sLengths[subTune - 1]; |
577 | |
578 /* Take off few last seconds */ | |
579 if (iResult > 2) | |
580 iResult -= 2; | |
581 } | |
568 } | 582 } |
569 } | 583 } |
570 #if 0 | 584 |
571 XSDEBUG("fname='%s', sub=%i, res=%i\n", fileName, subTune, iResult); | |
572 #endif | |
573 | |
574 return iResult; | 585 return iResult; |
575 } | 586 } |
576 | 587 |
577 | 588 |