blob: 91df10fb045a9be44aa918803a5f3b1bfa54c7b7 [file] [log] [blame]
Michiel Van Der Kolk4350eec2005-04-28 14:06:20 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Michiel van der Kolk
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Michiel Van Der Kolk4350eec2005-04-28 14:06:20 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000021#include "searchengine.h"
22#include "dbinterface.h"
23
24#undef SONGENTRY_SIZE
25#undef FILEENTRY_SIZE
26#undef ALBUMENTRY_SIZE
27#undef ARTISTENTRY_SIZE
28#undef FILERECORD2OFFSET
29
Michiel Van Der Kolkb1e1e442005-06-05 23:00:42 +000030#define SONGENTRY_SIZE (rb->tagdbheader->songlen+12+rb->tagdbheader->genrelen+12)
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000031#define FILEENTRY_SIZE (rb->tagdbheader->filelen+12)
32#define ALBUMENTRY_SIZE (rb->tagdbheader->albumlen+4+rb->tagdbheader->songarraylen*4)
33#define ARTISTENTRY_SIZE (rb->tagdbheader->artistlen+rb->tagdbheader->albumarraylen*4)
Michiel Van Der Kolkc735ed72005-07-01 17:29:44 +000034#define RUNDBENTRY_SIZE 20
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000035
36#define FILERECORD2OFFSET(_x_) (rb->tagdbheader->filestart + _x_ * FILEENTRY_SIZE)
37
Hristo Kovachevcc6f37b2006-02-18 20:51:34 +000038struct dbentry *currententry;
Michiel Van Der Kolkf34e4ff2005-05-11 00:12:33 +000039struct dbglobals dbglobal;
Hristo Kovachevcc6f37b2006-02-18 20:51:34 +000040static struct dbentry *entryarray;
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000041
42int database_init() {
43 char *p;
44 unsigned int i;
45 // allocate room for all entries
Hristo Kovachevcc6f37b2006-02-18 20:51:34 +000046 entryarray=(struct dbentry *)my_malloc(sizeof(struct dbentry)*rb->tagdbheader->filecount);
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000047 p=(char *)entryarray;
48 // zero all entries.
Hristo Kovachevcc6f37b2006-02-18 20:51:34 +000049 for(i=0;i<sizeof(struct dbentry)*rb->tagdbheader->filecount;i++)
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +000050 *(p++)=0;
Michiel Van Der Kolkc735ed72005-07-01 17:29:44 +000051 if(!*rb->tagdb_initialized) {
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000052 if(!rb->tagdb_init()) {
53 // failed loading db
54 return -1;
55 }
56 }
Michiel Van Der Kolkf34e4ff2005-05-11 00:12:33 +000057 dbglobal.playcountmin=0;
58 dbglobal.playcountmax=0;
59 dbglobal.gotplaycountlimits=0;
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000060 return 0;
61}
62
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000063long readlong(int fd) {
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +000064 long num;
65 rb->read(fd,&num,4);
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000066#ifdef ROCKBOX_LITTLE_ENDIAN
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +000067 num=BE32(num);
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000068#endif
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +000069 return num;
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000070}
71
72short readshort(int fd) {
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +000073 short num;
74 rb->read(fd,&num,2);
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000075#ifdef ROCKBOX_LITTLE_ENDIAN
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +000076 num=BE16(num);
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000077#endif
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +000078 return num;
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000079}
80
81
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000082void loadentry(int filerecord) {
83 if(entryarray[filerecord].loadedfiledata==0) {
84 rb->lseek(*rb->tagdb_fd,FILERECORD2OFFSET(filerecord),SEEK_SET);
85 entryarray[filerecord].filename=(char *)my_malloc(rb->tagdbheader->filelen);
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +000086 rb->read(*rb->tagdb_fd,entryarray[filerecord].filename,rb->tagdbheader->filelen);
87 entryarray[filerecord].hash=readlong(*rb->tagdb_fd);
88 entryarray[filerecord].songentry=readlong(*rb->tagdb_fd);
89 entryarray[filerecord].rundbentry=readlong(*rb->tagdb_fd);
90 entryarray[filerecord].loadedfiledata=1;
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000091 }
92 currententry=&entryarray[filerecord];
Michiel Van Der Kolkf34e4ff2005-05-11 00:12:33 +000093 dbglobal.currententryindex=filerecord;
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000094}
95
96void loadsongdata() {
Michiel Van Der Kolk238bea72005-04-28 18:49:23 +000097 if(currententry->loadedsongdata)
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000098 return;
99 currententry->title=(char *)my_malloc(rb->tagdbheader->songlen);
100 currententry->genre=(char *)my_malloc(rb->tagdbheader->genrelen);
101 rb->lseek(*rb->tagdb_fd,currententry->songentry,SEEK_SET);
102 rb->read(*rb->tagdb_fd,currententry->title,rb->tagdbheader->songlen);
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +0000103 currententry->artistoffset=readlong(*rb->tagdb_fd);
104 currententry->albumoffset=readlong(*rb->tagdb_fd);
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000105 rb->lseek(*rb->tagdb_fd,4,SEEK_CUR);
106 rb->read(*rb->tagdb_fd,currententry->genre,rb->tagdbheader->genrelen);
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +0000107 currententry->bitrate=readshort(*rb->tagdb_fd);
108 currententry->year=readshort(*rb->tagdb_fd);
Michiel Van Der Kolkb1e1e442005-06-05 23:00:42 +0000109 currententry->playtime=readlong(*rb->tagdb_fd);
110 currententry->track=readshort(*rb->tagdb_fd);
111 currententry->samplerate=readshort(*rb->tagdb_fd);
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000112 currententry->loadedsongdata=1;
113}
114
115void loadrundbdata() {
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000116 currententry->loadedrundbdata=1;
Michiel Van Der Kolkc735ed72005-07-01 17:29:44 +0000117 if(!*rb->rundb_initialized)
118 return;
119 if(currententry->rundbentry==-1)
120 return;
121 rb->lseek(*rb->rundb_fd,currententry->rundbentry,SEEK_SET);
122 currententry->rundbfe=readlong(*rb->rundb_fd);
123 currententry->rundbhash=readlong(*rb->rundb_fd);
124 currententry->rating=readshort(*rb->rundb_fd);
125 currententry->voladj=readshort(*rb->rundb_fd);
126 currententry->playcount=readlong(*rb->rundb_fd);
127 currententry->lastplayed=readlong(*rb->rundb_fd);
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000128}
129
130void loadartistname() {
131 /* memory optimization possible, only malloc for an album name once, then
132 * write that pointer to the entrys using it.
133 */
Michiel Van Der Kolk238bea72005-04-28 18:49:23 +0000134 if(currententry->loadedartistname)
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +0000135 return;
Michiel Van Der Kolk238bea72005-04-28 18:49:23 +0000136 loadsongdata();
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000137 currententry->artistname=(char *)my_malloc(rb->tagdbheader->artistlen);
138 rb->lseek(*rb->tagdb_fd,currententry->artistoffset,SEEK_SET);
139 rb->read(*rb->tagdb_fd,currententry->artistname,rb->tagdbheader->artistlen);
140 currententry->loadedartistname=1;
141}
142
143void loadalbumname() {
144 /* see the note at loadartistname */
Michiel Van Der Kolk238bea72005-04-28 18:49:23 +0000145 if(currententry->loadedalbumname)
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +0000146 return;
Michiel Van Der Kolk238bea72005-04-28 18:49:23 +0000147 loadsongdata();
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000148 currententry->albumname=(char *)my_malloc(rb->tagdbheader->albumlen);
149 rb->lseek(*rb->tagdb_fd,currententry->albumoffset,SEEK_SET);
150 rb->read(*rb->tagdb_fd,currententry->albumname,rb->tagdbheader->albumlen);
151 currententry->loadedalbumname=1;
152}
153
154char *getfilename(int entry) {
155 if(entryarray[entry].loadedfiledata==0)
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +0000156 return "error O.o;;;";
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000157 else
Michiel Van Der Kolkec407a82005-04-29 21:02:17 +0000158 return entryarray[entry].filename;
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000159}