/*****************************************************************************/
/*
                                  dict.h
*/
/*****************************************************************************/

#ifndef DICT_H_LOADED
#define DICT_H_LOADED 1

#include "wasd.h"

/**********/
/* macros */
/**********/

#define DICT_DEFAULT_SIZE  32  /* hash entries */
#define DICT_MIN_BYTES     64  /* minimum chunk of memory for entry strings */

#define DICT_HASH_MULT  69069  /* hash multiplier */

/* some of these are design elements and currently not in use */
#define DICT_TYPE_CONFIG     "~"  /* configuration/mapping entry */
#define DICT_TYPE_INTERNAL   "$"  /* server internal */
#define DICT_TYPE_GLOBAL     "_"  /* global configuration */
#define DICT_TYPE_OPAQUE     "?"  /* opaque value (not string) */
#define DICT_TYPE_OPAQUE_KEY "|"  /* opaque key and value (not strings) */
#define DICT_TYPE_REQUEST    ">"  /* request header */
#define DICT_TYPE_RESERVED   "!"  /* used by METACON.C to remove an entry */
#define DICT_TYPE_RESPONSE   "<"  /* response header */
#define DICT_TYPE_SSI        "^"  /* nudge, nudge, wink, wink */
#define DICT_TYPE_UNDEFINED  ""   /* null character */

#define DICT_TYPE_UNIQUE           0x80    /* enumerated entries */
#define DICT_TYPE_RESPONSE_UNIQUE  "\xbc"  /* enumerated response header */

#define DICT_GET_COUNT(ptr) (((DICT_STRUCT*)(ptr))->count)
#define DICT_GET_KEY(ptr) (((DICT_ENTRY_STRUCT*)(ptr))->key)
#define DICT_GET_KEY_LEN(ptr) (((DICT_ENTRY_STRUCT*)(ptr))->klen)
#define DICT_GET_TYPE(ptr) (((DICT_ENTRY_STRUCT*)(ptr))->type)
#define DICT_GET_VALUE(ptr) (((DICT_ENTRY_STRUCT*)(ptr))->value)
#define DICT_GET_VALUE_LEN(ptr) (((DICT_ENTRY_STRUCT*)(ptr))->vlen)
#define DICT_GET_BYTES(ptr) (((DICT_ENTRY_STRUCT*)(ptr))->bytes)
#define DICT_GET_VALUE_SIZE(ptr) \
((((DICT_ENTRY_STRUCT*)(ptr))->bytes) - (((DICT_ENTRY_STRUCT*)(ptr))->klen))

/**************/
/* structures */
/**************/

#pragma member_alignment __save
#pragma member_alignment

typedef struct DictStruct DICT_STRUCT; 
typedef struct DictEntryStruct DICT_ENTRY_STRUCT; 

struct DictEntryStruct
{
    DICT_ENTRY_STRUCT *blink,
                      *clink,
                      *flink;
    DICT_STRUCT *DictPtr;
    uint bytes,
         extract,
         hash,
         klen,
         vlen;
    uchar *key,
          *value;
    uchar type[2];
    uchar buffer[];
};

struct DictStruct
{
    uint bytes,
         count,
         dirty,
         size,
         unique;
    REQUEST_STRUCT *RequestPtr;
    DICT_ENTRY_STRUCT *head,
                      *next,
                      *tail;
    DICT_ENTRY_STRUCT **table;
};

#pragma member_alignment __restore

/***********************/
/* function prototypes */
/***********************/

DICT_STRUCT* DictCreate (REQUEST_STRUCT*, int);
DICT_STRUCT* DictDestroy (DICT_STRUCT*);
DICT_STRUCT* DictDuplicate (REQUEST_STRUCT*, DICT_STRUCT*);
uint DictDirty (DICT_STRUCT*);
DICT_ENTRY_STRUCT* DictExpandEntry (DICT_ENTRY_STRUCT*, uint);
DICT_ENTRY_STRUCT* DictExtractEntry (DICT_ENTRY_STRUCT*);
DICT_ENTRY_STRUCT* DictInsert (DICT_STRUCT*, uchar*, uchar*, int, uchar*, int);
DICT_ENTRY_STRUCT* DictInsertEntry (DICT_STRUCT*, DICT_ENTRY_STRUCT*);
DICT_ENTRY_STRUCT* DictIterate (DICT_STRUCT*, uchar*);
DICT_ENTRY_STRUCT* DictLookup (DICT_STRUCT*, uchar*, uchar*, int);
DICT_ENTRY_STRUCT* DictRemove (DICT_STRUCT*, uchar*, uchar*, int);
DICT_ENTRY_STRUCT* DictRemoveEntry (DICT_ENTRY_STRUCT*);
void DictValueLength (DICT_ENTRY_STRUCT*, uint);
void DictWatch (DICT_STRUCT*, uchar*, uchar*);
void DictWatchEntry (DICT_ENTRY_STRUCT*);
void DictTest (char*);

#endif /* DICT_H_LOADED */

/*****************************************************************************/

