Bump version numbers for 3.13
[maemo-rb.git] / apps / plugins / searchengine / dbinterface.c
blob91df10fb045a9be44aa918803a5f3b1bfa54c7b7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Michiel van der Kolk
12 * 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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "searchengine.h"
22 #include "dbinterface.h"
24 #undef SONGENTRY_SIZE
25 #undef FILEENTRY_SIZE
26 #undef ALBUMENTRY_SIZE
27 #undef ARTISTENTRY_SIZE
28 #undef FILERECORD2OFFSET
30 #define SONGENTRY_SIZE (rb->tagdbheader->songlen+12+rb->tagdbheader->genrelen+12)
31 #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)
34 #define RUNDBENTRY_SIZE 20
36 #define FILERECORD2OFFSET(_x_) (rb->tagdbheader->filestart + _x_ * FILEENTRY_SIZE)
38 struct dbentry *currententry;
39 struct dbglobals dbglobal;
40 static struct dbentry *entryarray;
42 int database_init() {
43 char *p;
44 unsigned int i;
45 // allocate room for all entries
46 entryarray=(struct dbentry *)my_malloc(sizeof(struct dbentry)*rb->tagdbheader->filecount);
47 p=(char *)entryarray;
48 // zero all entries.
49 for(i=0;i<sizeof(struct dbentry)*rb->tagdbheader->filecount;i++)
50 *(p++)=0;
51 if(!*rb->tagdb_initialized) {
52 if(!rb->tagdb_init()) {
53 // failed loading db
54 return -1;
57 dbglobal.playcountmin=0;
58 dbglobal.playcountmax=0;
59 dbglobal.gotplaycountlimits=0;
60 return 0;
63 long readlong(int fd) {
64 long num;
65 rb->read(fd,&num,4);
66 #ifdef ROCKBOX_LITTLE_ENDIAN
67 num=BE32(num);
68 #endif
69 return num;
72 short readshort(int fd) {
73 short num;
74 rb->read(fd,&num,2);
75 #ifdef ROCKBOX_LITTLE_ENDIAN
76 num=BE16(num);
77 #endif
78 return num;
82 void 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);
86 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;
92 currententry=&entryarray[filerecord];
93 dbglobal.currententryindex=filerecord;
96 void loadsongdata() {
97 if(currententry->loadedsongdata)
98 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);
103 currententry->artistoffset=readlong(*rb->tagdb_fd);
104 currententry->albumoffset=readlong(*rb->tagdb_fd);
105 rb->lseek(*rb->tagdb_fd,4,SEEK_CUR);
106 rb->read(*rb->tagdb_fd,currententry->genre,rb->tagdbheader->genrelen);
107 currententry->bitrate=readshort(*rb->tagdb_fd);
108 currententry->year=readshort(*rb->tagdb_fd);
109 currententry->playtime=readlong(*rb->tagdb_fd);
110 currententry->track=readshort(*rb->tagdb_fd);
111 currententry->samplerate=readshort(*rb->tagdb_fd);
112 currententry->loadedsongdata=1;
115 void loadrundbdata() {
116 currententry->loadedrundbdata=1;
117 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);
130 void loadartistname() {
131 /* memory optimization possible, only malloc for an album name once, then
132 * write that pointer to the entrys using it.
134 if(currententry->loadedartistname)
135 return;
136 loadsongdata();
137 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;
143 void loadalbumname() {
144 /* see the note at loadartistname */
145 if(currententry->loadedalbumname)
146 return;
147 loadsongdata();
148 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;
154 char *getfilename(int entry) {
155 if(entryarray[entry].loadedfiledata==0)
156 return "error O.o;;;";
157 else
158 return entryarray[entry].filename;