home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / uucp / duucp-1.17 / AU-117b4-src.lha / src / lib / header.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-24  |  2.5 KB  |  162 lines

  1. /*
  2.  *  HEADER SCANNING ROUTINES
  3.  *
  4.  *  given a memory buffer terminated with \0 (that might be a file image),
  5.  *  do various header handling operations
  6.  */
  7.  
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "config.h"
  12.  
  13. Prototype int ScanHeader (const char *, const char *);
  14. Prototype char *GetHeader (const char *, const char *);
  15. Prototype char *DupHeader (const char *, const char *);
  16. Prototype char *ScanNext (void);
  17.  
  18. /*
  19.  *  scan a header, elements in the header (comma separated).  Blank line
  20.  *  ends header scan
  21.  */
  22.  
  23. static const char
  24.     *SHBase = NULL;
  25.  
  26. int
  27. ScanHeader (const char *buf, const char *hdr)
  28. {
  29.     int
  30.         len = strlen (hdr);
  31.  
  32.     while (*buf && *buf != '\n') {
  33.         if (strncmp (buf, hdr, len) == 0)
  34.             break;
  35.         while (*buf && *buf != '\n')
  36.             ++buf;
  37.         if (*buf == '\n')
  38.             ++buf;
  39.     }
  40.  
  41.     if (*buf && *buf != '\n') {     /*  header found    */
  42.         SHBase = buf;
  43.         ScanNext ();        /*  skip header     */
  44.         return 0;
  45.     }
  46.  
  47.     SHBase = NULL;
  48.     return -1;
  49. }
  50.  
  51. char *
  52. ScanNext (void)
  53. {
  54.     const char
  55.         *p2,
  56.         *ptr;
  57.     static char
  58.         *Last;
  59.  
  60.     if (ptr = SHBase) {
  61.         while (*ptr == ' ' || *ptr == '\t' ||
  62.                *ptr == ',' || *ptr == '\n') {
  63.             if (ptr [0] == '\n') {
  64.                 if (ptr [1] != ' ' && ptr [1] != '\t')
  65.                     ptr = "\0";
  66.             }
  67.             ++ptr;
  68.         }
  69.         if (*ptr == 0) {
  70.             SHBase = NULL;
  71.             return NULL;
  72.         }
  73.         for (p2 = ptr; *p2 && *p2 != ' ' && *p2 != '\t' && *p2 != ',' && *p2 != '\n'; ++p2)
  74.             ;
  75.  
  76.         SHBase = p2;
  77.         {
  78.             short
  79.                 i = p2 - ptr;
  80.  
  81.             if (Last)
  82.                 free (Last);
  83.             if (Last = malloc (i + 1)) {
  84.                 movmem ((void *) ptr, Last, i);
  85.                 Last [i] = 0;
  86.             }
  87.         }
  88.     }
  89.     else {
  90.         if (Last)
  91.             free (Last);
  92.         Last = NULL;
  93.     }
  94.  
  95.     return Last;
  96. }
  97.  
  98. /*
  99.  *  return *temporary* buffer containing specified header, or NULL.
  100.  *  returned pointer includes the header itself.
  101.  */
  102.  
  103. char *
  104. GetHeader (const char *buf, const char *hdr)
  105. {
  106.     int
  107.         len = strlen (hdr);
  108.     static char
  109.         *Last = NULL;
  110.  
  111.     while (*buf && *buf != '\n') {
  112.         if (strncmp (buf, hdr, len) == 0)
  113.             break;
  114.         while (*buf && *buf != '\n')
  115.             ++buf;
  116.         if (*buf == '\n')
  117.             ++buf;
  118.     }
  119.  
  120.     if (Last) {
  121.         free (Last);
  122.         Last = NULL;
  123.     }
  124.  
  125.     if (*buf == 0 || *buf == '\n')
  126.         return NULL;
  127.  
  128.     /*
  129.      *  if header is found, determine end of header.
  130.      */
  131.  
  132.     {
  133.         const char
  134.             *ptr;
  135.         long
  136.             len;
  137.  
  138.         for (ptr = buf; *ptr && !(ptr[0] == '\n' && ptr[1] != ' ' && ptr[1] != '\t'); ++ptr)
  139.             ;
  140.         len = ptr - buf;
  141.  
  142.         if (Last = malloc (len + 1)) {
  143.             movmem ((void *) buf, Last, len);
  144.             Last [len] = 0;
  145.         }
  146.     }
  147.  
  148.     return Last;
  149. }
  150.  
  151. char *
  152. DupHeader (const char *buf, const char *hdr)
  153. {
  154.     char
  155.         *ptr;
  156.  
  157.     if (ptr = GetHeader (buf, hdr))
  158.         ptr = strdup (ptr);
  159.  
  160.     return ptr;
  161. }
  162.