| 368 |
void section_list_reset_articles(SECTION_LIST *p_section) |
void section_list_reset_articles(SECTION_LIST *p_section) |
| 369 |
{ |
{ |
| 370 |
p_section->article_count = 0; |
p_section->article_count = 0; |
| 371 |
|
p_section->topic_count = 0; |
| 372 |
|
p_section->visible_article_count = 0; |
| 373 |
|
p_section->visible_topic_count = 0; |
| 374 |
p_section->p_article_head = NULL; |
p_section->p_article_head = NULL; |
| 375 |
p_section->p_article_tail = NULL; |
p_section->p_article_tail = NULL; |
| 376 |
|
|
| 377 |
p_section->page_count = 0; |
p_section->page_count = 0; |
| 378 |
p_section->last_page_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 379 |
} |
} |
| 380 |
|
|
| 381 |
void section_list_cleanup(void) |
void section_list_cleanup(void) |
| 434 |
return -1; |
return -1; |
| 435 |
} |
} |
| 436 |
|
|
| 437 |
|
if (p_section->article_count >= BBS_article_limit_per_section) |
| 438 |
|
{ |
| 439 |
|
log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid); |
| 440 |
|
return -2; |
| 441 |
|
} |
| 442 |
|
|
| 443 |
if (p_article_block_pool->block_count == 0 || |
if (p_article_block_pool->block_count == 0 || |
| 444 |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK) |
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count >= ARTICLE_PER_BLOCK) |
| 445 |
{ |
{ |
| 477 |
// Copy article data |
// Copy article data |
| 478 |
*p_article = *p_article_src; |
*p_article = *p_article_src; |
| 479 |
|
|
| 480 |
|
if (p_article->visible) |
| 481 |
|
{ |
| 482 |
|
p_section->visible_article_count++; |
| 483 |
|
} |
| 484 |
|
|
| 485 |
// Link appended article as tail node of topic bi-directional list |
// Link appended article as tail node of topic bi-directional list |
| 486 |
if (p_article->tid != 0) |
if (p_article->tid != 0) |
| 487 |
{ |
{ |
| 501 |
} |
} |
| 502 |
else |
else |
| 503 |
{ |
{ |
| 504 |
|
p_section->topic_count++; |
| 505 |
|
|
| 506 |
|
if (p_article->visible) |
| 507 |
|
{ |
| 508 |
|
p_section->visible_topic_count++; |
| 509 |
|
} |
| 510 |
|
|
| 511 |
p_topic_head = p_article; |
p_topic_head = p_article; |
| 512 |
p_topic_tail = p_article; |
p_topic_tail = p_article; |
| 513 |
} |
} |
| 530 |
p_section->p_article_tail = p_article; |
p_section->p_article_tail = p_article; |
| 531 |
|
|
| 532 |
// Update page |
// Update page |
| 533 |
if (p_section->last_page_article_count % BBS_article_limit_per_page == 0) |
if (p_article->visible && p_section->last_page_visible_article_count % BBS_article_limit_per_page == 0) |
| 534 |
{ |
{ |
| 535 |
p_section->p_page_first_article[p_section->page_count] = p_article; |
p_section->p_page_first_article[p_section->page_count] = p_article; |
| 536 |
p_section->page_count++; |
p_section->page_count++; |
| 537 |
p_section->last_page_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 538 |
|
} |
| 539 |
|
|
| 540 |
|
if (p_article->visible) |
| 541 |
|
{ |
| 542 |
|
p_section->last_page_visible_article_count++; |
| 543 |
} |
} |
|
p_section->last_page_article_count++; |
|
| 544 |
|
|
| 545 |
return 0; |
return 0; |
| 546 |
} |
} |
| 548 |
int section_list_set_article_visible(SECTION_LIST *p_section, int32_t aid, int8_t visible) |
int section_list_set_article_visible(SECTION_LIST *p_section, int32_t aid, int8_t visible) |
| 549 |
{ |
{ |
| 550 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 551 |
|
ARTICLE *p_reply; |
| 552 |
|
int affected_count = 0; |
| 553 |
|
|
| 554 |
if (p_section == NULL) |
if (p_section == NULL) |
| 555 |
{ |
{ |
| 568 |
return 0; // Already set |
return 0; // Already set |
| 569 |
} |
} |
| 570 |
|
|
| 571 |
|
if (visible == 0) // 1 -> 0 |
| 572 |
|
{ |
| 573 |
|
p_section->visible_article_count--; |
| 574 |
|
|
| 575 |
|
if (p_article->tid == 0) |
| 576 |
|
{ |
| 577 |
|
p_section->visible_topic_count--; |
| 578 |
|
|
| 579 |
|
// Set related visible replies to invisible |
| 580 |
|
for (p_reply = p_article->p_topic_next; p_reply->tid != 0; p_reply = p_reply->p_topic_next) |
| 581 |
|
{ |
| 582 |
|
if (p_reply->tid != aid) |
| 583 |
|
{ |
| 584 |
|
log_error("Inconsistent tid = %d found in reply %d of topic %d\n", p_reply->tid, p_reply->aid, aid); |
| 585 |
|
continue; |
| 586 |
|
} |
| 587 |
|
|
| 588 |
|
if (p_reply->visible == 1) |
| 589 |
|
{ |
| 590 |
|
p_reply->visible = 0; |
| 591 |
|
p_section->visible_article_count--; |
| 592 |
|
affected_count++; |
| 593 |
|
} |
| 594 |
|
} |
| 595 |
|
} |
| 596 |
|
} |
| 597 |
|
else // 0 -> 1 |
| 598 |
|
{ |
| 599 |
|
p_section->visible_article_count++; |
| 600 |
|
|
| 601 |
|
if (p_article->tid == 0) |
| 602 |
|
{ |
| 603 |
|
p_section->visible_topic_count++; |
| 604 |
|
} |
| 605 |
|
} |
| 606 |
|
|
| 607 |
p_article->visible = visible; |
p_article->visible = visible; |
| 608 |
|
affected_count++; |
| 609 |
|
|
| 610 |
|
return affected_count; |
| 611 |
|
} |
| 612 |
|
|
| 613 |
|
ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset) |
| 614 |
|
{ |
| 615 |
|
ARTICLE *p_article; |
| 616 |
|
int left; |
| 617 |
|
int right; |
| 618 |
|
int mid; |
| 619 |
|
|
| 620 |
|
*p_page = -1; |
| 621 |
|
*p_offset = -1; |
| 622 |
|
|
| 623 |
|
if (p_section == NULL) |
| 624 |
|
{ |
| 625 |
|
log_error("section_list_find_article_with_offset() NULL pointer error\n"); |
| 626 |
|
return NULL; |
| 627 |
|
} |
| 628 |
|
|
| 629 |
|
if (p_section->article_count == 0) // empty |
| 630 |
|
{ |
| 631 |
|
*p_page = 0; |
| 632 |
|
*p_offset = 0; |
| 633 |
|
return NULL; |
| 634 |
|
} |
| 635 |
|
|
| 636 |
|
left = 0; |
| 637 |
|
right = p_section->page_count; |
| 638 |
|
|
| 639 |
|
// aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ] |
| 640 |
|
while (left < right - 1) |
| 641 |
|
{ |
| 642 |
|
// get page id no less than mid value of left page id and right page id |
| 643 |
|
mid = (left + right) / 2 + (right - left) % 2; |
| 644 |
|
|
| 645 |
|
if (mid >= p_section->page_count) |
| 646 |
|
{ |
| 647 |
|
log_error("page id (mid = %d) is out of boundary\n", mid); |
| 648 |
|
return NULL; |
| 649 |
|
} |
| 650 |
|
|
| 651 |
|
if (aid < p_section->p_page_first_article[mid]->aid) |
| 652 |
|
{ |
| 653 |
|
right = mid; |
| 654 |
|
} |
| 655 |
|
else |
| 656 |
|
{ |
| 657 |
|
left = mid; |
| 658 |
|
} |
| 659 |
|
} |
| 660 |
|
|
| 661 |
// TODO: |
*p_page = left; |
| 662 |
|
|
| 663 |
return 1; |
p_article = p_section->p_page_first_article[*p_page]; |
| 664 |
|
|
| 665 |
|
// p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid |
| 666 |
|
right = (*p_page == p_section->page_count - 1 ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid); |
| 667 |
|
|
| 668 |
|
// left will be the offset of article found or offset to insert |
| 669 |
|
left = 0; |
| 670 |
|
|
| 671 |
|
while (1) |
| 672 |
|
{ |
| 673 |
|
if (aid == p_article->aid) // found |
| 674 |
|
{ |
| 675 |
|
break; |
| 676 |
|
} |
| 677 |
|
else if (aid < p_article->aid) // not exist |
| 678 |
|
{ |
| 679 |
|
p_article = NULL; |
| 680 |
|
break; |
| 681 |
|
} |
| 682 |
|
|
| 683 |
|
// aid > p_article->aid |
| 684 |
|
p_article = p_article->p_next; |
| 685 |
|
left++; |
| 686 |
|
|
| 687 |
|
// over last article in the page |
| 688 |
|
if (p_article == p_section->p_article_head || p_article->aid >= right) |
| 689 |
|
{ |
| 690 |
|
p_article = NULL; |
| 691 |
|
break; |
| 692 |
|
} |
| 693 |
|
} |
| 694 |
|
|
| 695 |
|
*p_offset = left; |
| 696 |
|
|
| 697 |
|
return p_article; |
| 698 |
|
} |
| 699 |
|
|
| 700 |
|
int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid) |
| 701 |
|
{ |
| 702 |
|
ARTICLE *p_article; |
| 703 |
|
int32_t page; |
| 704 |
|
int32_t offset; |
| 705 |
|
int visible_article_count; |
| 706 |
|
int page_head_set; |
| 707 |
|
|
| 708 |
|
if (p_section == NULL) |
| 709 |
|
{ |
| 710 |
|
log_error("section_list_calculate_page() NULL pointer error\n"); |
| 711 |
|
return -1; |
| 712 |
|
} |
| 713 |
|
|
| 714 |
|
p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset); |
| 715 |
|
if (p_article == NULL) |
| 716 |
|
{ |
| 717 |
|
if (page < 0) |
| 718 |
|
{ |
| 719 |
|
return 0; |
| 720 |
|
} |
| 721 |
|
|
| 722 |
|
log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n", start_aid, p_section->sid); |
| 723 |
|
} |
| 724 |
|
|
| 725 |
|
if (offset > 0) |
| 726 |
|
{ |
| 727 |
|
p_article = p_section->p_page_first_article[page]; |
| 728 |
|
} |
| 729 |
|
|
| 730 |
|
visible_article_count = 0; |
| 731 |
|
page_head_set = 0; |
| 732 |
|
|
| 733 |
|
do |
| 734 |
|
{ |
| 735 |
|
if (!page_head_set && visible_article_count == 0) |
| 736 |
|
{ |
| 737 |
|
p_section->p_page_first_article[page] = p_article; |
| 738 |
|
page_head_set = 1; |
| 739 |
|
} |
| 740 |
|
|
| 741 |
|
if (p_article->visible) |
| 742 |
|
{ |
| 743 |
|
visible_article_count++; |
| 744 |
|
} |
| 745 |
|
|
| 746 |
|
p_article = p_article->p_next; |
| 747 |
|
|
| 748 |
|
// skip remaining invisible articles |
| 749 |
|
while (p_article->visible == 0 && p_article != p_section->p_article_head) |
| 750 |
|
{ |
| 751 |
|
p_article = p_article->p_next; |
| 752 |
|
} |
| 753 |
|
|
| 754 |
|
if (visible_article_count >= BBS_article_limit_per_page && p_article != p_section->p_article_head) |
| 755 |
|
{ |
| 756 |
|
page++; |
| 757 |
|
visible_article_count = 0; |
| 758 |
|
page_head_set = 0; |
| 759 |
|
|
| 760 |
|
if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head) |
| 761 |
|
{ |
| 762 |
|
log_error("Count of page exceed limit in section %d\n", p_section->sid); |
| 763 |
|
break; |
| 764 |
|
} |
| 765 |
|
} |
| 766 |
|
} while (p_article != p_section->p_article_head); |
| 767 |
|
|
| 768 |
|
p_section->page_count = page + (visible_article_count > 0 ? 1 : 0); |
| 769 |
|
p_section->last_page_visible_article_count = visible_article_count; |
| 770 |
|
|
| 771 |
|
return 0; |
| 772 |
} |
} |