diff options
author | ptt <ptt@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-05-01 00:00:27 +0800 |
---|---|---|
committer | ptt <ptt@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-05-01 00:00:27 +0800 |
commit | 770a864a23d078ed050edb116ee27db0ea43a449 (patch) | |
tree | 34e3a9225036ab166bfe59903a67b17a006730cb | |
parent | fe0da2f0c443d67c437276909c42d742c1eda259 (diff) | |
download | pttbbs-770a864a23d078ed050edb116ee27db0ea43a449.tar pttbbs-770a864a23d078ed050edb116ee27db0ea43a449.tar.gz pttbbs-770a864a23d078ed050edb116ee27db0ea43a449.tar.bz2 pttbbs-770a864a23d078ed050edb116ee27db0ea43a449.tar.lz pttbbs-770a864a23d078ed050edb116ee27db0ea43a449.tar.xz pttbbs-770a864a23d078ed050edb116ee27db0ea43a449.tar.zst pttbbs-770a864a23d078ed050edb116ee27db0ea43a449.zip |
update getindex to binary search
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@1903 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | include/proto.h | 2 | ||||
-rw-r--r-- | mbbsd/record.c | 49 |
2 files changed, 32 insertions, 19 deletions
diff --git a/include/proto.h b/include/proto.h index 523964fe..9c9267fc 100644 --- a/include/proto.h +++ b/include/proto.h @@ -442,7 +442,7 @@ int search_rec(char* dirname, int (*filecheck)()); int append_record_forward(char *fpath, fileheader_t *record, int size); int get_sum_records(char* fpath, int size); int substitute_ref_record(char* direct, fileheader_t *fhdr, int ent); -int getindex(char *fpath, char *fname, int start); +int getindex(char *fpath, fileheader_t *fh, int start); /* register */ int getnewuserid(void); diff --git a/mbbsd/record.c b/mbbsd/record.c index e551db61..be3b1c07 100644 --- a/mbbsd/record.c +++ b/mbbsd/record.c @@ -141,35 +141,48 @@ substitute_ref_record(char *direct, fileheader_t * fhdr, int ent) get_record(genbuf, &hdr, sizeof(hdr), num); if (strcmp(hdr.filename, fhdr->filename)) { - if((num = getindex(genbuf, fhdr->filename, num))) + if((num = getindex(genbuf, fhdr, num))) + { substitute_record(genbuf, fhdr, sizeof(*fhdr), num); - fhdr->money = FHR_REFERENCE | num ; // Ptt: update now! + } } else - substitute_record(genbuf, fhdr, sizeof(*fhdr), num); + { + fhdr->money = hdr.money; + substitute_record(genbuf, fhdr, sizeof(*fhdr), num); + } + fhdr->money = FHR_REFERENCE | num ; // Ptt: update now! } substitute_record(direct, fhdr, sizeof(*fhdr), ent); return num; } int -getindex(char *fpath, char *fname, int start) +getindex(char *direct, fileheader_t *fh_o, int end) { // Ptt: 從前面找很費力 太暴力 -#define READSIZE 64 // 8192 / sizeof(fileheader_t) - int fd, i, len, n = 1; /* n starts from 1 */ - fileheader_t fhdrs[READSIZE]; - - if ((fd = open(fpath, O_RDONLY, 0)) != -1) { - while( (len = read(fd, fhdrs, sizeof(fhdrs))) > 0 ){ - len /= sizeof(fileheader_t); - for( i = 0 ; i < len ; ++i, ++n ) - if (!strcmp(fhdrs[i].filename, fname)) { - close(fd); - return now; - } - } - close(fd); + int fd=-1, begin=1, i, stamp, s; + fileheader_t fh; + + i = get_num_records(direct, sizeof(fileheader_t)); + if(end>i) end = i; + stamp = atoi(fh_o->filename+2); + i=(begin+end)/2; + for(; end>begin+1; i=(begin+end)/2) + { + if(get_record_keep(direct, &fh, sizeof(fileheader_t), i, &fd)==-1) + break; + if(!fh.filename[0]) break; + s = atoi(fh.filename+2); + if (s > stamp) end = i+1; + else if(s == stamp) + { + close(fd); + fh_o->money = fh.money; + return i; + } + else begin = i; } + if(fd==-1) close(fd); return 0; } |