
/*
 *  Cfg.C
 *
 *  CONFIGURATION FILE EXTRACTION
 */

#include "lib.h"

static FILE *Fi;

int
OpenCfgFile()
{
    CloseCfgFile();
    Fi = fopen("s:dnet.config", "r");
    return (Fi != NULL);
}

char *
GetCfgLine(what)
char *what;
{
    static char Buf[128];
    register char *ptr;
    if (Fi) {
	while (fgets(Buf, sizeof(Buf), Fi)) {
	    if (BCmp(Buf, what, 4) == 0) {
		Buf[strlen(Buf)-1] = 0;
		for (ptr = Buf + 4; *ptr == ' ' || *ptr == 9; ++ptr);
		return(ptr);
	    }
	}
    }
    return(NULL);
}

void
CloseCfgFile()
{
    if (Fi)
	fclose(Fi);
    Fi = NULL;
}

void
GetOneCfg(what)
char *what;
{
    char *str;
    OpenCfgFile();
    str = GetCfgLine(what);
    CloseCfgFile();
}

int
ExtractFieldVal(str, field, pidx)
char *str, *field;
short *pidx;
{
    short idx = (pidx) ? *pidx : 0;
    short flen = strlen(field);

    while (*str) {
	if (strncmp(str, field, flen) == 0) {
	    if (pidx)
		*pidx = idx + flen;	/*  past the field but not the val */
	    return(atoi(str + flen));
	}
	++str;
	++idx;
    }
    return(-1);
}

