RCS_ID_C "$Id: niftyprofile.c,v 1.3 1993/07/12 22:54:04 ppessi Exp $";
/* getprofile.c    simple preferences reading for profile information
 *
 * Copyright 1988, Chris Newman
 * All Rights Reserved
 * Permission is granted to copy, modify, and use this as long
 * as this notice remains intact.  This is a nifty program.
 *
 * $Author: ppessi $ $Revision: 1.3 $ $Date: 1993/07/12 22:54:04 $
 */

#include <stdio.h>
#include <ctype.h>

#include "nifty.h"
#include "niftyprofile.h"

#define	BUFLEN		500
#define	MAXPREF		500
#define	MAXPROGNAME	100
#define	MAXPREFNAME	100
#define	MAXPATH		500
#define	MAXFILELIST	800

#define	SKIPSPACES(scan)  while( *scan == ' ' || *scan == '\t' ) scan++
#define	SCANFOR(scan, ch) while( *scan && *scan != ch ) scan++
#define	TOLOWER(ch) ( isupper(ch) ? tolower(ch) : ch )
#define	LOWERSTRCPY(dest, src,	endch) \
        while( *dest++ = TOLOWER(*src), *src++ != endch ); *(--dest) = 0

char ProgName[MAXPROGNAME] = "*";
char PrefFile[MAXFILELIST] = RESOURCELIST;
char CurPrefFile[MAXPATH] = "preferences";
char LastProfile[MAXPREF] = "on";
char LineBuf[BUFLEN];

char *getprofile(preference)
char	*preference;
{
    char    *nextfile = PrefFile;
    char    progName[MAXPROGNAME], fname[MAXPATH], pref[MAXPREFNAME];
    short   prefLen, pgLen, fnameLen;
    FILE    *file;
    register char    *ptr, *scan;

    /* set home directory full path */
    fnameLen = 0;

    /* set program name (progName) and preference name (pref) */
    scan = preference;
    ptr = pref;
    SCANFOR( scan, '.' );
    if (*scan++) {
	LOWERSTRCPY( ptr, scan, '\0' );
	ptr = progName;
	scan = preference;
	LOWERSTRCPY( ptr, scan, '.' );
    } else {
	scan = preference;
	LOWERSTRCPY( ptr, scan, '\0' );
	ptr = progName;
	scan = ProgName;
	LOWERSTRCPY( ptr, scan, '\0' );
    }
    if ( !(prefLen = strlen(pref)) || !(pgLen = strlen(progName)) )
	return NULL;

    /* loop through listed files */
    while( *nextfile ) {
	if (*nextfile != '/') {
	    (void) strcpy(fname+fnameLen, nextfile);
	    (void) strcpy(CurPrefFile, fname);
	    file = fopen(fname, "r");
	} else {
	    (void) strcpy(CurPrefFile, nextfile);
	    file = fopen(nextfile, "r");
	}
	if (file == NULL) {
	    scan = NULL;
	    while( *nextfile++ );
	    continue;
	}

	/* read each line in preference file */
	while (!feof(file)) {
	    (void) fgets( LineBuf, BUFLEN, file );
	    for (scan = LineBuf; (*scan = TOLOWER( *scan )) && *scan != ':'; scan++);
	    SCANFOR( scan, '\n' );
	    *scan = 0;
	    scan = LineBuf;
	    SKIPSPACES( scan );
	    if (*scan == '*' || !strncmp(scan, progName, pgLen)) {
		SCANFOR( scan, '.' );
		if (!strncmp(++scan, pref, prefLen) && *(scan += prefLen) == ':') {
		    scan++;
		    SKIPSPACES( scan );
		    break;
		}
	    }
	    scan = NULL;
	}
	(void) fclose( file );

	/* keep track of the last profile in a global variable */
	if (scan) {
	    (void) strncpy(LastProfile, scan, MAXPREF);
	    scan = LastProfile;
	    break;
	}

	/* move pointer to next filename */
	while ( *nextfile++ );
    }

    return scan;
}

int getprofileswitch( preference, dflt )
char	*preference;
int	dflt;
{
    if (getprofile( preference ) != NULL) {
	if (*LastProfile == '0' || *LastProfile == 'n' ||
	    (*LastProfile == 'o' && LastProfile[1] == 'f') || *LastProfile == 'f')
	    dflt = 0;
	else if (*LastProfile == '1' || *LastProfile == 'y' ||
		 (*LastProfile == 'o' && LastProfile[1] == 'n') || *LastProfile == 't')
	    dflt = 1;
    }

    return dflt;
}

int getprofileint( preference, dflt )
char	*preference;
int	dflt;
{
    int	    test = 0, negate = 1;
    char    *numptr;

    if ((numptr = getprofile(preference)) != NULL) {
	if (*numptr == '-')
	    negate = -1, numptr++;
	while( *numptr >= '0' && *numptr <= '9' )
	    test *= 10, test += *numptr++ - '0';
	if (numptr != LastProfile)
	    dflt = test * negate;
    }

    return dflt;
}

int profileentryexists( preference )
char	*preference;
{
    return( (int) (getprofile(preference) == NULL) );
}

