#include <andrewos.h>
#include <class.h>

#include <fldlist.ih>
#include <attrvalst.eh>

boolean attributevalueset__InitializeObject(classid, self)
struct classheader *classid;
struct attributevalueset *self;
{
    attributevalueset_SetString(self, TRUE, TRUE);
    return TRUE;
}

void attributevalueset__FinalizeObject(classid, self)
struct classheader *classid;
struct attributevalueset *self;
{
}

char *attributevalueset__ParseString(self, str, startBracket, endBracket)
struct attributevalueset *self;
char *str;
char startBracket;
char endBracket;
{
    char attrName[10000];
    struct fieldlist *attrValPath;
    char *endStr;
    long count;
    long i;

    attrValPath = fieldlist_Create(str, ",", '{', '}', &endStr);
    if (attrValPath != NULL) {
	count = fieldlist_NumberOfElements(attrValPath);
	for (i = 0; i < count; i++) {
	    char *entryStr = fieldlist_GetNth(attrValPath, i);
	    struct fieldlist *entryPath = fieldlist_Create(entryStr, "=", '\0', '\0', NULL);
	    long entryCount;

	    if (entryPath  != NULL) {
		if ((entryCount = fieldlist_NumberOfElements(entryPath)) > 0) {
		    char *attrName = fieldlist_GetNth(entryPath, 0);

		    if (*attrName != '\0') {
			char *value = (entryCount > 1) ? (char *) fieldlist_GetNth(entryPath, 1) : "";
			char *realVal = (char *) malloc(strlen(value) + 1);

			strcpy(realVal, value);
			attributevalueset_Insert(self, attrName, realVal);
		    }
		}
		fieldlist_Destroy(entryPath);
	    }
	}
	fieldlist_Destroy(attrValPath);
    }
    return endStr;
}
