#ifndef QPAIR_H
#define QPAIR_H

#include "qmalloc.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct QPAIRELEMENT {
	char *var;
	char *val;
	struct QPAIRELEMENT *hash_next;
	struct QPAIRELEMENT *next;
	struct QPAIRELEMENT *hash_prev;
	struct QPAIRELEMENT *prev;
} QPair;

typedef struct {
	QPair **htable;
	QPair *start;
	Qmalloc *qmalloc;
	Qmalloc *free_qmalloc; /* We should free this when freeing the qPair structure */
	int hash_size;
	int auto_eliminate_leaks;
	int (*hash_fn)(char *s, int max);
} QPairList;

int qpair_set_time(QPairList **qp, char *name, time_t val);
QPairList *qpair_copy(QPairList *plist);
int qpair_set_x(QPairList **qp, char *name, char *val, char *file, int line);
int qpair_set_int_x(QPairList **qp, char *name, int val, char *file, int line);
int qpair_del_x(QPairList **qp, char *name, char *value, char *file, int line);
int qpair_set_allow_dup_x(QPairList **qp, char *name, char *val, char *file, int line);
int qpair_free_x(QPairList **qp, char *file, int line);
#define qpair_set(x,y,z) qpair_set_x(x,y,z,__FILE__,__LINE__)
#define qpair_set_int(x,y,z) qpair_set_int_x(x,y,z,__FILE__,__LINE__)
#define qpair_del(x,y) qpair_del_x(x,y,NULL,__FILE__,__LINE__)
#define qpair_remove(x,y,z) qpair_del_x(x,y,z,__FILE__,__LINE__)
#define qpair_set_allow_dup(x,y,z) qpair_set_allow_dup_x(x,y,z,__FILE__,__LINE__)
#define qpair_free(x) qpair_free_x(x,__FILE__,__LINE__)
#define qpair_dup(x) qpair_dup_x(x,__FILE__,__LINE__)

int qpair_set_do(QPairList **qpp, char *name, char *val, int allow_dups, char *file, int line);
QPair *qpair_start(QPairList *pl);
int	qpair_free_except_with_prefix(QPairList **qpp, char *prefix, char *prefix2);
void qpair_remove_empty_values(QPairList **qpp);

QPairList *qpair_new_x(int hsize, char *file, int line);
#define qpair_new(x) qpair_new_x(x,__FILE__,__LINE__)
char *qpair_find(QPairList *qp, char *name);
int qpair_find_int(QPairList *qp, char *name);
time_t qpair_find_time(QPairList *qp, char *name);

#define qpair_add qpair_set
#define qpair_add_int qpair_set_int
#define qpair_get qpair_find
int qpair_equal(QPairList *q1, QPairList *q2);
char *qpair_find_null(QPairList *qp, char *name);

QPairList *qpair_new_do_x(int hsize, int initial_qmalloc_chunk_size, Qmalloc *qm, char *file, int line);
#define qpair_new_do(hsize,initial_qmalloc_chunk_size,qm) qpair_new_do_x(hsize,initial_qmalloc_chunk_size,qm,__FILE__,__LINE__)
int qpair_str_hash(char *s, int max);

QPair *qpair_find_element(QPairList *qp, char *name);
char *qpair_token_encode_one(char *var, char *val, char *tmp, int tmpsz);
char *qpair_token_encode(QPairList *list, char *tmp, int tmpsz, int ignore_empty_values);
int qpair_token_split(char *s, QPairList **pairs,  char *tmp, int tmpsz);
int qpair_token_split2(char *s, QPairList **pairs,  char *tmp, int tmpsz);

int qpair_loadfile(QPairList **pp, char *file);
int qpair_savefile(QPairList *plist, char *file);;
int qpair_eliminate_leaks(QPairList **qpp);
QPairList *qpair_dup_x(QPairList *qp, char *file, int line);

#ifdef __cplusplus
}
#endif

#endif
