home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume19 / rkive / part02 / format.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-29  |  7.2 KB  |  229 lines

  1. /*
  2. **
  3. ** This software is Copyright (c) 1989 by Kent Landfield.
  4. **
  5. ** Permission is hereby granted to copy, distribute or otherwise 
  6. ** use any part of this package as long as you do not try to make 
  7. ** money from it or pretend that you wrote it.  This copyright 
  8. ** notice must be maintained in any copy made.
  9. **
  10. **
  11. **  History:
  12. **    Creation: Tue Feb 21 08:52:35 CST 1989 due to necessity.
  13. **                                                               
  14. */
  15. #ifndef lint
  16. static char SID[] = "@(#)format.c    1.1 6/1/89";
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <sys/types.h>
  21. #include "article.h"
  22.  
  23. extern FILE *errfp;
  24.  
  25. char *itoa(n)
  26. int n;
  27. {
  28.     static char str[20];
  29.  
  30.     int i, c, j, sign;
  31.     
  32.     if ((sign = n) < 0) 
  33.     n = -n;
  34.  
  35.     i = 0;
  36.     do {
  37.         str[i++] = n % 10 + '0';
  38.     } while ((n /= 10) > 0);
  39.  
  40.     if (sign < 0)
  41.         str[i++] = '-';
  42.     str[i] = '\0';
  43.  
  44.     for (i = 0, j = strlen(str)-1; i < j; i++,j--) {
  45.         c = str[i];
  46.         str[i] = str[j];
  47.         str[j] = c;
  48.     }
  49.     return(str);
  50. }
  51.  
  52. char *add_string(ptr, member_str)
  53. char *ptr, *member_str;
  54. {
  55.     while(*member_str)
  56.         *ptr++ = *member_str++;
  57.     return(ptr);
  58. }
  59.  
  60.  
  61. char *basename(name)
  62.     char *name;
  63. {
  64.     char *p;
  65.     char *strrchr();
  66.     
  67.     if ((p = strrchr(name,'/')) == NULL)
  68.         return(name);
  69.     return(++p);
  70. }
  71.  
  72.  
  73. format_output(fp, frmptr, filename, default_type)
  74. FILE *fp;              /* Output file pointer                   */
  75. char *frmptr;          /* Selection Format pointer              */
  76. char *filename;        /* File name the info came from          */
  77. int default_type;      /* Where called from, Rkive or Article   */
  78. {
  79.     char *basename();
  80.     char *aptr, *cp, c;
  81.     char format[BUFSIZ];
  82.     void exit();
  83.  
  84.     /* Did the user specify a format to use   */
  85.     /* or should the default format be used ? */
  86.  
  87.     if (frmptr == NULL) {
  88.        if (default_type == ARCHIVE) 
  89.            (void) fprintf(fp,"%-s\t%-s\t%s\n",
  90.                  article.newsarticle, header.archive_name,
  91.                  article.description);
  92.        else
  93.            (void) fprintf(fp,"%-s\t%-s\n", filename, header.subject);
  94.        return;
  95.     }
  96.  
  97.     for (cp = format; cp < format + BUFSIZ; cp++)
  98.          *cp = '\0';
  99.  
  100.     aptr = frmptr;
  101.     cp = format;
  102.  
  103.     while (c = *aptr++) {
  104.         if (c == '%') {
  105.            switch (*aptr++) {
  106.                case '%':
  107.                    *cp++ = '%';
  108.                    continue;
  109.                case 'A':     /* Approved  */
  110.                    cp = add_string(cp, header.approved);
  111.                    continue;
  112.                case 'B':     /* Base name of the file path  */
  113.                    cp = add_string(cp, basename(filename));
  114.                    continue;
  115.                case 'C':     /* Supersedes  */
  116.                    cp = add_string(cp, header.supersedes);
  117.                    continue;
  118.                case 'D':     /* Date  */
  119.                    cp = add_string(cp, header.subdate);
  120.                    continue;
  121.                case 'F':     /* From */
  122.                    cp = add_string(cp, header.from);
  123.                    continue;
  124.                case 'G':     /* newGroups disk location */
  125.                    cp = add_string(cp, article.newsgroup);
  126.                    continue;
  127.                case 'K':     /* Keywords  */
  128.                    cp = add_string(cp, header.keywords);
  129.                    continue;
  130.                case 'L':     /* Lines */
  131.                    cp = add_string(cp, header.numlines);
  132.                    continue;
  133.                case 'M':     /* Message-ID */
  134.                    cp = add_string(cp, header.ident);
  135.                    continue;
  136.                case 'N':     /* Newsgroups */
  137.                    cp = add_string(cp, header.nbuf);
  138.                    continue;
  139.                case 'O':     /* Actual Archived filename */
  140.                    cp = add_string(cp, filename);
  141.                    continue;
  142.                case 'P':     /* Path  */
  143.                    cp = add_string(cp, header.path);
  144.                    continue;
  145.                case 'R':     /* References  */
  146.                    cp = add_string(cp, header.references);
  147.                    continue;
  148.                case 'S':     /* Subject  */
  149.                    cp = add_string(cp, header.subject);
  150.                    continue;
  151.                case 'T':     /* Subject Topic  */
  152.                    cp = add_string(cp, article.description);
  153.                    continue;
  154.                case 'V':     /* Volume-Issue article filename  */
  155.                    cp = add_string(cp, article.filename);
  156.                    continue;
  157.                case 'a':     /* Archive-name */
  158.                    cp = add_string(cp, header.archive_name);
  159.                    continue;
  160.                case 'b':     /* Submitted-by */
  161.                    cp = add_string(cp, header.submitted_by);
  162.                    continue;
  163.                case 'c':     /* Control  */
  164.                    cp = add_string(cp, header.ctlmsg);
  165.                    continue;
  166.                case 'd':     /* Distribution  */
  167.                    cp = add_string(cp, header.distribution);
  168.                    continue;
  169.                case 'e':     /* Expires  */
  170.                    cp = add_string(cp, header.expdate);
  171.                    continue;
  172.                case 'f':     /* Followup-to  */
  173.                    cp = add_string(cp, header.followup_to);
  174.                    continue;
  175.                case 'i':     /* issue (if archive) */
  176.                    cp = add_string(cp,itoa(article.issue));
  177.                    continue;
  178.                case 'l':     /* Author's logon address */
  179.                    cp = add_string(cp, article.author_signon);
  180.                    continue;
  181.                case 'n':     /* Author's name */
  182.                    cp = add_string(cp, article.author_name);
  183.                    continue;
  184.                case 'o':     /* Organization */
  185.                    cp = add_string(cp, header.organization);
  186.                    continue;
  187.                case 'p':     /* Posting-number */
  188.                    cp = add_string(cp, header.posting_num);
  189.                    continue;
  190.                case 'r':     /* Reply-to */
  191.                    cp = add_string(cp, header.replyto);
  192.                    continue;
  193.                case 's':     /* Sender  */
  194.                    cp = add_string(cp, header.sender);
  195.                    continue;
  196.                case 't':     /* Patch-To  */
  197.                    cp = add_string(cp, header.patch_to);
  198.                    continue;
  199.                case 'u':     /* Summary */
  200.                    cp = add_string(cp, header.summary);
  201.                    continue;
  202.                case 'v':     /* volume (if archive) */
  203.                    cp = add_string(cp,itoa(article.volume));
  204.                    continue;
  205.                case 'x':     /* Xref */
  206.                    cp = add_string(cp, header.xref);
  207.                    continue;
  208.                default:
  209.                   (void) fprintf(errfp, "invalid format - %c\n", *--aptr);
  210.                   exit(2);
  211.            }    /* end switch */
  212.         }
  213.         else if (c == '\134') {
  214.            switch (*aptr++) {
  215.                case 'n':
  216.                    *cp++ = '\n';
  217.                    continue;
  218.                case 't':
  219.                    *cp++ = '\t';
  220.                    continue;
  221.            }
  222.         }
  223.         *cp++ = c;
  224.      }  /* end while */
  225.  
  226.      (void) fprintf(fp, "%s\n", format);
  227.      return;
  228. }
  229.