/[LeafOK_CVS]/lbbs/src/section_list.c
ViewVC logotype

Diff of /lbbs/src/section_list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.7 by sysadm, Thu May 22 06:20:47 2025 UTC Revision 1.11 by sysadm, Fri May 23 07:06:57 2025 UTC
# Line 309  ARTICLE *article_block_find_by_index(int Line 309  ARTICLE *article_block_find_by_index(int
309          return (p_block->articles + (index % ARTICLE_PER_BLOCK));          return (p_block->articles + (index % ARTICLE_PER_BLOCK));
310  }  }
311    
312  SECTION_LIST *section_list_create(const char *sname, const char *stitle, const char *master_name)  SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name)
313  {  {
314          SECTION_LIST *p_section;          SECTION_LIST *p_section;
315    
# Line 343  SECTION_LIST *section_list_create(const Line 343  SECTION_LIST *section_list_create(const
343    
344          p_section = p_section_list_pool + section_list_count;          p_section = p_section_list_pool + section_list_count;
345    
346            p_section->sid = sid;
347    
348          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));          strncpy(p_section->sname, sname, sizeof(p_section->sname - 1));
349          p_section->sname[sizeof(p_section->sname - 1)] = '\0';          p_section->sname[sizeof(p_section->sname - 1)] = '\0';
350    
# Line 368  SECTION_LIST *section_list_create(const Line 370  SECTION_LIST *section_list_create(const
370  void section_list_reset_articles(SECTION_LIST *p_section)  void section_list_reset_articles(SECTION_LIST *p_section)
371  {  {
372          p_section->article_count = 0;          p_section->article_count = 0;
373            p_section->topic_count = 0;
374            p_section->visible_article_count = 0;
375            p_section->visible_topic_count = 0;
376          p_section->p_article_head = NULL;          p_section->p_article_head = NULL;
377          p_section->p_article_tail = NULL;          p_section->p_article_tail = NULL;
378    
379          p_section->page_count = 0;          p_section->page_count = 0;
380          p_section->last_page_article_count = 0;          p_section->last_page_visible_article_count = 0;
381  }  }
382    
383  void section_list_cleanup(void)  void section_list_cleanup(void)
# Line 431  int section_list_append_article(SECTION_ Line 436  int section_list_append_article(SECTION_
436                  return -1;                  return -1;
437          }          }
438    
439            if (p_section->article_count >= BBS_article_limit_per_section)
440            {
441                    log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid);
442                    return -2;
443            }
444    
445          if (p_article_block_pool->block_count == 0 ||          if (p_article_block_pool->block_count == 0 ||
446                  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)
447          {          {
# Line 468  int section_list_append_article(SECTION_ Line 479  int section_list_append_article(SECTION_
479          // Copy article data          // Copy article data
480          *p_article = *p_article_src;          *p_article = *p_article_src;
481    
482            if (p_article->visible)
483            {
484                    p_section->visible_article_count++;
485            }
486    
487          // Link appended article as tail node of topic bi-directional list          // Link appended article as tail node of topic bi-directional list
488          if (p_article->tid != 0)          if (p_article->tid != 0)
489          {          {
# Line 487  int section_list_append_article(SECTION_ Line 503  int section_list_append_article(SECTION_
503          }          }
504          else          else
505          {          {
506                    p_section->topic_count++;
507    
508                    if (p_article->visible)
509                    {
510                            p_section->visible_topic_count++;
511                    }
512    
513                  p_topic_head = p_article;                  p_topic_head = p_article;
514                  p_topic_tail = p_article;                  p_topic_tail = p_article;
515          }          }
# Line 509  int section_list_append_article(SECTION_ Line 532  int section_list_append_article(SECTION_
532          p_section->p_article_tail = p_article;          p_section->p_article_tail = p_article;
533    
534          // Update page          // Update page
535          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) ||
536                    p_section->article_count == 1)
537          {          {
538                  p_section->p_page_first_article[p_section->page_count] = p_article;                  p_section->p_page_first_article[p_section->page_count] = p_article;
539                  p_section->page_count++;                  p_section->page_count++;
540                  p_section->last_page_article_count = 0;                  p_section->last_page_visible_article_count = 0;
541            }
542    
543            if (p_article->visible)
544            {
545                    p_section->last_page_visible_article_count++;
546          }          }
         p_section->last_page_article_count++;  
547    
548          return 0;          return 0;
549  }  }
# Line 523  int section_list_append_article(SECTION_ Line 551  int section_list_append_article(SECTION_
551  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)
552  {  {
553          ARTICLE *p_article;          ARTICLE *p_article;
554            ARTICLE *p_reply;
555            int affected_count = 0;
556    
557          if (p_section == NULL)          if (p_section == NULL)
558          {          {
# Line 541  int section_list_set_article_visible(SEC Line 571  int section_list_set_article_visible(SEC
571                  return 0; // Already set                  return 0; // Already set
572          }          }
573    
574            if (visible == 0) // 1 -> 0
575            {
576                    p_section->visible_article_count--;
577    
578                    if (p_article->tid == 0)
579                    {
580                            p_section->visible_topic_count--;
581    
582                            // Set related visible replies to invisible
583                            for (p_reply = p_article->p_topic_next; p_reply->tid != 0; p_reply = p_reply->p_topic_next)
584                            {
585                                    if (p_reply->tid != aid)
586                                    {
587                                            log_error("Inconsistent tid = %d found in reply %d of topic %d\n", p_reply->tid, p_reply->aid, aid);
588                                            continue;
589                                    }
590    
591                                    if (p_reply->visible == 1)
592                                    {
593                                            p_reply->visible = 0;
594                                            p_section->visible_article_count--;
595                                            affected_count++;
596                                    }
597                            }
598                    }
599            }
600            else // 0 -> 1
601            {
602                    p_section->visible_article_count++;
603    
604                    if (p_article->tid == 0)
605                    {
606                            p_section->visible_topic_count++;
607                    }
608            }
609    
610          p_article->visible = visible;          p_article->visible = visible;
611            affected_count++;
612    
613            return affected_count;
614    }
615    
616    ARTICLE *section_list_find_article_with_offset(SECTION_LIST *p_section, int32_t aid, int32_t *p_page, int32_t *p_offset, ARTICLE **pp_next)
617    {
618            ARTICLE *p_article;
619            int left;
620            int right;
621            int mid;
622    
623            *p_page = -1;
624            *p_offset = -1;
625            *pp_next = NULL;
626    
627            if (p_section == NULL)
628            {
629                    log_error("section_list_find_article_with_offset() NULL pointer error\n");
630                    return NULL;
631            }
632    
633            if (p_section->article_count == 0) // empty
634            {
635                    *p_page = 0;
636                    *p_offset = 0;
637                    return NULL;
638            }
639    
640            left = 0;
641            right = p_section->page_count;
642    
643            // aid in the range [ head aid of pages[left], tail aid of pages[right - 1] ]
644            while (left < right - 1)
645            {
646                    // get page id no less than mid value of left page id and right page id
647                    mid = (left + right) / 2 + (right - left) % 2;
648    
649                    if (mid >= p_section->page_count)
650                    {
651                            log_error("page id (mid = %d) is out of boundary\n", mid);
652                            return NULL;
653                    }
654    
655                    if (aid < p_section->p_page_first_article[mid]->aid)
656                    {
657                            right = mid;
658                    }
659                    else
660                    {
661                            left = mid;
662                    }
663            }
664    
665            *p_page = left;
666    
667            p_article = p_section->p_page_first_article[*p_page];
668    
669            // p_section->p_page_first_article[*p_page]->aid <= aid < p_section->p_page_first_article[*p_page + 1]->aid
670            right = (*p_page == MAX(0, p_section->page_count - 1) ? INT32_MAX : p_section->p_page_first_article[*p_page + 1]->aid);
671    
672            // left will be the offset of article found or offset to insert
673            left = 0;
674    
675            while (aid > p_article->aid)
676            {
677                    p_article = p_article->p_next;
678                    left++;
679    
680                    if (aid == p_article->aid)
681                    {
682                            *pp_next = p_article->p_next;
683                            break;
684                    }
685    
686                    // over last article in the page
687                    if (p_article == p_section->p_article_head || p_article->aid >= right)
688                    {
689                            *pp_next = (p_article == p_section->p_article_head ? p_section->p_article_head : p_section->p_page_first_article[*p_page + 1]);
690                            *p_offset = left;
691                            return NULL; // not found
692                    }
693            }
694    
695            if (aid < p_article->aid)
696            {
697                    *pp_next = p_article;
698                    p_article = NULL; // not found
699            }
700            else // aid == p_article->aid
701            {
702                    *pp_next = p_article->p_next;
703            }
704    
705            *p_offset = left;
706    
707            return p_article;
708    }
709    
710    int section_list_calculate_page(SECTION_LIST *p_section, int32_t start_aid)
711    {
712            ARTICLE *p_article;
713            ARTICLE *p_next;
714            int32_t page;
715            int32_t offset;
716            int visible_article_count;
717            int page_head_set;
718    
719            if (p_section == NULL)
720            {
721                    log_error("section_list_calculate_page() NULL pointer error\n");
722                    return -1;
723            }
724    
725            if (p_section->article_count == 0) // empty
726            {
727                    p_section->page_count = 0;
728                    p_section->last_page_visible_article_count = 0;
729    
730                    return 0;
731            }
732    
733            if (start_aid > 0)
734            {
735                    p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next);
736                    if (p_article == NULL)
737                    {
738                            if (page < 0)
739                            {
740                                    return -1;
741                            }
742                            log_error("section_list_calculate_page() aid = %d not found in section sid = %d\n",
743                                              start_aid, p_section->sid);
744                            return -2;
745                    }
746    
747                    if (offset > 0)
748                    {
749                            p_article = p_section->p_page_first_article[page];
750                    }
751            }
752            else
753            {
754                    p_article = p_section->p_article_head;
755                    page = 0;
756                    offset = 0;
757            }
758    
759            visible_article_count = 0;
760            page_head_set = 0;
761    
762            do
763            {
764                    if (!page_head_set && visible_article_count == 0)
765                    {
766                            p_section->p_page_first_article[page] = p_article;
767                            page_head_set = 1;
768                    }
769    
770                    if (p_article->visible)
771                    {
772                            visible_article_count++;
773                    }
774    
775                    p_article = p_article->p_next;
776    
777                    // skip remaining invisible articles
778                    while (p_article->visible == 0 && p_article != p_section->p_article_head)
779                    {
780                            p_article = p_article->p_next;
781                    }
782    
783                    if (visible_article_count >= BBS_article_limit_per_page && p_article != p_section->p_article_head)
784                    {
785                            page++;
786                            visible_article_count = 0;
787                            page_head_set = 0;
788    
789                            if (page >= BBS_article_limit_per_section / BBS_article_limit_per_page && p_article != p_section->p_article_head)
790                            {
791                                    log_error("Count of page exceed limit in section %d\n", p_section->sid);
792                                    break;
793                            }
794                    }
795            } while (p_article != p_section->p_article_head);
796    
797            p_section->page_count = page + (visible_article_count > 0 ? 1 : 0);
798            p_section->last_page_visible_article_count = visible_article_count;
799    
800            return 0;
801    }
802    
803    int section_list_count_of_topic_articles(int32_t aid)
804    {
805            ARTICLE *p_article;
806            int article_count;
807    
808            p_article = article_block_find_by_aid(aid);
809            if (p_article == NULL)
810            {
811                    return 0; // Not found
812            }
813    
814            article_count = 0;
815    
816            do
817            {
818                    article_count++;
819                    p_article = p_article->p_topic_next;
820            } while (p_article->aid != aid);
821    
822            return article_count;
823    }
824    
825    int section_list_move_topic(SECTION_LIST *p_section_src, SECTION_LIST *p_section_dest, int32_t aid)
826    {
827            ARTICLE *p_article;
828            ARTICLE *p_next;
829            int32_t page;
830            int32_t offset;
831            int32_t move_article_count;
832            int32_t dest_article_count_old;
833            int32_t last_unaffected_aid_src;
834    
835            if (p_section_dest == NULL)
836            {
837                    log_error("section_list_move_topic() NULL pointer error\n");
838                    return -1;
839            }
840    
841            if ((p_article = section_list_find_article_with_offset(p_section_src, aid, &page, &offset, &p_next)) == NULL)
842            {
843                    log_error("section_list_move_topic() error: article %d not found in section %d\n", aid, p_section_src->sid);
844                    return -2;
845            }
846    
847            if (p_article->tid != 0)
848            {
849                    log_error("section_list_move_topic(aid = %d) error: article is not head of topic, tid = %d\n", aid, p_article->tid);
850                    return -2;
851            }
852    
853            last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid);
854    
855            move_article_count = section_list_count_of_topic_articles(aid);
856            if (move_article_count <= 0)
857            {
858                    log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid);
859                    return -2;
860            }
861    
862            if (p_section_dest->article_count + move_article_count > BBS_article_limit_per_section)
863            {
864                    log_error("section_list_move_topic() error: article_count %d reach limit in section %d\n",
865                                      p_section_dest->article_count + move_article_count, p_section_dest->sid);
866                    return -3;
867            }
868    
869            dest_article_count_old = p_section_dest->article_count;
870    
871            do
872            {
873                    if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL)
874                    {
875                            log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid);
876                            return -4;
877                    }
878    
879                    // Remove from bi-directional article list of src section
880                    if (p_section_src->p_article_head == p_article)
881                    {
882                            p_section_src->p_article_head = p_article->p_next;
883                    }
884                    if (p_section_src->p_article_tail == p_article)
885                    {
886                            p_section_src->p_article_tail = p_article->p_prior;
887                    }
888                    if (p_section_src->p_article_head == p_article) // || p_section_src->p_article_tail == p_article
889                    {
890                            p_section_src->p_article_head = NULL;
891                            p_section_src->p_article_tail = NULL;
892                    }
893    
894                    p_article->p_prior->p_next = p_article->p_next;
895                    p_article->p_next->p_prior = p_article->p_prior;
896    
897                    // Insert into bi-directional article list of dest section
898                    if (p_next == NULL) // empty section
899                    {
900                            p_section_dest->p_article_head = p_article;
901                            p_section_dest->p_article_tail = p_article;
902                            p_article->p_prior = p_article;
903                            p_article->p_next = p_article;
904                    }
905                    else
906                    {
907                            if (p_section_dest->p_article_head == p_next)
908                            {
909                                    if (p_article->aid < p_next->aid)
910                                    {
911                                            p_section_dest->p_article_head = p_article;
912                                    }
913                                    else // p_article->aid > p_next->aid
914                                    {
915                                            p_section_dest->p_article_tail = p_article;
916                                    }
917                            }
918    
919                            p_article->p_prior = p_next->p_prior;
920                            p_article->p_next = p_next;
921                            p_next->p_prior->p_next = p_article;
922                            p_next->p_prior = p_article;
923                    }
924    
925                    // Update article / topic counter of src / desc section
926                    p_section_src->article_count--;
927                    p_section_dest->article_count++;
928                    if (p_article->tid == 0)
929                    {
930                            p_section_src->topic_count--;
931                            p_section_dest->topic_count++;
932                    }
933    
934                    // Update visible article / topic counter of src / desc section
935                    if (p_article->visible)
936                    {
937                            p_section_src->visible_article_count--;
938                            p_section_dest->visible_article_count++;
939                            if (p_article->tid == 0)
940                            {
941                                    p_section_src->visible_topic_count--;
942                                    p_section_dest->visible_topic_count++;
943                            }
944                    }
945    
946                    // Update page for empty dest section
947                    if (p_section_dest->article_count == 1)
948                    {
949                            p_section_dest->p_page_first_article[0] = p_article;
950                            p_section_dest->page_count = 1;
951                            p_section_dest->last_page_visible_article_count = (p_article->visible ? 1 : 0);
952                    }
953    
954                    p_article = p_article->p_topic_next;
955            } while (p_article->aid != aid);
956    
957            if (p_section_dest->article_count - dest_article_count_old != move_article_count)
958            {
959                    log_error("section_list_move_topic() error: count of moved articles %d != %d\n",
960                                      p_section_dest->article_count - dest_article_count_old, move_article_count);
961            }
962    
963          // TODO:          // Re-calculate pages of both src and desc sections
964            if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0)
965            {
966                    log_error("section_list_calculate_page(section = %d, aid = %d) error at aid = %d\n", p_section_src->sid, last_unaffected_aid_src, aid);
967            }
968            if (section_list_calculate_page(p_section_dest, aid) < 0)
969            {
970                    log_error("section_list_calculate_page(section = %d, aid = %d) error\n", p_section_dest->sid, aid);
971            }
972    
973          return 1;          return move_article_count;
974  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1