/*
 * Karray: string-addressable KW array, used for easy manipulation
 * of configuration files.
 *
 * Copyright (c) 1996 David L Parsons
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that the above copyright notice and this paragraph are
 * duplicated in all such forms and that any documentation,
 * advertising materials, and other materials related to such
 * distribution and use acknowledge that the software was developed
 * by David L Parsons (orc@pell.chi.il.us).  My name may not be used
 * to endorse or promote products derived from this software without
 * specific prior written permission.  THIS SOFTWARE IS PROVIDED
 * ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
 * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * The Karray class provides a handy base-class for building string-addressable
 * configuration lists.
 *
 * public interface:
 *
 *	Karray()			create ourself, not initialized
 *	~Karray()			die.
 *
 *	int autoappend()		return current autoappend mode
 *	int autoappend(int)		set or clear autoappend mode
 *
 *	int count()			return # items in the array
 *	int changed()			if anything has changed
 *	int changed(int)		set the changed flags
 *
 *	KW& operator[](char *)		return value for named index
 *
 *	KW& create(char*,char*)		Add a name to the array
 *	int locate(char*)		return array position for a name
 *	int has(char*)			return if the name is in the array
 *	int drop(char*)			remove a name from the array
 *	void Erase()			clobbers everything in the array.
 *
 *	int foreach((int*)(char*,KW*))	iterator.
 *	int print(FILE *)		write the array to disk.
 *	int read(FILE *)		read the array off disk.
 */
#ifndef _KARRAY_D
#define _KARRAY_D

#include <malloc.h>
#include <basis/KW.H>

typedef int(*PTKV) (char*, KW*);

class Karray {
protected:
    struct KA {
	int   _used;
	char *_key;
	KW   *_content;
    } *_list;

    int _nrlist;
    int _autoappend;
    int _dirty;

    int changed(int val);			/* set changed flags */
    inline int autoappend(int val) { return _autoappend = val ? 1 : 0; }
    void Erase();				/* removes everything */

public:
    inline Karray() { _list = (struct KA*)0; _nrlist = 0; }
    virtual ~Karray();

    virtual int print(FILE *out = stdout);	/* print the object */
    virtual int read(FILE *in = stdin);		/* load the object */

    inline int autoappend() { return _autoappend; }
    inline int count() { return _nrlist; }	/* return# items in list */
    int changed();				/* did something change? */

    KW& operator[] (char* key);			/* get heap for name */
    KW& valueof(char* key);			/* get heap for name */
    KW& create(char* key,char* value = 0);	/* add key to the array */
    int locate(char* key);			/* find index of name */
    int drop(char* key);			/* remove item by name */

    inline int has(char* key)	{ return locate(key) != EOF; }

    int foreach(int (*)(char* key, KW* val));	/* iterator */
} ;

#define yn(val)	(val ? "true" : "false")

#endif/*_KARRAY_D*/
