summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichiel Van Der Kolk <not.valid@email.address>2005-04-27 12:11:50 +0000
committerMichiel Van Der Kolk <not.valid@email.address>2005-04-27 12:11:50 +0000
commit7e3f91d3d8bb0a672bd9803b84c2f08b76c92b60 (patch)
tree1e660bb1a6ce81060268820a9b2f6ae5234e566f
parent67772bbf1b5bb90848d82ac3415738345088086e (diff)
downloadrockbox-7e3f91d3d8bb0a672bd9803b84c2f08b76c92b60.tar.gz
rockbox-7e3f91d3d8bb0a672bd9803b84c2f08b76c92b60.zip
Obvious optimizations (i wasn't quite awake yet) + Code formatting policy.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6365 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/database.c207
1 files changed, 104 insertions, 103 deletions
diff --git a/apps/database.c b/apps/database.c
index 7f605baa16..615403dbed 100644
--- a/apps/database.c
+++ b/apps/database.c
@@ -115,138 +115,139 @@ void tagdb_shutdown(void)
115/*** TagDatabase code ***/ 115/*** TagDatabase code ***/
116 116
117void writetagdbheader() { 117void writetagdbheader() {
118 lseek(tagdb_fd,0,SEEK_SET); 118 lseek(tagdb_fd,0,SEEK_SET);
119 write(tagdb_fd, &tagdbheader, 68); 119 write(tagdb_fd, &tagdbheader, 68);
120} 120}
121 121
122void getfentrybyoffset(int offset) { 122void getfentrybyoffset(int offset) {
123 lseek(tagdb_fd,offset,SEEK_SET); 123 lseek(tagdb_fd,offset,SEEK_SET);
124 fread(tagdb_fd,sbuf,tagdbheader.filelen); 124 fread(tagdb_fd,sbuf,tagdbheader.filelen);
125 fread(tagdb_fd,&fe+sizeof(char *),12); 125 fread(tagdb_fd,&fe+sizeof(char *),12);
126 fe.name=sbuf; 126 fe.name=sbuf;
127 currentfeoffset=offset; 127 currentfeoffset=offset;
128 currentferecord=(offset-tagdbheader.filestart)/FILEENTRY_SIZE; 128 currentferecord=(offset-tagdbheader.filestart)/FILEENTRY_SIZE;
129} 129}
130 130
131#define getfentrybyrecord(_x_) getfentrybyoffset(FILERECORD2OFFSET(_x_)) 131#define getfentrybyrecord(_x_) getfentrybyoffset(FILERECORD2OFFSET(_x_))
132 132
133int getfentrybyfilename(char *fname) { 133int getfentrybyfilename(char *fname) {
134 int min=0; 134 int min=0;
135 int max=tagdbheader.filecount; 135 int max=tagdbheader.filecount;
136 while(min<max) { 136 while(min<max) {
137 int mid=(min+max)/2; 137 int mid=(min+max)/2;
138 int compare; 138 int compare;
139 getfentrybyrecord(mid); 139 getfentrybyrecord(mid);
140 compare=strcasecmp(fname,fe.name)); 140 compare=strcasecmp(fname,fe.name));
141 if(compare==0) 141 if(compare==0)
142 return 1; 142 return 1;
143 else if(compare<0) 143 else if(compare<0)
144 max=mid; 144 max=mid;
145 else 145 else
146 min=mid+1; 146 min=mid+1;
147 } 147 }
148 return 0; 148 return 0;
149} 149}
150 150
151int getfentrybyhash(int hash) { 151int getfentrybyhash(int hash) {
152 int min=0; 152 int min=0;
153 for(min=0;min<tagdbheader.filecount;min++) { 153 for(min=0;min<tagdbheader.filecount;min++) {
154 getfentrybyrecord(min); 154 getfentrybyrecord(min);
155 if(hash==fe.hash) 155 if(hash==fe.hash)
156 return 1; 156 return 1;
157 } 157 }
158 return 0; 158 return 0;
159} 159}
160 160
161int deletefentry(char *fname) { 161int deletefentry(char *fname) {
162 if(!getfentrybyfilename(fname)) 162 if(!getfentrybyfilename(fname))
163 return 0; 163 return 0;
164 int restrecord = currentferecord+1; 164 int restrecord = currentferecord+1;
165 if(currentferecord==tagdbheader.filecount) /* file is last entry */ 165 if(currentferecord!=tagdbheader.filecount) /* file is not last entry */
166 ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE); 166 shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord),(tagdbheader.filecount-restrecord)*FILEENTRY_SIZE);
167 else 167 ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE);
168 shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord)); 168 tagdbheader.filecount--;
169 tagdbheader.filecount--; 169 update_fentryoffsets(restrecord,tagdbheader.filecount);
170 update_fentryoffsets(restrecord,tagdbheader.filecount); 170 writetagdbheader();
171 writetagdbheader(); 171 return 1;
172 return 1;
173} 172}
174 173
175int update_fentryoffsets(int start, int end) { 174int update_fentryoffsets(int start, int end) {
176 int i; 175 int i;
177 for(int i=start;i<end;i++) { 176 for(int i=start;i<end;i++) {
178 getfentrybyrecord(i); 177 getfentrybyrecord(i);
179 if(fe.songentry!=-1) { 178 if(fe.songentry!=-1) {
180 int *p; 179 int p;
181 void *songentry=(void *)sbuf+tagdbheader.filelen+2; 180 lseek(tagdb_fd,fe.songentry+tagdbheader.songlen+8,SEEK_SET);
182 lseek(tagdb_fd,fe.songentry,SEEK_SET); 181 read(tagdb_fd,&p,sizeof(int));
183 read(tagdb_fd,songentry,SONGENTRY_SIZE); 182 if(p!=currentfeoffset) {
184 p=(int *)(songentry+tagdbheader.songlen+8); 183 p=currentfeoffset;
185 if(*p!=currentfeoffset) { 184 lseek(tagdb_fd,fe.songentry+tagdbheader.songlen+8,SEEK_SET);
186 *p=currentfeoffset; 185 write(tagdb_fd,&p,sizeof(int));
187 lseek(tagdb_fd,fe.songentry,SEEK_SET); 186 }
188 write(tagdb_fd,songentry,SONGENTRY_SIZE); 187 }
189 } 188 if(fe.rundbentry!=-1) {
190 } 189 splash(HZ*2, "o.o.. found a rundbentry? o.o; didn't update it, update the code o.o;");
191 if(fe.rundbentry!=-1) { 190 }
192 splash(HZ*2, "o.o.. found a rundbentry? o.o; didn't update it, update the code o.o;"); 191 }
193 }
194 }
195} 192}
196 193
197int tagdb_shiftdown(int targetoffset, int startingoffset) { 194int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes) {
198 int amount; 195 int amount;
199 if(targetoffset>=startingoffset) { 196 if(targetoffset>=startingoffset) {
200 splash(HZ*2,"Woah. no beeping way. (tagdb_shiftdown)"); 197 splash(HZ*2,"Woah. no beeping way. (tagdb_shiftdown)");
201 return 0; 198 return 0;
199 }
200 lseek(tagdb_fd,startingoffset,SEEK_SET);
201 while(amount=read(tagdb_fd,sbuf,bytes > 1024 ? 1024 : bytes)) {
202 int written;
203 startingoffset+=amount;
204 lseek(tagdb_fd,targetoffset,SEEK_SET);
205 written=write(tagdb_fd,sbuf,amount);
206 targetoffset+=written;
207 if(amount!=written) {
208 splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftdown)");
209 return 0;
202 } 210 }
203 lseek(tagdb_fd,startingoffset,SEEK_SET); 211 lseek(tagdb_fd,startingoffset,SEEK_SET);
204 while(amount=read(tagdb_fd,sbuf,1024)) { 212 bytes-=amount;
205 int written; 213 }
206 startingoffset+=amount; 214 return 1;
207 lseek(tagdb_fd,targetoffset,SEEK_SET);
208 written=write(tagdb_fd,sbuf,amount);
209 targetoffset+=written;
210 if(amount!=written) {
211 splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftdown)");
212 return 0;
213 }
214 lseek(tagdb_fd,startingoffset,SEEK_SET);
215 }
216 ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END) - (startingoffset-targetoffset));
217 return 1;
218} 215}
219 216
220int tagdb_shiftup(int targetoffset, int startingoffset) { 217int tagdb_shiftup(int targetoffset, int startingoffset, int bytes) {
221 int amount,amount2; 218 int amount,amount2;
222 int readpos,writepos,filelen; 219 int readpos,writepos,filelen;
223 int ok; 220 int ok;
224 if(targetoffset<=startingoffset) { 221 if(targetoffset<=startingoffset) {
225 splash(HZ*2,"Um. no. (tagdb_shiftup)"); 222 splash(HZ*2,"Um. no. (tagdb_shiftup)");
223 return 0;
224 }
225 filelen=lseek(tagdb_fd,0,SEEK_END);
226 readpos=startingoffset+bytes;
227 do {
228 amount=bytes>1024 ? 1024 : bytes;
229 readpos-=amount;
230 writepos=readpos+targetoffset-startingoffset;
231 lseek(tagdb_fd,readpos,SEEK_SET);
232 amount2=read(tagdb_fd,sbuf,amount);
233 if(amount2!=amount) {
234 splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
235 return 0;
226 } 236 }
227 filelen=lseek(tagdb_fd,0,SEEK_END); 237 lseek(tagdb_fd,writepos,SEEK_SET);
228 readpos=filelen; 238 amount=write(tagdb_fd,sbuf,amount2);
229 do { 239 if(amount2!=amount) {
230 amount=readpos-startingoffset>1024 ? 1024 : readpos-startingoffset; 240 splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
231 readpos-=amount; 241 return 0;
232 writepos=readpos+(targetoffset-startingoffset);
233 lseek(tagdb_fd,readpos,SEEK_SET);
234 amount2=read(tagdb_fd,sbuf,amount);
235 if(amount2!=amount) {
236 splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
237 }
238 lseek(tagdb_fd,writepos,SEEK_SET);
239 amount=write(tagdb_fd,sbuf,amount2);
240 if(amount2!=amount) {
241 splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
242 }
243 } while (amount>0);
244 if(amount==0)
245 return 1;
246 else {
247 splash(HZ*2,"Something went wrong, >.>;; (tagdb_shiftup)");
248 return 0;
249 } 242 }
243 bytes-=amount;
244 } while (amount>0);
245 if(bytes==0)
246 return 1;
247 else {
248 splash(HZ*2,"Something went wrong, >.>;; (tagdb_shiftup)");
249 return 0;
250 }
250} 251}
251 252
252/*** end TagDatabase code ***/ 253/*** end TagDatabase code ***/