TIP: Click on subject to list as thread! ANSI
echo: public_domain
to: All
from: andrew clarke
date: 1996-03-13 03:31:52
subject: ctlparse.c

/*
 *  CTLPARSE.C - Parse a configuration control file.
 *  Written 1993,94,95,96 by Andrew Clarke and released to the public domain.
 */

#include 
#include 
#include 
#include 
#include "ctlparse.h"

#define BUFFER_LEN  512

void ctl_parse(void (*ctl_proc) (char *arg, char *val, int status, int
line), char *pArgs[], FILE * fp, int case_flag)
{
    int bArgFound, iArg, iLine = 1;
    char *p, *pEol, *pEnd, *pVal, szBuf[BUFFER_LEN];

    /* first open the config file */
    if (!fp) {
        ctl_proc(NULL, NULL, CTL_NOFILE, 0);
        return;
    }

    /* file is open so prepare for the scan */
    while (1) {
        *szBuf = '\0';

        /* read a line of the file into the buffer */
        fgets(szBuf, BUFFER_LEN, fp);
        if (!*szBuf) {   /* read was unsuccessful */
            if (feof(fp)) {     /* end-of-file marker encountered */
                ctl_proc(NULL, NULL, CTL_ENDOFFILE, iLine);
                return;
            } else {            /* general failure of some sort */
                ctl_proc(NULL, NULL, CTL_READFAILURE, iLine);
                return;
            }
        }

        p = szBuf;

        if (!strchr(p, '\n')) {
            ctl_proc(NULL, NULL, CTL_LINETOOLONG, iLine);
            return;
        }

        /* scan for comment character */
        pEol = strchr(p, ';');
        if (pEol)
            *pEol = '\0';          /* kill comment and point to end of line */
        else
            pEol = p + strlen(p);  /* point to end of line */

        /* skip leading whitespace */
        while (isspace(*p))
            ++p;

        /* remove trailing whitespace  */
        pEnd = p + strlen(p) - 1;
        while (isspace(*pEnd))
            *(pEnd--) = '\0';

        /* if it's a blank line then don't bother processing it */
        if (*p) {
            pVal = p;

            /* split the argument and parameter */
            while (*pVal && !isspace(*pVal))
                ++pVal;
            if (*pVal)
                *(pVal++) = '\0';

            /* skip leading whitespace in parameter */
            while (*pVal && isspace(*pVal))
                ++pVal;

            for (iArg = 0; *(pArgs + iArg); iArg++) {

                switch (case_flag) {
                case CTL_NOIGNORECASE:
                    bArgFound = (!strcmp(*(pArgs + iArg), p));
                    break;
                case CTL_IGNORECASE:
                    bArgFound = (!stricmp(*(pArgs + iArg), p));
                    break;
                default:
                    /* value of case_flag is invalid */
                    ctl_proc(NULL, NULL, CTL_INVCASEFLAG, iLine);
                    return;
                } /* end switch() */

                if (bArgFound) {
                    if (!(*pVal))  /* argument missing parameter */
                        ctl_proc(*(pArgs + iArg), NULL, CTL_MISSINGPARAM, iLine);
                    else           /* valid argument with parameter */
                        ctl_proc(*(pArgs + iArg), pVal, iArg + 1, iLine);
                    break;         /* break for() */
                } /* end if() */

            } /* end for() */

            if (!bArgFound)     /* illegal argument */
                ctl_proc(p, pVal, CTL_ILLEGALARG, iLine);

        } /* end if() */
        /* comment, white space, blank line, or line too long encountered */
        iLine++;
    } /* end while (1) */
}

--- Msgedsq/2 3.30
* Origin: This one HAS to be original X (3:635/727.4{at}fidonet)
SEEN-BY: 50/99 78/0 635/309 503 544 727 640/230 711/401 410 413 430 808 809
SEEN-BY: 711/934 712/610 713/888 800/1 7877/2809
@PATH: 635/727 544 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.