1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Michiel Van Der Kolk 2005-04-27 12:11:50 +00:00
parent 67772bbf1b
commit 7e3f91d3d8

View file

@ -162,10 +162,9 @@ int deletefentry(char *fname) {
if(!getfentrybyfilename(fname)) if(!getfentrybyfilename(fname))
return 0; return 0;
int restrecord = currentferecord+1; int restrecord = currentferecord+1;
if(currentferecord==tagdbheader.filecount) /* file is last entry */ if(currentferecord!=tagdbheader.filecount) /* file is not last entry */
shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord),(tagdbheader.filecount-restrecord)*FILEENTRY_SIZE);
ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE); ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE);
else
shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord));
tagdbheader.filecount--; tagdbheader.filecount--;
update_fentryoffsets(restrecord,tagdbheader.filecount); update_fentryoffsets(restrecord,tagdbheader.filecount);
writetagdbheader(); writetagdbheader();
@ -177,15 +176,13 @@ int update_fentryoffsets(int start, int end) {
for(int i=start;i<end;i++) { for(int i=start;i<end;i++) {
getfentrybyrecord(i); getfentrybyrecord(i);
if(fe.songentry!=-1) { if(fe.songentry!=-1) {
int *p; int p;
void *songentry=(void *)sbuf+tagdbheader.filelen+2; lseek(tagdb_fd,fe.songentry+tagdbheader.songlen+8,SEEK_SET);
lseek(tagdb_fd,fe.songentry,SEEK_SET); read(tagdb_fd,&p,sizeof(int));
read(tagdb_fd,songentry,SONGENTRY_SIZE); if(p!=currentfeoffset) {
p=(int *)(songentry+tagdbheader.songlen+8); p=currentfeoffset;
if(*p!=currentfeoffset) { lseek(tagdb_fd,fe.songentry+tagdbheader.songlen+8,SEEK_SET);
*p=currentfeoffset; write(tagdb_fd,&p,sizeof(int));
lseek(tagdb_fd,fe.songentry,SEEK_SET);
write(tagdb_fd,songentry,SONGENTRY_SIZE);
} }
} }
if(fe.rundbentry!=-1) { if(fe.rundbentry!=-1) {
@ -194,14 +191,14 @@ int update_fentryoffsets(int start, int end) {
} }
} }
int tagdb_shiftdown(int targetoffset, int startingoffset) { int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes) {
int amount; int amount;
if(targetoffset>=startingoffset) { if(targetoffset>=startingoffset) {
splash(HZ*2,"Woah. no beeping way. (tagdb_shiftdown)"); splash(HZ*2,"Woah. no beeping way. (tagdb_shiftdown)");
return 0; return 0;
} }
lseek(tagdb_fd,startingoffset,SEEK_SET); lseek(tagdb_fd,startingoffset,SEEK_SET);
while(amount=read(tagdb_fd,sbuf,1024)) { while(amount=read(tagdb_fd,sbuf,bytes > 1024 ? 1024 : bytes)) {
int written; int written;
startingoffset+=amount; startingoffset+=amount;
lseek(tagdb_fd,targetoffset,SEEK_SET); lseek(tagdb_fd,targetoffset,SEEK_SET);
@ -212,36 +209,40 @@ int tagdb_shiftdown(int targetoffset, int startingoffset) {
return 0; return 0;
} }
lseek(tagdb_fd,startingoffset,SEEK_SET); lseek(tagdb_fd,startingoffset,SEEK_SET);
bytes-=amount;
} }
ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END) - (startingoffset-targetoffset));
return 1; return 1;
} }
int tagdb_shiftup(int targetoffset, int startingoffset) { int tagdb_shiftup(int targetoffset, int startingoffset, int bytes) {
int amount,amount2; int amount,amount2;
int readpos,writepos,filelen; int readpos,writepos,filelen;
int ok; int ok;
if(targetoffset<=startingoffset) { if(targetoffset<=startingoffset) {
splash(HZ*2,"Um. no. (tagdb_shiftup)"); splash(HZ*2,"Um. no. (tagdb_shiftup)");
return 0;
} }
filelen=lseek(tagdb_fd,0,SEEK_END); filelen=lseek(tagdb_fd,0,SEEK_END);
readpos=filelen; readpos=startingoffset+bytes;
do { do {
amount=readpos-startingoffset>1024 ? 1024 : readpos-startingoffset; amount=bytes>1024 ? 1024 : bytes;
readpos-=amount; readpos-=amount;
writepos=readpos+(targetoffset-startingoffset); writepos=readpos+targetoffset-startingoffset;
lseek(tagdb_fd,readpos,SEEK_SET); lseek(tagdb_fd,readpos,SEEK_SET);
amount2=read(tagdb_fd,sbuf,amount); amount2=read(tagdb_fd,sbuf,amount);
if(amount2!=amount) { if(amount2!=amount) {
splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)"); splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
return 0;
} }
lseek(tagdb_fd,writepos,SEEK_SET); lseek(tagdb_fd,writepos,SEEK_SET);
amount=write(tagdb_fd,sbuf,amount2); amount=write(tagdb_fd,sbuf,amount2);
if(amount2!=amount) { if(amount2!=amount) {
splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)"); splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
return 0;
} }
bytes-=amount;
} while (amount>0); } while (amount>0);
if(amount==0) if(bytes==0)
return 1; return 1;
else { else {
splash(HZ*2,"Something went wrong, >.>;; (tagdb_shiftup)"); splash(HZ*2,"Something went wrong, >.>;; (tagdb_shiftup)");