/* File: DefaultsTable.m - NXString subclass for handling defaults
 *
 * By: Christopher Lane
 * Symbolic Systems Resources Group
 * Knowledge Systems Laboratory
 * Stanford University
 *
 * Date:  25 January 1993
 *
 * Copyright: 1992 & 1993 by The Leland Stanford Junior University.
 *            portions copyright 1992, 1993 by Christopher Lane
 * This program may be distributed without restriction for non-commercial use.
 */

#import "DefaultsTable.h"

#import <stdlib.h>

#define intDefaultFormat "%d"
#define floatDefaultFormat "%f"
#define sizeDefaultFormat "w:%f h:%f"
#define pointDefaultFormat "x:%f y:%f"
#define colorDefaultFormat "r:%f g:%f b:%f a:%f"

#define convertToColor NXConvertRGBAToColor
#define convertFromColor NXConvertColorToRGBA

int writeIntDefault(const char *name, int value)
{
	char string[BUFSIZ];
	
	(void) sprintf(string, intDefaultFormat, value);
	
	return writeDefault(name, string);
}

int writeFloatDefault(const char *name, float value)
{
	char string[BUFSIZ];
	
	(void) sprintf(string, floatDefaultFormat, value);
	
	return writeDefault(name, string);
}

int writeSizeDefault(const char *name, NXSize size)
{
	char string[BUFSIZ];
	
	(void) sprintf(string, sizeDefaultFormat, size.width, size.height);
	
	return writeDefault(name, string);
}

NXSize getSizeDefault(const char *name)
{
	NXSize size;
	const char *string = getStringDefault(name);
	
	(void) sscanf(string, sizeDefaultFormat, &size.width, &size.height);

	return size;
}

int writePointDefault(const char *name, NXPoint point)
{
	char string[BUFSIZ];
	
	(void) sprintf(string, pointDefaultFormat, point.x, point.y);
	
	return writeDefault(name, string);
}

NXPoint getPointDefault(const char *name)
{
	NXPoint point;
	const char *string = getStringDefault(name);
	
	(void) sscanf(string, pointDefaultFormat, &point.x, &point.y);

	return point;
}

int writeColorDefault(const char *name, NXColor color)
{
	char string[BUFSIZ];
	float primaries[3], alpha;
	
	convertFromColor(color, &(primaries[0]), &(primaries[1]), &(primaries[2]), &alpha);
	
	(void) sprintf(string, colorDefaultFormat, primaries[0], primaries[1], primaries[2], alpha);
	
	return writeDefault(name, string);
}

NXColor getColorDefault(const char *name)
{
	float primaries[3], alpha;
	const char *string = getStringDefault(name);
	
	(void) sscanf(string, colorDefaultFormat, &(primaries[0]), &(primaries[1]), &(primaries[2]), &alpha);

	return convertToColor(primaries[0], primaries[1], primaries[2], alpha);
}

@implementation DefaultsTable : NXStringTable

- initFromFile:(const char *) file
{
	return [[self init] readFromFile:file];
}

- registerDefaults:(const char *) owner
{
        if(![self applyToDefaults:owner function:NXRegisterDefaults]) return nil;

        return self;
}

- writeDefaults:(const char *) owner
{
        if(![self applyToDefaults:owner function:(int (*)(const char *, const NXDefaultsVector)) NXWriteDefaults]) return nil;

        return self;
}

- updateDefaults
{
        NXUpdateDefaults();

        return self;
}

- (int) applyToDefaults:(const char *) owner function:(int (*)(const char *, const NXDefaultsVector)) routine
{
        int i, status;
        const void *key, *value;
        struct _NXDefault *vector;
        NXHashState state = [self initState];

        if((vector = (struct _NXDefault *) malloc(([self count] + 1) * sizeof(struct _NXDefault))) == NULL) perror("calloc");

        for(i = 0; [self nextState:&state key:&key value:&value]; i++) {
                 vector[i].name = (char *) key;
                 vector[i].value = (char *) value;
                 }

        vector[i].name = vector[i].value = NULL;

        status = (*routine)(owner, vector);

        free(vector);

        return status;
}

@end
