home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / nn.tar / nn-6.5.1 / sort.c < prev    next >
C/C++ Source or Header  |  1995-04-29  |  10KB  |  371 lines

  1. /*
  2.  *    (c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
  3.  *
  4.  *    Article sorting.
  5.  */
  6.  
  7. #include "config.h"
  8. #include "articles.h"
  9.  
  10. /* sort.c */
  11.  
  12. static order_subj_date __APROTO((article_header **ah1, article_header **ah2));
  13. static order_date_subj_date __APROTO((article_header **ah1, article_header **ah2));
  14. static order_arrival __APROTO((article_header **a, article_header **b));
  15. static order_date __APROTO((register article_header **ah1, register article_header **ah2));
  16. static order_from_date __APROTO((register article_header **ah1, register article_header **ah2));
  17. static flag_type article_equal __APROTO((article_header **ah1, article_header **ah2));
  18.  
  19. static int order_arrival();
  20.  
  21. #ifdef BAD_ORDER_SUBJ_DATE
  22. /* If one article's subject is identical to the first part of another
  23.  article, the two subjects will still be considered identical if the
  24.  length of the shorter subject is at least the limit set by this variable. */
  25. export int subject_match_limit = 20;     /* "strncmp" limit for subjects */
  26. #else
  27. /* Subjects are considered identical if their first s-m-l characters match */
  28. export int subject_match_limit = 256;     /* "strncmp" limit for subjects */
  29. #endif
  30.  
  31. export int match_skip_prefix = 0; /* skip first N bytes in matches */
  32. export int match_parts_equal = 0; /* match digits as equal */
  33. export int match_parts_begin = 4; /* require N characters for part match */
  34.  
  35. export int sort_mode = 1;        /* default sort mode */
  36.  
  37. extern int bypass_consolidation;
  38.  
  39. /*
  40.  *    String matching macroes.
  41.  *
  42.  *     MATCH_DROP(t, a) and MATCH_DROP(t, b) must both be proven false
  43.  *    before MATCH_?? (t, a, b) is used.
  44.  */
  45.  
  46. #define    MATCH_DROP(table, c) ( c & 0200 || table[c] == 0 )
  47. #define MATCH_EQ(table, a, b) ( a == b || table[a] == table[b] )
  48. #define MATCH_LS_EQ(table, a, b) ( a <= b || table[a] <= table[b] )
  49. #define MATCH_LS(table, a, b) ( a < b || table[a] < table[b] )
  50. #define    MATCH_CMP(table, a, b) (table[a] - table[b])
  51.  
  52. static char match_subject[128] = {
  53.  
  54. /*  NUL SOH STX ETX EOT ENQ ACK BEL BS  TAB NL  VT  FF  CR  SO  SI  */
  55.     00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
  56.  
  57. /*  DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM  SUB ESC FS  GS  RS  US  */
  58.     00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
  59.  
  60. /*  SP  !   "   #   $   %   &   '   (   )   *   +   ,   -   .   /   */
  61.     00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 99, 00, 00, 00, 00,
  62. /*                                              ^^                  */
  63.  
  64. /*  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ?   */
  65.      1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 00, 00, 00, 00, 00, 00,
  66.  
  67. /*  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   */
  68.     00, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  69.  
  70. /*  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _   */
  71.     26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 00, 00,
  72.  
  73. /*  `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   */
  74.     00, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  75.  
  76. /*  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~   DEL */
  77.     26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 00, 00
  78. };
  79.  
  80.  
  81. static int
  82. order_subj_date(ah1, ah2)
  83. article_header **ah1, **ah2;
  84. {
  85.     register char *a = (**ah1).subject, *b = (**ah2).subject;
  86.     register char ca, cb;
  87.     register int p, len;
  88.  
  89.     if (match_skip_prefix) {
  90.     if ((int)(**ah1).subj_length > match_skip_prefix) {
  91.         if ((int)(**ah2).subj_length > match_skip_prefix) {
  92.         a += match_skip_prefix;
  93.         b += match_skip_prefix;
  94.         } else
  95.         return 1;
  96.     } else {
  97.         if ((int)(**ah2).subj_length > match_skip_prefix) {
  98.         return -1;
  99.         }
  100.     }
  101.     }
  102.  
  103. #ifdef BAD_ORDER_SUBJ_DATE
  104.     for (len = 0; ; len++, a++, b++) {
  105. #else
  106.     for (len = subject_match_limit; --len >= 0; a++, b++) {
  107. #endif
  108.     while ((ca = *a) && MATCH_DROP(match_subject, ((int8)ca))) a++;
  109.     while ((cb = *b) && MATCH_DROP(match_subject, ((int8)cb))) b++;
  110.  
  111.     if (ca == NUL) {
  112.         if (cb == NUL) break;
  113. #ifdef BAD_ORDER_SUBJ_DATE
  114.         if (len >= subject_match_limit) break;
  115. #endif
  116.         return -1;
  117.     }
  118.  
  119.     if (cb == NUL) {
  120. #ifdef BAD_ORDER_SUBJ_DATE
  121.         if (len >= subject_match_limit) break;
  122. #endif
  123.         return 1;
  124.     }
  125.  
  126.     if ((p = MATCH_CMP(match_subject, (int8)ca, (int8)cb))) return p;
  127.     }
  128.  
  129.     if ((**ah1).t_stamp > (**ah2).t_stamp) return 1;
  130.     if ((**ah1).t_stamp < (**ah2).t_stamp) return -1;
  131.     return order_arrival(ah1, ah2);
  132. }
  133.  
  134. /* data_subj_date can only be used after root_t_stamp is set */
  135.  
  136. static int
  137. order_date_subj_date(ah1, ah2)
  138. article_header **ah1, **ah2;
  139. {
  140.     register time_stamp t1 = (**ah1).root_t_stamp;
  141.     register time_stamp t2 = (**ah2).root_t_stamp;
  142.  
  143.     if (t1 > t2) return 1;
  144.     if (t1 < t2) return -1;
  145.     return order_subj_date(ah1, ah2);    /* get original order */
  146. }
  147.  
  148.  
  149. static int
  150. order_arrival(a, b)
  151. article_header **a, **b;
  152. {
  153.     register long i;
  154.  
  155.     if ((i = (int)((*a)->a_number - (*b)->a_number)) == 0)
  156.     i = (*a)->fpos - (*b)->fpos;
  157.  
  158.     return (i > 0) ? 1 : (i < 0) ? -1 : 0;
  159. }
  160.  
  161. static int
  162. order_date(ah1, ah2)
  163. register article_header **ah1, **ah2;
  164. {
  165.     if ((**ah1).t_stamp > (**ah2).t_stamp) return 1;
  166.     if ((**ah1).t_stamp < (**ah2).t_stamp) return -1;
  167.     return order_arrival(ah1, ah2);
  168. }
  169.  
  170. static int
  171. order_from_date(ah1, ah2)
  172. register article_header **ah1, **ah2;
  173. {
  174.     register int i;
  175.  
  176.     if ((i = strcmp((**ah1).sender, (**ah2).sender))) return i;
  177.     return order_date(ah1, ah2);
  178. }
  179.  
  180. static flag_type article_equal(ah1, ah2) /* ah1.hdr == ah2.hdr */
  181. article_header **ah1, **ah2;
  182. {
  183.     register char *a = (**ah1).subject, *b = (**ah2).subject;
  184.     register int len;
  185.  
  186.     if (match_skip_prefix) {
  187.     if ((int)(**ah1).subj_length > match_skip_prefix) {
  188.         if ((int)(**ah2).subj_length > match_skip_prefix) {
  189.         a += match_skip_prefix;
  190.         b += match_skip_prefix;
  191.         }
  192.     }
  193.     }
  194.  
  195.     for (len = 0;; len++, a++, b++) {
  196.     while (*a && MATCH_DROP(match_subject, (int8)*a)) a++;
  197.     while (*b && MATCH_DROP(match_subject, (int8)*b)) b++;
  198.  
  199.     if (*a == NUL) {
  200.         if (*b == NUL) break;
  201.         if (len >= subject_match_limit) return A_ALMOST_SAME;
  202.         return 0;
  203.     }
  204.  
  205.     if (*b == NUL) {
  206.         if (len >= subject_match_limit) return A_ALMOST_SAME;
  207.         return 0;
  208.     }
  209.  
  210.     if (!MATCH_EQ(match_subject, (int8)*a, (int8)*b)) {
  211.         if (len >= subject_match_limit)
  212.         return A_ALMOST_SAME;
  213.         if (len >= match_parts_begin && match_parts_equal &&
  214.                isdigit(*a) && isdigit(*b))
  215.         return A_ALMOST_SAME;
  216.         return 0;
  217.     }
  218.     }
  219.  
  220.     return A_SAME;
  221. }
  222.  
  223. void
  224. sort_articles(mode)
  225. int mode;
  226. {
  227.     register article_header **app;
  228.     register long n;
  229.     register flag_type same;
  230.     fct_type cmp;
  231.  
  232.     if (n_articles <= 0) return;
  233.  
  234.     for (n = n_articles; --n >= 0;)
  235.     articles[n]->flag &= ~(A_SAME|A_ALMOST_SAME|A_NEXT_SAME|A_ROOT_ART);
  236.  
  237.     if (n_articles == 1) {
  238.     articles[0]->flag |= A_ROOT_ART;
  239.     return;
  240.     }
  241.  
  242.     if (mode == -1) mode = sort_mode;
  243.  
  244.     switch (mode) {
  245.      case -2:            /* restore original ordering for update */
  246.     cmp = order_arrival;
  247.     break;
  248.      default:
  249.      case 0:            /* arrival (no sort) */
  250.     cmp = order_arrival;
  251.     bypass_consolidation = 1;
  252.     break;
  253.      case 1:            /* date-subject-date */
  254.      case 2:            /* subject-date */
  255.     cmp = order_subj_date;
  256.     break;
  257.      case 3:            /* date only */
  258.     cmp = order_date;
  259.     bypass_consolidation = 1;
  260.     break;
  261.      case 4:            /* sender-date */
  262.     cmp = order_from_date;
  263.     bypass_consolidation = 1;
  264.     break;
  265.     }
  266.  
  267.     quicksort(articles, n_articles, article_header *, cmp);
  268.  
  269.     articles[0]->root_t_stamp = articles[0]->t_stamp;
  270.     articles[0]->flag |= A_ROOT_ART;
  271.     for (n = n_articles - 1, app = articles + 1; --n >= 0; app++) {
  272.     if ((same = article_equal(app, app - 1))) {
  273.         app[0]->root_t_stamp = app[-1]->root_t_stamp;
  274.         app[0]->flag |= same;
  275.         app[-1]->flag |= A_NEXT_SAME;
  276.     } else {
  277.         app[0]->root_t_stamp = app[0]->t_stamp;
  278.         app[0]->flag |= A_ROOT_ART;
  279.     }
  280.     }
  281.  
  282.     if (mode == 1)
  283.     quicksort(articles, n_articles, article_header *, order_date_subj_date);
  284. }
  285.  
  286. /*
  287.  *    If articles are not sorted via sort_articles, they must still be
  288.  *    marked with proper attributes (e.g. A_ROOT_ART) via no_sort_articles.
  289.  */
  290.  
  291. void
  292. no_sort_articles()
  293. {
  294.     register article_number n;
  295.     register article_header *ah;
  296.  
  297.     for (n = n_articles; --n >= 0;) {
  298.     ah = articles[n];
  299.     ah->flag &= ~(A_SAME|A_ALMOST_SAME|A_NEXT_SAME|A_ROOT_ART);
  300.     ah->flag |= A_ROOT_ART;
  301.     }
  302.     bypass_consolidation = 1;
  303. }
  304.  
  305. /*
  306.  * Eliminate articles with the A_KILL flag set preserving the present ordering.
  307.  * This will only release the last entries in the articles array.
  308.  * Neither strings nor articles headers are released.
  309.  */
  310.  
  311. int
  312. elim_articles(list, list_lgt)
  313. register article_number *list;
  314. int list_lgt;
  315. {
  316.     register article_header **srca, **desta;
  317.     register article_number n, count;
  318.     register flag_type same;
  319.     int changed, llen;
  320.  
  321.     count = 0;
  322.     changed = 0, llen = 0;
  323.     for (n = 0, srca = desta = articles; n < n_articles; n++, srca++) {
  324.     if ((*srca)->attr == A_KILL) {
  325.         if (list_lgt > 0) {
  326.         if (n < *list) {
  327.             if (llen) changed = 1;
  328.         } else
  329.         if (n == *list) {
  330.             if (llen) {
  331.             llen++;
  332.             list_lgt--;
  333.             *list++ = -1;
  334.             } else
  335.             ++(*list);
  336.             changed = 1;
  337.         }
  338.         }
  339.         continue;
  340.     }
  341.     if (list_lgt > 0 && n == *list) {
  342.         *list++ = count;
  343.         list_lgt--;
  344.         llen++;
  345.     }
  346.     count++;
  347.     *desta++ = *srca;
  348.     }
  349.     if (list_lgt > 0) {
  350.     if (!llen) *list = 0;
  351.     changed = 1;
  352.     }
  353.     n_articles = count;
  354.  
  355.     if (changed && n_articles > 0) {
  356.     srca = articles;
  357.     srca[0]->flag &= ~(A_SAME|A_ALMOST_SAME|A_NEXT_SAME);
  358.     srca[0]->flag |= A_ROOT_ART;
  359.     for (n = n_articles - 1, srca++; --n >= 0; srca++) {
  360.         srca[0]->flag &= ~(A_SAME|A_ALMOST_SAME|A_NEXT_SAME|A_ROOT_ART);
  361.         if ((same = article_equal(srca, srca - 1))) {
  362.         srca[0]->flag |= same;
  363.         srca[-1]->flag |= A_NEXT_SAME;
  364.         } else
  365.         srca[0]->flag |= A_ROOT_ART;
  366.     }
  367.     }
  368.  
  369.     return changed;
  370. }
  371.