blob: c9bbc12c02e5aa421cf843a6da26a06748ef6bc6 [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 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000019#include "searchengine.h"
20#include "dbinterface.h"
21
22#undef SONGENTRY_SIZE
23#undef FILEENTRY_SIZE
24#undef ALBUMENTRY_SIZE
25#undef ARTISTENTRY_SIZE
26#undef FILERECORD2OFFSET
27
28#define SONGENTRY_SIZE (rb->tagdbheader->songlen+12+rb->tagdbheader->genrelen+4)
29#define FILEENTRY_SIZE (rb->tagdbheader->filelen+12)
30#define ALBUMENTRY_SIZE (rb->tagdbheader->albumlen+4+rb->tagdbheader->songarraylen*4)
31#define ARTISTENTRY_SIZE (rb->tagdbheader->artistlen+rb->tagdbheader->albumarraylen*4)
32
33#define FILERECORD2OFFSET(_x_) (rb->tagdbheader->filestart + _x_ * FILEENTRY_SIZE)
34
35struct entry *currententry;
36
37static struct entry *entryarray;
38
39int database_init() {
40 char *p;
41 unsigned int i;
42 // allocate room for all entries
43 entryarray=(struct entry *)my_malloc(sizeof(struct entry)*rb->tagdbheader->filecount);
44 p=(char *)entryarray;
45 // zero all entries.
46 for(i=0;i<sizeof(struct entry)*rb->tagdbheader->filecount;i++)
47 *(p++)=0;
48 if(*rb->tagdb_initialized!=1) {
49 if(!rb->tagdb_init()) {
50 // failed loading db
51 return -1;
52 }
53 }
54 return 0;
55}
56
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000057long readlong(int fd) {
58 long num;
59 rb->read(fd,&num,4);
60#ifdef ROCKBOX_LITTLE_ENDIAN
61 num=BE32(num);
62#endif
63 return num;
64}
65
66short readshort(int fd) {
67 short num;
68 rb->read(fd,&num,2);
69#ifdef ROCKBOX_LITTLE_ENDIAN
70 num=BE16(num);
71#endif
72 return num;
73}
74
75
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000076void loadentry(int filerecord) {
77 if(entryarray[filerecord].loadedfiledata==0) {
78 rb->lseek(*rb->tagdb_fd,FILERECORD2OFFSET(filerecord),SEEK_SET);
79 entryarray[filerecord].filename=(char *)my_malloc(rb->tagdbheader->filelen);
80 rb->read(*rb->tagdb_fd,entryarray[filerecord].filename,rb->tagdbheader->filelen);
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000081 entryarray[filerecord].hash=readlong(*rb->tagdb_fd);
82 entryarray[filerecord].songentry=readlong(*rb->tagdb_fd);
83 entryarray[filerecord].rundbentry=readlong(*rb->tagdb_fd);
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000084 entryarray[filerecord].loadedfiledata=1;
85 }
86 currententry=&entryarray[filerecord];
87}
88
89void loadsongdata() {
Michiel Van Der Kolk238bea72005-04-28 18:49:23 +000090 if(currententry->loadedsongdata)
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000091 return;
92 currententry->title=(char *)my_malloc(rb->tagdbheader->songlen);
93 currententry->genre=(char *)my_malloc(rb->tagdbheader->genrelen);
94 rb->lseek(*rb->tagdb_fd,currententry->songentry,SEEK_SET);
95 rb->read(*rb->tagdb_fd,currententry->title,rb->tagdbheader->songlen);
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +000096 currententry->artistoffset=readlong(*rb->tagdb_fd);
97 currententry->albumoffset=readlong(*rb->tagdb_fd);
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +000098 rb->lseek(*rb->tagdb_fd,4,SEEK_CUR);
99 rb->read(*rb->tagdb_fd,currententry->genre,rb->tagdbheader->genrelen);
Michiel Van Der Kolk9ceac0a2005-04-28 14:20:23 +0000100 currententry->bitrate=readshort(*rb->tagdb_fd);
101 currententry->year=readshort(*rb->tagdb_fd);
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000102 currententry->loadedsongdata=1;
103}
104
105void loadrundbdata() {
106 // we don't do this yet.
107 currententry->loadedrundbdata=1;
108}
109
110void loadartistname() {
111 /* memory optimization possible, only malloc for an album name once, then
112 * write that pointer to the entrys using it.
113 */
Michiel Van Der Kolk238bea72005-04-28 18:49:23 +0000114 if(currententry->loadedartistname)
115 return;
116 loadsongdata();
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000117 currententry->artistname=(char *)my_malloc(rb->tagdbheader->artistlen);
118 rb->lseek(*rb->tagdb_fd,currententry->artistoffset,SEEK_SET);
119 rb->read(*rb->tagdb_fd,currententry->artistname,rb->tagdbheader->artistlen);
120 currententry->loadedartistname=1;
121}
122
123void loadalbumname() {
124 /* see the note at loadartistname */
Michiel Van Der Kolk238bea72005-04-28 18:49:23 +0000125 if(currententry->loadedalbumname)
126 return;
127 loadsongdata();
Michiel Van Der Kolk9369d482005-04-28 12:33:38 +0000128 currententry->albumname=(char *)my_malloc(rb->tagdbheader->albumlen);
129 rb->lseek(*rb->tagdb_fd,currententry->albumoffset,SEEK_SET);
130 rb->read(*rb->tagdb_fd,currententry->albumname,rb->tagdbheader->albumlen);
131 currententry->loadedalbumname=1;
132}
133
134char *getfilename(int entry) {
135 if(entryarray[entry].loadedfiledata==0)
136 return "error O.o;;;";
137 else
138 return entryarray[entry].filename;
139}