#ifndef DONE_PAIR
#define DONE_PAIR
#define pair_add(a,b,c) pair_add_real(a,b,c,__FILE__,__LINE__,TRUE,FALSE)
#define pair_add_atend(a,b,c) pair_add_real(a,b,c,__FILE__,__LINE__,TRUE,TRUE)
#define pair_add_int(a,b,c) pair_add_int_real(a,b,c,__FILE__,__LINE__)
#define pair_add_allowdup(a,b,c) pair_add_real(a,b,c,__FILE__,__LINE__,FALSE,FALSE)

#define pair_loadfile(a,b) pair_loadfile_x(a,b,__FILE__,__LINE__)

typedef struct PAIR {char *var; char *val; int val_len; struct PAIR *next;} Pair;


void pair_setval(Pair *p, char *val);
// these two are in here because htey rely on pair calls, could probably be removed...
char *lib_token_encode_one(char *var, char *val, char *tmp, int tmpsz);
char *lib_token_encode_quoted(char **names, char **values, int n, int ignore_empty_values, char *bf, int bfsz);
time_t pair_find_time(Pair *p, char *var);
int pair_add_time(Pair **pp, char *var, time_t val);
int pair_add_toend(Pair **pp, Pair **next, char *var, char *val);
int pair_savefile_safe(Pair *p, char *file);
int pair_loadfile_x(Pair **pp, char *file, char *src, int line);
void pair_copy_real(Pair **dst, Pair *src,char *filename , int line);
int pair_add_real(Pair **p, char *var, char *val, char *file, int line, int check_dups, int atend);
int pair_add_int_real(Pair **pp, char *var, int val,char *file, int line);
Pair *pair_append(Pair *p, char *val);
Pair *pair_add_p(Pair **pp, char *var, char *val);
Pair *pair_find_p(Pair *p, char *var);
int pair_add_bin(Pair **pp, char *var, char *val, int len);
char *pair_find_bin(Pair *p, char *var, int *len);
char *pair_find_value(Pair *p, char *var);
int pair_len(Pair *p);

#define pair_dup(x) pair_dup_real(x,__FILE__,__LINE__)
#define pair_copy(x,y) pair_copy_real(x,y,__FILE__,__LINE__)
#define pair_free(x) pair_free_real(x,__FILE__,__LINE__)
int pair_free_real(Pair **p,char *file, int line);

Pair *pair_dup_real(Pair *src, char *file, int line);
char *pair_findn(Pair *p, char *var, int n);
int pair_addn(Pair **p, char *var, int n, char *val);
int pair_deln(Pair **p, char *var, int n);
int pair_token_split(char *s, Pair **pairs,  char *tmp, int tmpsz);
int pair_token_split_keep(char *s, Pair **pairs,  char *tmp, int tmpsz);
Pair *pair_token_decode(char *string);
int pair_token_decode_one(char **string, char *var, char *val, int bfsz);
char *pair_token_encode(Pair *list, char *tmp, int tmpsz);
char *pair_token_encode_one(char *var, char *val, char *tmp, int tmpsz);


Pair *pair_split(char *pin, char *sep);
#define lib_email_getheaders_pair(fname) lib_email_getheaders_real(fname,__FILE__,__LINE__)

Pair *lib_email_getheaders_real(char *fname, char *file, int line);

int pair_savefile(Pair *pp, char *file);

char *pair_find(Pair *p, char *var);
int pair_find_int(Pair *p, char *var);
char *pair_find_null(Pair *p, char *var);

char *pair_findnc(Pair *p, char *var);
char *pair_findnc_null(Pair *p, char *var);

int pair_remove(Pair **pp, char *var, char *val);
int pair_removenc(Pair **pp, char *var, char *val);
int pair_remove_empty_values(Pair **pp);


void pair_reverse(Pair **list);

int pair_add_end(Pair **pp, Pair **next, char *var, char *val);

int pair_cmp(const void *p1, const void *p2);
int pair_cmp_v(const void *p1, const void *p2);
int pair_cmpnc(const void *p1, const void *p2);
int pair_sort(Pair **pp, int (*cmp_fn)(const void *p1, const void * pp2));
void pair_log(Pair *list,int (*printf_fn)(const char *fmt, ...));
void pair_log_short(Pair *list,char *start_msg,int (*printf_fn)(const char *fmt, ...));

int pair_add_double(Pair **pp, char *var, double val);
int pair_loadfile_sep(Pair **pp, char *file, char *sep);
int pair_loadfile_concat(Pair **pp, char *file, char *concat);
int pair_printf(Pair **pp, char *var, char *fmt,...) ;
#endif
