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:
parent
67772bbf1b
commit
7e3f91d3d8
1 changed files with 104 additions and 103 deletions
207
apps/database.c
207
apps/database.c
|
|
@ -115,138 +115,139 @@ void tagdb_shutdown(void)
|
||||||
/*** TagDatabase code ***/
|
/*** TagDatabase code ***/
|
||||||
|
|
||||||
void writetagdbheader() {
|
void writetagdbheader() {
|
||||||
lseek(tagdb_fd,0,SEEK_SET);
|
lseek(tagdb_fd,0,SEEK_SET);
|
||||||
write(tagdb_fd, &tagdbheader, 68);
|
write(tagdb_fd, &tagdbheader, 68);
|
||||||
}
|
}
|
||||||
|
|
||||||
void getfentrybyoffset(int offset) {
|
void getfentrybyoffset(int offset) {
|
||||||
lseek(tagdb_fd,offset,SEEK_SET);
|
lseek(tagdb_fd,offset,SEEK_SET);
|
||||||
fread(tagdb_fd,sbuf,tagdbheader.filelen);
|
fread(tagdb_fd,sbuf,tagdbheader.filelen);
|
||||||
fread(tagdb_fd,&fe+sizeof(char *),12);
|
fread(tagdb_fd,&fe+sizeof(char *),12);
|
||||||
fe.name=sbuf;
|
fe.name=sbuf;
|
||||||
currentfeoffset=offset;
|
currentfeoffset=offset;
|
||||||
currentferecord=(offset-tagdbheader.filestart)/FILEENTRY_SIZE;
|
currentferecord=(offset-tagdbheader.filestart)/FILEENTRY_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define getfentrybyrecord(_x_) getfentrybyoffset(FILERECORD2OFFSET(_x_))
|
#define getfentrybyrecord(_x_) getfentrybyoffset(FILERECORD2OFFSET(_x_))
|
||||||
|
|
||||||
int getfentrybyfilename(char *fname) {
|
int getfentrybyfilename(char *fname) {
|
||||||
int min=0;
|
int min=0;
|
||||||
int max=tagdbheader.filecount;
|
int max=tagdbheader.filecount;
|
||||||
while(min<max) {
|
while(min<max) {
|
||||||
int mid=(min+max)/2;
|
int mid=(min+max)/2;
|
||||||
int compare;
|
int compare;
|
||||||
getfentrybyrecord(mid);
|
getfentrybyrecord(mid);
|
||||||
compare=strcasecmp(fname,fe.name));
|
compare=strcasecmp(fname,fe.name));
|
||||||
if(compare==0)
|
if(compare==0)
|
||||||
return 1;
|
return 1;
|
||||||
else if(compare<0)
|
else if(compare<0)
|
||||||
max=mid;
|
max=mid;
|
||||||
else
|
else
|
||||||
min=mid+1;
|
min=mid+1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getfentrybyhash(int hash) {
|
int getfentrybyhash(int hash) {
|
||||||
int min=0;
|
int min=0;
|
||||||
for(min=0;min<tagdbheader.filecount;min++) {
|
for(min=0;min<tagdbheader.filecount;min++) {
|
||||||
getfentrybyrecord(min);
|
getfentrybyrecord(min);
|
||||||
if(hash==fe.hash)
|
if(hash==fe.hash)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int deletefentry(char *fname) {
|
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 */
|
||||||
ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE);
|
shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord),(tagdbheader.filecount-restrecord)*FILEENTRY_SIZE);
|
||||||
else
|
ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE);
|
||||||
shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord));
|
tagdbheader.filecount--;
|
||||||
tagdbheader.filecount--;
|
update_fentryoffsets(restrecord,tagdbheader.filecount);
|
||||||
update_fentryoffsets(restrecord,tagdbheader.filecount);
|
writetagdbheader();
|
||||||
writetagdbheader();
|
return 1;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int update_fentryoffsets(int start, int end) {
|
int update_fentryoffsets(int start, int end) {
|
||||||
int i;
|
int i;
|
||||||
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) {
|
||||||
}
|
splash(HZ*2, "o.o.. found a rundbentry? o.o; didn't update it, update the code o.o;");
|
||||||
if(fe.rundbentry!=-1) {
|
}
|
||||||
splash(HZ*2, "o.o.. found a rundbentry? o.o; didn't update it, update the code o.o;");
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
while(amount=read(tagdb_fd,sbuf,bytes > 1024 ? 1024 : bytes)) {
|
||||||
|
int written;
|
||||||
|
startingoffset+=amount;
|
||||||
|
lseek(tagdb_fd,targetoffset,SEEK_SET);
|
||||||
|
written=write(tagdb_fd,sbuf,amount);
|
||||||
|
targetoffset+=written;
|
||||||
|
if(amount!=written) {
|
||||||
|
splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftdown)");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
lseek(tagdb_fd,startingoffset,SEEK_SET);
|
lseek(tagdb_fd,startingoffset,SEEK_SET);
|
||||||
while(amount=read(tagdb_fd,sbuf,1024)) {
|
bytes-=amount;
|
||||||
int written;
|
}
|
||||||
startingoffset+=amount;
|
return 1;
|
||||||
lseek(tagdb_fd,targetoffset,SEEK_SET);
|
|
||||||
written=write(tagdb_fd,sbuf,amount);
|
|
||||||
targetoffset+=written;
|
|
||||||
if(amount!=written) {
|
|
||||||
splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftdown)");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
lseek(tagdb_fd,startingoffset,SEEK_SET);
|
|
||||||
}
|
|
||||||
ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END) - (startingoffset-targetoffset));
|
|
||||||
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);
|
||||||
|
readpos=startingoffset+bytes;
|
||||||
|
do {
|
||||||
|
amount=bytes>1024 ? 1024 : bytes;
|
||||||
|
readpos-=amount;
|
||||||
|
writepos=readpos+targetoffset-startingoffset;
|
||||||
|
lseek(tagdb_fd,readpos,SEEK_SET);
|
||||||
|
amount2=read(tagdb_fd,sbuf,amount);
|
||||||
|
if(amount2!=amount) {
|
||||||
|
splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
filelen=lseek(tagdb_fd,0,SEEK_END);
|
lseek(tagdb_fd,writepos,SEEK_SET);
|
||||||
readpos=filelen;
|
amount=write(tagdb_fd,sbuf,amount2);
|
||||||
do {
|
if(amount2!=amount) {
|
||||||
amount=readpos-startingoffset>1024 ? 1024 : readpos-startingoffset;
|
splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
|
||||||
readpos-=amount;
|
return 0;
|
||||||
writepos=readpos+(targetoffset-startingoffset);
|
|
||||||
lseek(tagdb_fd,readpos,SEEK_SET);
|
|
||||||
amount2=read(tagdb_fd,sbuf,amount);
|
|
||||||
if(amount2!=amount) {
|
|
||||||
splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
|
|
||||||
}
|
|
||||||
lseek(tagdb_fd,writepos,SEEK_SET);
|
|
||||||
amount=write(tagdb_fd,sbuf,amount2);
|
|
||||||
if(amount2!=amount) {
|
|
||||||
splash(HZ*2,"Something went very wrong. expect database corruption. (tagdb_shiftup)");
|
|
||||||
}
|
|
||||||
} while (amount>0);
|
|
||||||
if(amount==0)
|
|
||||||
return 1;
|
|
||||||
else {
|
|
||||||
splash(HZ*2,"Something went wrong, >.>;; (tagdb_shiftup)");
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
bytes-=amount;
|
||||||
|
} while (amount>0);
|
||||||
|
if(bytes==0)
|
||||||
|
return 1;
|
||||||
|
else {
|
||||||
|
splash(HZ*2,"Something went wrong, >.>;; (tagdb_shiftup)");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*** end TagDatabase code ***/
|
/*** end TagDatabase code ***/
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue