Michiel Van Der Kolk | 4350eec | 2005-04-28 14:06:20 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 by Michiel van der Kolk |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Michiel Van Der Kolk | 4350eec | 2005-04-28 14:06:20 +0000 | [diff] [blame] | 16 | * |
| 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 Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 21 | #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 Kolk | b1e1e44 | 2005-06-05 23:00:42 +0000 | [diff] [blame] | 30 | #define SONGENTRY_SIZE (rb->tagdbheader->songlen+12+rb->tagdbheader->genrelen+12) |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 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) |
Michiel Van Der Kolk | c735ed7 | 2005-07-01 17:29:44 +0000 | [diff] [blame] | 34 | #define RUNDBENTRY_SIZE 20 |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 35 | |
| 36 | #define FILERECORD2OFFSET(_x_) (rb->tagdbheader->filestart + _x_ * FILEENTRY_SIZE) |
| 37 | |
Hristo Kovachev | cc6f37b | 2006-02-18 20:51:34 +0000 | [diff] [blame] | 38 | struct dbentry *currententry; |
Michiel Van Der Kolk | f34e4ff | 2005-05-11 00:12:33 +0000 | [diff] [blame] | 39 | struct dbglobals dbglobal; |
Hristo Kovachev | cc6f37b | 2006-02-18 20:51:34 +0000 | [diff] [blame] | 40 | static struct dbentry *entryarray; |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 41 | |
| 42 | int database_init() { |
| 43 | char *p; |
| 44 | unsigned int i; |
| 45 | // allocate room for all entries |
Hristo Kovachev | cc6f37b | 2006-02-18 20:51:34 +0000 | [diff] [blame] | 46 | entryarray=(struct dbentry *)my_malloc(sizeof(struct dbentry)*rb->tagdbheader->filecount); |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 47 | p=(char *)entryarray; |
| 48 | // zero all entries. |
Hristo Kovachev | cc6f37b | 2006-02-18 20:51:34 +0000 | [diff] [blame] | 49 | for(i=0;i<sizeof(struct dbentry)*rb->tagdbheader->filecount;i++) |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 50 | *(p++)=0; |
Michiel Van Der Kolk | c735ed7 | 2005-07-01 17:29:44 +0000 | [diff] [blame] | 51 | if(!*rb->tagdb_initialized) { |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 52 | if(!rb->tagdb_init()) { |
| 53 | // failed loading db |
| 54 | return -1; |
| 55 | } |
| 56 | } |
Michiel Van Der Kolk | f34e4ff | 2005-05-11 00:12:33 +0000 | [diff] [blame] | 57 | dbglobal.playcountmin=0; |
| 58 | dbglobal.playcountmax=0; |
| 59 | dbglobal.gotplaycountlimits=0; |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
Michiel Van Der Kolk | 9ceac0a | 2005-04-28 14:20:23 +0000 | [diff] [blame] | 63 | long readlong(int fd) { |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 64 | long num; |
| 65 | rb->read(fd,&num,4); |
Michiel Van Der Kolk | 9ceac0a | 2005-04-28 14:20:23 +0000 | [diff] [blame] | 66 | #ifdef ROCKBOX_LITTLE_ENDIAN |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 67 | num=BE32(num); |
Michiel Van Der Kolk | 9ceac0a | 2005-04-28 14:20:23 +0000 | [diff] [blame] | 68 | #endif |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 69 | return num; |
Michiel Van Der Kolk | 9ceac0a | 2005-04-28 14:20:23 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | short readshort(int fd) { |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 73 | short num; |
| 74 | rb->read(fd,&num,2); |
Michiel Van Der Kolk | 9ceac0a | 2005-04-28 14:20:23 +0000 | [diff] [blame] | 75 | #ifdef ROCKBOX_LITTLE_ENDIAN |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 76 | num=BE16(num); |
Michiel Van Der Kolk | 9ceac0a | 2005-04-28 14:20:23 +0000 | [diff] [blame] | 77 | #endif |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 78 | return num; |
Michiel Van Der Kolk | 9ceac0a | 2005-04-28 14:20:23 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 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); |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 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; |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 91 | } |
| 92 | currententry=&entryarray[filerecord]; |
Michiel Van Der Kolk | f34e4ff | 2005-05-11 00:12:33 +0000 | [diff] [blame] | 93 | dbglobal.currententryindex=filerecord; |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void loadsongdata() { |
Michiel Van Der Kolk | 238bea7 | 2005-04-28 18:49:23 +0000 | [diff] [blame] | 97 | if(currententry->loadedsongdata) |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 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); |
Michiel Van Der Kolk | 9ceac0a | 2005-04-28 14:20:23 +0000 | [diff] [blame] | 103 | currententry->artistoffset=readlong(*rb->tagdb_fd); |
| 104 | currententry->albumoffset=readlong(*rb->tagdb_fd); |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 105 | rb->lseek(*rb->tagdb_fd,4,SEEK_CUR); |
| 106 | rb->read(*rb->tagdb_fd,currententry->genre,rb->tagdbheader->genrelen); |
Michiel Van Der Kolk | 9ceac0a | 2005-04-28 14:20:23 +0000 | [diff] [blame] | 107 | currententry->bitrate=readshort(*rb->tagdb_fd); |
| 108 | currententry->year=readshort(*rb->tagdb_fd); |
Michiel Van Der Kolk | b1e1e44 | 2005-06-05 23:00:42 +0000 | [diff] [blame] | 109 | currententry->playtime=readlong(*rb->tagdb_fd); |
| 110 | currententry->track=readshort(*rb->tagdb_fd); |
| 111 | currententry->samplerate=readshort(*rb->tagdb_fd); |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 112 | currententry->loadedsongdata=1; |
| 113 | } |
| 114 | |
| 115 | void loadrundbdata() { |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 116 | currententry->loadedrundbdata=1; |
Michiel Van Der Kolk | c735ed7 | 2005-07-01 17:29:44 +0000 | [diff] [blame] | 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); |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void 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 Kolk | 238bea7 | 2005-04-28 18:49:23 +0000 | [diff] [blame] | 134 | if(currententry->loadedartistname) |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 135 | return; |
Michiel Van Der Kolk | 238bea7 | 2005-04-28 18:49:23 +0000 | [diff] [blame] | 136 | loadsongdata(); |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 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; |
| 141 | } |
| 142 | |
| 143 | void loadalbumname() { |
| 144 | /* see the note at loadartistname */ |
Michiel Van Der Kolk | 238bea7 | 2005-04-28 18:49:23 +0000 | [diff] [blame] | 145 | if(currententry->loadedalbumname) |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 146 | return; |
Michiel Van Der Kolk | 238bea7 | 2005-04-28 18:49:23 +0000 | [diff] [blame] | 147 | loadsongdata(); |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 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; |
| 152 | } |
| 153 | |
| 154 | char *getfilename(int entry) { |
| 155 | if(entryarray[entry].loadedfiledata==0) |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 156 | return "error O.o;;;"; |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 157 | else |
Michiel Van Der Kolk | ec407a8 | 2005-04-29 21:02:17 +0000 | [diff] [blame] | 158 | return entryarray[entry].filename; |
Michiel Van Der Kolk | 9369d48 | 2005-04-28 12:33:38 +0000 | [diff] [blame] | 159 | } |