home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * HEADER SCANNING ROUTINES
- *
- * given a memory buffer terminated with \0 (that might be a file image),
- * do various header handling operations
- */
-
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "config.h"
-
- Prototype int ScanHeader(const char *, const char *);
- Prototype char *GetHeader(const char *, const char *);
- Prototype char *DupHeader(const char *, const char *);
- Prototype char *ScanNext(void);
-
- /*
- * scan a header, elements in the header (comma separated). Blank line
- * ends header scan
- */
-
- static char *SHBase;
-
- int
- ScanHeader(buf, hdr)
- const char *buf;
- const char *hdr;
- {
- int len = strlen(hdr);
- while (*buf && *buf != '\n') {
- if (strncmp(buf, hdr, len) == 0)
- break;
- while (*buf && *buf != '\n')
- ++buf;
- if (*buf == '\n')
- ++buf;
- }
- if (*buf && *buf != '\n') { /* header found */
- SHBase = buf;
- ScanNext(); /* skip header */
- return(0);
- } else {
- SHBase = NULL;
- return(-1);
- }
- }
-
- char *
- ScanNext(void)
- {
- char *ptr;
- char *p2;
- static char *Last;
-
- if (ptr = SHBase) {
- while (*ptr == ' ' || *ptr == 9 || *ptr == ',' || *ptr == '\n') {
- if (ptr[0] == '\n') {
- if (ptr[1] != ' ' && ptr[1] != '\t')
- ptr = "\0";
- }
- ++ptr;
- }
- if (*ptr == 0) {
- SHBase = NULL;
- return(NULL);
- }
- for (p2 = ptr; *p2 && *p2 != ' ' && *p2 != '\t' && *p2 != ',' && *p2 != '\n'; ++p2);
-
- SHBase = p2;
- {
- short i = p2 - ptr;
-
- if (Last)
- free(Last);
- if (Last = malloc(i + 1)) {
- movmem(ptr, Last, i);
- Last[i] = 0;
- }
- }
- } else {
- if (Last)
- free(Last);
- Last = NULL;
- }
- return(Last);
- }
-
- /*
- * return *temporary* buffer containing specified header, or NULL.
- * returned pointer includes the header itself.
- */
-
- char *
- GetHeader(buf, hdr)
- const char *buf;
- const char *hdr;
- {
- int len = strlen(hdr);
- static char *Last;
-
- while (*buf && *buf != '\n') {
- if (strncmp(buf, hdr, len) == 0)
- break;
- while (*buf && *buf != '\n')
- ++buf;
- if (*buf == '\n')
- ++buf;
- }
- if (Last) {
- free(Last);
- Last = NULL;
- }
-
- if (*buf == 0 || *buf == '\n')
- return(NULL);
-
- /*
- * if header is found, determine end of header.
- */
-
- {
- char *ptr;
- long len;
-
- for (ptr = buf; *ptr && !(ptr[0] == '\n' && ptr[1] != ' ' && ptr[1] != '\t'); ++ptr);
- len = ptr - buf;
-
- if (Last = malloc(len + 1)) {
- movmem(buf, Last, len);
- Last[len] = 0;
- }
- }
- return(Last);
- }
-
- char *
- DupHeader(buf, hdr)
- const char *buf;
- const char *hdr;
- {
- char *ptr;
-
- if (ptr = GetHeader(buf, hdr))
- ptr = strdup(ptr);
- return(ptr);
- }
-
-