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 ***/
|
||||
|
||||
void writetagdbheader() {
|
||||
lseek(tagdb_fd,0,SEEK_SET);
|
||||
write(tagdb_fd, &tagdbheader, 68);
|
||||
lseek(tagdb_fd,0,SEEK_SET);
|
||||
write(tagdb_fd, &tagdbheader, 68);
|
||||
}
|
||||
|
||||
void getfentrybyoffset(int offset) {
|
||||
lseek(tagdb_fd,offset,SEEK_SET);
|
||||
fread(tagdb_fd,sbuf,tagdbheader.filelen);
|
||||
fread(tagdb_fd,&fe+sizeof(char *),12);
|
||||
fe.name=sbuf;
|
||||
currentfeoffset=offset;
|
||||
currentferecord=(offset-tagdbheader.filestart)/FILEENTRY_SIZE;
|
||||
lseek(tagdb_fd,offset,SEEK_SET);
|
||||
fread(tagdb_fd,sbuf,tagdbheader.filelen);
|
||||
fread(tagdb_fd,&fe+sizeof(char *),12);
|
||||
fe.name=sbuf;
|
||||
currentfeoffset=offset;
|
||||
currentferecord=(offset-tagdbheader.filestart)/FILEENTRY_SIZE;
|
||||
}
|
||||
|
||||
#define getfentrybyrecord(_x_) getfentrybyoffset(FILERECORD2OFFSET(_x_))
|
||||
|
||||
int getfentrybyfilename(char *fname) {
|
||||
int min=0;
|
||||
int max=tagdbheader.filecount;
|
||||
while(min<max) {
|
||||
int mid=(min+max)/2;
|
||||
int compare;
|
||||
getfentrybyrecord(mid);
|
||||
compare=strcasecmp(fname,fe.name));
|
||||
if(compare==0)
|
||||
int min=0;
|
||||
int max=tagdbheader.filecount;
|
||||
while(min<max) {
|
||||
int mid=(min+max)/2;
|
||||
int compare;
|
||||
getfentrybyrecord(mid);
|
||||
compare=strcasecmp(fname,fe.name));
|
||||
if(compare==0)
|
||||
return 1;
|
||||
else if(compare<0)
|
||||
else if(compare<0)
|
||||
max=mid;
|
||||
else
|
||||
else
|
||||
min=mid+1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int getfentrybyhash(int hash) {
|
||||
int min=0;
|
||||
for(min=0;min<tagdbheader.filecount;min++) {
|
||||
getfentrybyrecord(min);
|
||||
if(hash==fe.hash)
|
||||
int min=0;
|
||||
for(min=0;min<tagdbheader.filecount;min++) {
|
||||
getfentrybyrecord(min);
|
||||
if(hash==fe.hash)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int deletefentry(char *fname) {
|
||||
if(!getfentrybyfilename(fname))
|
||||
return 0;
|
||||
int restrecord = currentferecord+1;
|
||||
if(currentferecord==tagdbheader.filecount) /* file is last entry */
|
||||
ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END)-FILEENTRY_SIZE);
|
||||
else
|
||||
shiftdown(FILERECORD2OFFSET(currentferecord),FILERECORD2OFFSET(restrecord));
|
||||
tagdbheader.filecount--;
|
||||
update_fentryoffsets(restrecord,tagdbheader.filecount);
|
||||
writetagdbheader();
|
||||
return 1;
|
||||
if(!getfentrybyfilename(fname))
|
||||
return 0;
|
||||
int restrecord = currentferecord+1;
|
||||
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);
|
||||
tagdbheader.filecount--;
|
||||
update_fentryoffsets(restrecord,tagdbheader.filecount);
|
||||
writetagdbheader();
|
||||
return 1;
|
||||
}
|
||||
|
||||
int update_fentryoffsets(int start, int end) {
|
||||
int i;
|
||||
for(int i=start;i<end;i++) {
|
||||
getfentrybyrecord(i);
|
||||
if(fe.songentry!=-1) {
|
||||
int *p;
|
||||
void *songentry=(void *)sbuf+tagdbheader.filelen+2;
|
||||
lseek(tagdb_fd,fe.songentry,SEEK_SET);
|
||||
read(tagdb_fd,songentry,SONGENTRY_SIZE);
|
||||
p=(int *)(songentry+tagdbheader.songlen+8);
|
||||
if(*p!=currentfeoffset) {
|
||||
*p=currentfeoffset;
|
||||
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;");
|
||||
}
|
||||
}
|
||||
int i;
|
||||
for(int i=start;i<end;i++) {
|
||||
getfentrybyrecord(i);
|
||||
if(fe.songentry!=-1) {
|
||||
int p;
|
||||
lseek(tagdb_fd,fe.songentry+tagdbheader.songlen+8,SEEK_SET);
|
||||
read(tagdb_fd,&p,sizeof(int));
|
||||
if(p!=currentfeoffset) {
|
||||
p=currentfeoffset;
|
||||
lseek(tagdb_fd,fe.songentry+tagdbheader.songlen+8,SEEK_SET);
|
||||
write(tagdb_fd,&p,sizeof(int));
|
||||
}
|
||||
}
|
||||
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 amount;
|
||||
if(targetoffset>=startingoffset) {
|
||||
splash(HZ*2,"Woah. no beeping way. (tagdb_shiftdown)");
|
||||
return 0;
|
||||
int tagdb_shiftdown(int targetoffset, int startingoffset, int bytes) {
|
||||
int amount;
|
||||
if(targetoffset>=startingoffset) {
|
||||
splash(HZ*2,"Woah. no beeping way. (tagdb_shiftdown)");
|
||||
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);
|
||||
while(amount=read(tagdb_fd,sbuf,1024)) {
|
||||
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);
|
||||
}
|
||||
ftruncate(tagdb_fd,lseek(tagdb_fd,0,SEEK_END) - (startingoffset-targetoffset));
|
||||
return 1;
|
||||
bytes-=amount;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tagdb_shiftup(int targetoffset, int startingoffset) {
|
||||
int amount,amount2;
|
||||
int readpos,writepos,filelen;
|
||||
int ok;
|
||||
if(targetoffset<=startingoffset) {
|
||||
splash(HZ*2,"Um. no. (tagdb_shiftup)");
|
||||
int tagdb_shiftup(int targetoffset, int startingoffset, int bytes) {
|
||||
int amount,amount2;
|
||||
int readpos,writepos,filelen;
|
||||
int ok;
|
||||
if(targetoffset<=startingoffset) {
|
||||
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);
|
||||
readpos=filelen;
|
||||
do {
|
||||
amount=readpos-startingoffset>1024 ? 1024 : readpos-startingoffset;
|
||||
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)");
|
||||
}
|
||||
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;
|
||||
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)");
|
||||
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 ***/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue