diff options
author | kcwu <kcwu@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2007-06-12 22:59:46 +0800 |
---|---|---|
committer | kcwu <kcwu@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2007-06-12 22:59:46 +0800 |
commit | ed870cef48392b5006d413b043589eb866988a87 (patch) | |
tree | b6dc781083029411b9c490eccf48aede211e5797 /mbbsd/brc.c | |
parent | b1742a061cfe32ebd4619a8f8a91812eaaf58bc0 (diff) | |
download | pttbbs-ed870cef48392b5006d413b043589eb866988a87.tar pttbbs-ed870cef48392b5006d413b043589eb866988a87.tar.gz pttbbs-ed870cef48392b5006d413b043589eb866988a87.tar.bz2 pttbbs-ed870cef48392b5006d413b043589eb866988a87.tar.lz pttbbs-ed870cef48392b5006d413b043589eb866988a87.tar.xz pttbbs-ed870cef48392b5006d413b043589eb866988a87.tar.zst pttbbs-ed870cef48392b5006d413b043589eb866988a87.zip |
revise brc interface and its document.
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@3542 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd/brc.c')
-rw-r--r-- | mbbsd/brc.c | 160 |
1 files changed, 106 insertions, 54 deletions
diff --git a/mbbsd/brc.c b/mbbsd/brc.c index 8c4b86da..8360cedc 100644 --- a/mbbsd/brc.c +++ b/mbbsd/brc.c @@ -6,7 +6,7 @@ */ #ifndef BRC_MAXNUM -#define BRC_STRLEN 15 /* Length of board name */ +#define BRC_STRLEN 15 /* Length of board name, for old brc */ #define BRC_MAXSIZE 24576 /* Effective size of brc rc file, 8192 * 3 */ #define BRC_MAXNUM 80 /* Upper bound of brc_num, size of brc_list */ #endif @@ -26,45 +26,38 @@ typedef unsigned short brcnbrd_t; * brc_num 1 byte, binary integer * brc_list brc_num * sizeof(int) bytes, brc_num binary integer(s) */ +static char brc_initialized = 0; static time4_t brc_expire_time; /* Will be set to the time one year before login. All the files created * before then will be recognized read. */ -static int brc_changed = 0; +static int brc_changed = 0; /**< brc_list/brc_num changed */ /* The below two will be filled by read_brc_buf() and brc_update() */ static char *brc_buf = NULL; static int brc_size; static int brc_alloc; +// read records for currbid +static int brc_currbid; +static int brc_num; +static time4_t brc_list[BRC_MAXNUM]; + static char * const fn_oldboardrc = ".boardrc"; static char * const fn_brc = ".brc2"; -#if 0 -/* unused after brc2 */ -static char * -brc_getrecord(char *ptr, char *endp, brcbid_t *bid, - brcnbrd_t *pnum, time4_t *list) -{ - brcnbrd_t num; - char *tmp; - - if (ptr + sizeof(brcbid_t) + sizeof(brcnbrd_t) > endp) - return endp + 1; /* dangling, ignoring it */ - *bid = *(brcbid_t*)ptr; /* bid */ - ptr += sizeof(brcbid_t); - num = *(brcnbrd_t*)ptr; /* brc_num */ - ptr += sizeof(brcnbrd_t); - tmp = ptr + num * sizeof(time4_t); /* end of this record */ - if (tmp <= endp){ - memcpy(list, ptr, num * sizeof(time4_t)); /* brc_list */ - if (num > BRC_MAXNUM) - num = BRC_MAXNUM; - *pnum = num; - } - return tmp; -} -#endif - +/** + * find read records of bid in given buffer region + * + * @param[in] begin + * @param[in] endp + * @param bid + * @param[out] num number of records, which could be written for \a bid + * = brc_num if found + * = 0 or dangling size if not found + * + * @return address of record. \a begin <= addr < \a endp. + * 0 if not found + */ /* Returns the address of the record strictly between begin and endp with * bid equal to the parameter. Returns 0 if not found. * brcnbrd_t *num is an output parameter which will filled with brc_num @@ -97,7 +90,7 @@ brc_findrecord_in(char *begin, char *endp, brcbid_t bid, brcnbrd_t *num) return 0; } -time4_t * +static time4_t * brc_find_record(int bid, int *num) { char *p; @@ -152,11 +145,13 @@ brc_enlarge_buf(void) #endif buffer = (char*)THE_MALLOC(brc_alloc); + assert(buffer); memcpy(buffer, brc_buf, brc_alloc); free(brc_buf); brc_alloc += BRC_BLOCKSIZE; brc_buf = (char*)malloc(brc_alloc); + assert(brc_buf); memcpy(brc_buf, buffer, brc_alloc - BRC_BLOCKSIZE); #ifdef DEBUG @@ -179,6 +174,7 @@ brc_get_buf(int size){ if (brc_alloc > BRC_MAXSIZE) brc_alloc = BRC_MAXSIZE; brc_buf = (char*)malloc(brc_alloc); + assert(brc_buf); } static inline void @@ -239,10 +235,13 @@ brc_insert_record(brcbid_t bid, brcnbrd_t num, const time4_t* list) brc_changed = 0; } +/** + * write \a brc_num and \a brc_list back to \a brc_buf. + */ void brc_update(){ - if (brc_changed && cuser.userlevel && brc_num > 0) - brc_insert_record(currbid, brc_num, brc_list); + if (brc_currbid && brc_changed && cuser.userlevel && brc_num > 0) + brc_insert_record(brc_currbid, brc_num, brc_list); } /* return 1 if successfully read from old .boardrc file. @@ -314,36 +313,65 @@ read_brc_buf(void) } void +brc_release() +{ + if (brc_buf) { + free(brc_buf); + brc_buf = NULL; + } + brc_currbid = 0; + brc_num = 0; + brc_changed = 0; + brc_size = brc_alloc = 0; +} + +void brc_finalize(){ char brcfile[STRLEN]; char tmpfile[STRLEN]; - int fd; - int ok=0; + brc_update(); setuserfile(brcfile, fn_brc); snprintf(tmpfile, sizeof(tmpfile), "%s.tmp.%x", brcfile, getpid()); - if (brc_buf != NULL && - (fd = open(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0644)) != -1) { - if(write(fd, brc_buf, brc_size)==brc_size) - ok=1; - close(fd); + if (brc_buf != NULL) { + int fd = open(tmpfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd != -1) { + int ok=0; + if(write(fd, brc_buf, brc_size)==brc_size) + ok=1; + close(fd); + if(ok) + Rename(tmpfile, brcfile); + else + unlink(tmpfile); + } } - if(ok) - Rename(tmpfile, brcfile); + + brc_release(); + brc_initialized = 0; } int brc_initialize(){ - static char done = 0; - if (done) + if (brc_initialized) return 1; - done = 1; + brc_initialized = 1; brc_expire_time = login_start_time - 365 * 86400; read_brc_buf(); return 0; } -int +/** + * get the read records for bid + * + * @param bid + * @param[out] num number of record for \a bid. 1 <= \a num <= \a BRC_MAXNUM + * \a num = 1 if no records. + * @param[out] list the list of records, length \a num + * + * @return number of read record, 0 if no records + */ +static int brc_read_record(int bid, int *num, time4_t *list){ char *ptr; brcnbrd_t tnum; @@ -358,12 +386,16 @@ brc_read_record(int bid, int *num, time4_t *list){ return 0; } +/** + * @return number of records in \a boardname + */ int brc_initial_board(const char *boardname) { brc_initialize(); if (strcmp(currboard, boardname) == 0) { + assert(currbid == brc_currbid); return brc_num; } @@ -372,16 +404,17 @@ brc_initial_board(const char *boardname) if( currbid == 0 ) currbid = getbnum(DEFAULT_BOARD); assert(0<=currbid-1 && currbid-1<MAX_BOARD); + brc_currbid = currbid; currboard = bcache[currbid - 1].brdname; currbrdattr = bcache[currbid - 1].brdattr; - return brc_read_record(currbid, &brc_num, brc_list); + return brc_read_record(brc_currbid, &brc_num, brc_list); } -void +static void brc_trunc(int bid, time4_t ftime){ brc_insert_record(bid, 1, &ftime); - if ( bid == currbid ){ + if ( bid == brc_currbid ){ brc_num = 1; brc_list[0] = ftime; brc_changed = 0; @@ -389,11 +422,21 @@ brc_trunc(int bid, time4_t ftime){ } void +brc_toggle_all_read(int bid, int is_all_read) +{ + if (is_all_read) + brc_trunc(bid, now); + else + brc_trunc(bid, 1); +} + +void brc_addlist(const char *fname) { int n, i; time4_t ftime; + assert(currbid == brc_currbid); if (!cuser.userlevel) return; @@ -426,30 +469,39 @@ brc_addlist(const char *fname) } int -brc_unread_time(time4_t ftime, int bnum, const time4_t *blist) +brc_unread_time(int bid, time4_t ftime) { - int n; + int i; + int bnum; + const time4_t *blist; if (ftime <= brc_expire_time) /* too old */ return 0; + if (brc_currbid && bid == brc_currbid) { + blist = brc_list; + bnum = brc_num; + } else { + blist = brc_find_record(bid, &bnum); + } + if (bnum <= 0) return 1; - for (n = 0; n < bnum; n++) { /* using linear search */ - if (ftime > blist[n]) + for (i = 0; i < bnum; i++) { /* using linear search */ + if (ftime > blist[i]) return 1; - else if (ftime == blist[n]) + else if (ftime == blist[i]) return 0; } return 0; } int -brc_unread(const char *fname, int bnum, const time4_t *blist) +brc_unread(int bid, const char *fname) { int ftime; ftime = atoi(&fname[2]); /* this will get the time of the file created */ - return brc_unread_time(ftime, bnum, blist); + return brc_unread_time(bid, ftime); } |