/* CoreProperties.c
	*/
	
#include "CoreProperties.h"
#include "Core.h"
#include "malloc.h"

Property CreateProp(next, key, value)
	Property next;
	Atom key;
	Ref value;
	{
	Property prop;
	prop=New(Property, PropertyRec);
	prop->next=next;
	prop->key=key;
	prop->value=value;
	return(prop);
	};
	
Atom uniqueID=0;
String *propNames;
PropPrintProc *printProcs;
	
Atom RegisterProp(name, printProc) 
	String name;
	PropPrintProc printProc;
	{
	PropPrintProc *newPrintProcs;
	String *newPropNames;
	Nat i;
	newPropNames=(String *)malloc((uniqueID+1)*sizeof(String));
	newPrintProcs=(PropPrintProc *)malloc((uniqueID+1)*sizeof(PropPrintProc));
	for (i=0; i<uniqueID; i++) {
		newPropNames[i]=propNames[i];
		newPrintProcs[i]=printProcs[i];
		};
	newPropNames[uniqueID]=name;
	newPrintProcs[uniqueID]=printProc;
	propNames=newPropNames;
	printProcs=newPrintProcs;
	uniqueID+=1;
	return(uniqueID-1);
	};
	
Ref GetProp(from, prop)
	Properties from;
	Atom prop;
	{
	while (from) {
		if (from->key==prop) return(from->value);
		from=from->next;
		};
	return(nil);
	};
	
Properties PutProp(on, prop, value)
	Properties on;
	Atom prop;
	Ref value;
	{
	Properties scan=on;
	while (scan) {
		if (scan->key==prop) {
			scan->value=value;
			return(on);
			};
		scan=scan->next;
		};
	return(CreateProp(on, prop, value));
	};
	
void PrintProperties(props, indent, cr, level)
	Properties props;
	Nat indent;
	Bool cr;
	Nat level;
	{
	Properties scan=props;
	while (scan) {
		if (printProcs[scan->key]!=nil) (printProcs[scan->key])(scan->key, scan->value, indent, level, cr);
		scan=scan->next;
		};
	};
	
void PrintPropertyString(prop, value, indent, level, cr)
	Atom prop;
	Ref value;
	Nat indent;
	Nat level;
	Bool cr;
	{
	level=level;
	PrintIndent(indent, cr);
	printf("%s: %s", propNames[prop], value);
	};