From 5fd7f7661b55d3913b0ad0142897605f36ce6d8c Mon Sep 17 00:00:00 2001 From: in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> Date: Fri, 14 Jan 2005 04:08:00 +0000 Subject: getindex(): fix bug and re-indent. use linear search if binary search could not find the target. git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@2395 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- mbbsd/record.c | 59 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/mbbsd/record.c b/mbbsd/record.c index 110ddd41..7a5d9c53 100644 --- a/mbbsd/record.c +++ b/mbbsd/record.c @@ -159,29 +159,48 @@ substitute_ref_record(char *direct, fileheader_t * fhdr, int ent) int getindex(char *direct, fileheader_t *fh_o, int end) { // Ptt: �q�e����ܶO�O �ӼɤO - int fd=-1, begin=1, i, stamp, s; + int fd = -1, begin = 1, i, stamp, s, times; 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->multi.money = fh.multi.money; - return i; - } - else begin = i; + if( end > (i = get_num_records(direct, sizeof(fileheader_t))) ) + end = i; + stamp = atoi(fh_o->filename + 2); + for( i = (begin + end) / 2, times = 0 ; + end > begin + 1 && times < 20 ; /* �̦h�u�� 20 �� */ + i = (begin + end) / 2, ++times ){ + if( get_record_keep(direct, &fh, sizeof(fileheader_t), i, &fd)==-1 || + !fh.filename[0] ) + break; + s = atoi(fh.filename + 2); + if( s > stamp ) + end = i + 1; + else if( s == stamp ){ + close(fd); + fh_o->multi.money = fh.multi.money; + return i; + } + else + begin = i; } - if(fd==-1) close(fd); + if( times == 20 ){ + /* �W���� binary search �걼�F, ���N��� linear search */ + end = get_num_records(direct, sizeof(fileheader_t)); + for( i = 1 ; i <= end ; ++i ){ + if( get_record_keep(direct, &fh, + sizeof(fileheader_t), i, &fd)==-1 ){ + if( fd != -1 ) + close(fd); + return 0; + } + if( atoi(fh.filename + 2) == stamp ){ + close(fd); + fh_o->multi.money = fh.multi.money; + return i; + } + } + } + if( fd != -1 ) + close(fd); return 0; } -- cgit v1.2.3