| 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) |
| 471 |
// Copy article data |
// Copy article data |
| 472 |
*p_article = *p_article_src; |
*p_article = *p_article_src; |
| 473 |
|
|
| 474 |
|
if (p_article->visible) |
| 475 |
|
{ |
| 476 |
|
p_section->visible_article_count++; |
| 477 |
|
} |
| 478 |
|
|
| 479 |
// Link appended article as tail node of topic bi-directional list |
// Link appended article as tail node of topic bi-directional list |
| 480 |
if (p_article->tid != 0) |
if (p_article->tid != 0) |
| 481 |
{ |
{ |
| 495 |
} |
} |
| 496 |
else |
else |
| 497 |
{ |
{ |
| 498 |
|
p_section->topic_count++; |
| 499 |
|
|
| 500 |
|
if (p_article->visible) |
| 501 |
|
{ |
| 502 |
|
p_section->visible_topic_count++; |
| 503 |
|
} |
| 504 |
|
|
| 505 |
p_topic_head = p_article; |
p_topic_head = p_article; |
| 506 |
p_topic_tail = p_article; |
p_topic_tail = p_article; |
| 507 |
} |
} |
| 524 |
p_section->p_article_tail = p_article; |
p_section->p_article_tail = p_article; |
| 525 |
|
|
| 526 |
// Update page |
// Update page |
| 527 |
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) |
| 528 |
{ |
{ |
| 529 |
p_section->p_page_first_article[p_section->page_count] = p_article; |
p_section->p_page_first_article[p_section->page_count] = p_article; |
| 530 |
p_section->page_count++; |
p_section->page_count++; |
| 531 |
p_section->last_page_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 532 |
} |
} |
| 533 |
p_section->last_page_article_count++; |
p_section->last_page_visible_article_count++; |
| 534 |
|
|
| 535 |
return 0; |
return 0; |
| 536 |
} |
} |
| 538 |
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) |
| 539 |
{ |
{ |
| 540 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 541 |
|
ARTICLE *p_reply; |
| 542 |
|
int affected_count = 0; |
| 543 |
|
|
| 544 |
if (p_section == NULL) |
if (p_section == NULL) |
| 545 |
{ |
{ |
| 558 |
return 0; // Already set |
return 0; // Already set |
| 559 |
} |
} |
| 560 |
|
|
| 561 |
|
if (visible == 0) // 1 -> 0 |
| 562 |
|
{ |
| 563 |
|
p_section->visible_article_count--; |
| 564 |
|
|
| 565 |
|
if (p_article->tid == 0) |
| 566 |
|
{ |
| 567 |
|
p_section->visible_topic_count--; |
| 568 |
|
|
| 569 |
|
// Set related visible replies to invisible |
| 570 |
|
for (p_reply = p_article->p_topic_next; p_reply->tid != 0; p_reply = p_reply->p_topic_next) |
| 571 |
|
{ |
| 572 |
|
if (p_reply->tid != aid) |
| 573 |
|
{ |
| 574 |
|
log_error("Inconsistent tid = %d found in reply %d of topic %d\n", p_reply->tid, p_reply->aid, aid); |
| 575 |
|
continue; |
| 576 |
|
} |
| 577 |
|
|
| 578 |
|
if (p_reply->visible == 1) |
| 579 |
|
{ |
| 580 |
|
p_reply->visible = 0; |
| 581 |
|
p_section->visible_article_count--; |
| 582 |
|
affected_count++; |
| 583 |
|
} |
| 584 |
|
} |
| 585 |
|
} |
| 586 |
|
} |
| 587 |
|
else // 0 -> 1 |
| 588 |
|
{ |
| 589 |
|
p_section->visible_article_count++; |
| 590 |
|
|
| 591 |
|
if (p_article->tid == 0) |
| 592 |
|
{ |
| 593 |
|
p_section->visible_topic_count++; |
| 594 |
|
} |
| 595 |
|
} |
| 596 |
|
|
| 597 |
p_article->visible = visible; |
p_article->visible = visible; |
| 598 |
|
affected_count++; |
| 599 |
|
|
| 600 |
|
return affected_count; |
| 601 |
|
} |
| 602 |
|
|
| 603 |
|
ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset) |
| 604 |
|
{ |
| 605 |
|
ARTICLE *p_article; |
| 606 |
|
int left; |
| 607 |
|
int right; |
| 608 |
|
int mid; |
| 609 |
|
|
| 610 |
|
*p_page = -1; |
| 611 |
|
*p_offset = -1; |
| 612 |
|
|
| 613 |
|
if (p_section == NULL) |
| 614 |
|
{ |
| 615 |
|
log_error("section_list_find_article_with_offset() NULL pointer error\n"); |
| 616 |
|
return NULL; |
| 617 |
|
} |
| 618 |
|
|
| 619 |
// TODO: |
if (p_section->article_count == 0) // empty |
| 620 |
|
{ |
| 621 |
|
*p_page = 0; |
| 622 |
|
*p_offset = 0; |
| 623 |
|
return NULL; |
| 624 |
|
} |
| 625 |
|
|
| 626 |
|
left = 0; |
| 627 |
|
right = p_section->page_count; |
| 628 |
|
|
| 629 |
|
// aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ] |
| 630 |
|
while (left < right - 1) |
| 631 |
|
{ |
| 632 |
|
// get page id no less than mid value of left page id and right page id |
| 633 |
|
mid = (left + right) / 2 + (right - left) % 2; |
| 634 |
|
|
| 635 |
return 1; |
if (mid >= p_section->page_count) |
| 636 |
|
{ |
| 637 |
|
log_error("page id (mid = %d) is out of boundary\n", mid); |
| 638 |
|
return NULL; |
| 639 |
|
} |
| 640 |
|
|
| 641 |
|
if (aid < p_section->p_page_first_article[mid]->aid) |
| 642 |
|
{ |
| 643 |
|
right = mid; |
| 644 |
|
} |
| 645 |
|
else |
| 646 |
|
{ |
| 647 |
|
left = mid; |
| 648 |
|
} |
| 649 |
|
} |
| 650 |
|
|
| 651 |
|
*p_page = left; |
| 652 |
|
|
| 653 |
|
p_article = p_section->p_page_first_article[*p_page]; |
| 654 |
|
|
| 655 |
|
// p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid |
| 656 |
|
right = (*p_page == p_section->page_count - 1 ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid); |
| 657 |
|
|
| 658 |
|
// left will be the offset of article found or offset to insert |
| 659 |
|
left = 0; |
| 660 |
|
|
| 661 |
|
while (1) |
| 662 |
|
{ |
| 663 |
|
if (aid == p_article->aid) // found |
| 664 |
|
{ |
| 665 |
|
break; |
| 666 |
|
} |
| 667 |
|
else if (aid < p_article->aid) // not exist |
| 668 |
|
{ |
| 669 |
|
p_article = NULL; |
| 670 |
|
break; |
| 671 |
|
} |
| 672 |
|
|
| 673 |
|
// aid > p_article->aid |
| 674 |
|
p_article = p_article->p_next; |
| 675 |
|
left++; |
| 676 |
|
|
| 677 |
|
// over last article in the page |
| 678 |
|
if (p_article == p_section->p_article_head || p_article->aid >= right) |
| 679 |
|
{ |
| 680 |
|
p_article = NULL; |
| 681 |
|
break; |
| 682 |
|
} |
| 683 |
|
} |
| 684 |
|
|
| 685 |
|
*p_offset = left; |
| 686 |
|
|
| 687 |
|
return p_article; |
| 688 |
|
} |
| 689 |
|
|
| 690 |
|
int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid) |
| 691 |
|
{ |
| 692 |
|
// ARTICLE *p_article; |
| 693 |
|
|
| 694 |
|
if (p_section == NULL) |
| 695 |
|
{ |
| 696 |
|
log_error("section_list_calculate_page() NULL pointer error\n"); |
| 697 |
|
return -1; |
| 698 |
|
} |
| 699 |
|
|
| 700 |
|
return 0; |
| 701 |
} |
} |