#ifndef KEEP_H
#define KEEP_H 1

typedef struct KEEPLIST {char *mem; int sz; int used;  struct KEEPLIST *next;} Keeplist;

void keep_freeall(Keeplist **h);

#define keep_malloc(a,b) keep_malloc2(a,b,__FILE__,__LINE__)
#define keep_strdup(a,b) keep_strdup2(a,b,__FILE__,__LINE__)
#define keep_malloc_align(a,b) keep_malloc_align2(a,b,__FILE__,__LINE__)
#define keep_calloc(a,b,c) keep_calloc2(a,b,c,__FILE__,__LINE__)

void *keep_malloc2(int sz,Keeplist **h, char *source, int line);
char *keep_strdup2(Keeplist **h, char *src, char *source, int line);
void *keep_malloc_align2(int sz,Keeplist **h, char *source, int line);
int keep_total(void);
void *keep_calloc2(Keeplist **h, int sz, int num, char *source, int line);
void *keep_realloc(Keeplist **h, void *x,  int sz);

typedef struct KPAIRVAL {char *var; char *val; struct KPAIRVAL *next;} Kpairval;
typedef struct KPAIR {Keeplist *k; Kpairval *first; Kpairval *last;} Kpair;
#define lib_email_getheaders(fname) lib_email_getheaders_keep(fname)
char *kpair_findnc_null(Kpair *kp, char *want);
char *kpair_findnc(Kpair *kp, char *want);


void kpair_copy(Kpair **dst, Kpair *src);
char *kpair_find_null(Kpair *kp, char *want);
int kpair_free(Kpair **pkp);
int kpair_new(Kpair **pkp);
int kpair_removenc(Kpair *kp, char *var, char *val);
int kpair_add_end(Kpair **pkp, char *var, char *val);
//int kpair_add2(Kpair **pkp, char *var, char *val, char *source, int line);
Kpairval *kpair_add2(Kpair **pkp, char *var, char *val, char *source, int line);
#define kpair_add(a,b,c) kpair_add2(a,b,c,__FILE__,__LINE__)
int kpair_add_k2(Kpair **pkp, char *var, char *val,  Keeplist **k, char *source, int line);
#define kpair_add_k(a,b,c,d) kpair_add_k2(a,b,c,d,__FILE__,__LINE__)
int kpair_add_raw(Kpair **pkp, char *var, char *val);

Kpairval *kpair_add_nodup(Kpair **pkp, char *var, char *val);

char *kpair_find_nonull(Kpair *kp, char *want);
int kpair_token_split(char *s, Kpair **pairs,  char *tmp, int tmpsz, Keeplist **k);
int kpair_new_reuse(Kpair **pkp, Keeplist **k);
Kpair *lib_email_getheaders_keep(char *fname);


typedef struct KWORDLIST {
	Keeplist *keep;
	struct KWORD *head;
} Kwordlist;
typedef struct KWORD {
	char *word;
	struct KWORD *next;
} Kword;

void kword_add(Kwordlist *k, char *w);
Kwordlist *kword_new(void);
Kwordlist *kword_reverse(Kwordlist *list);
void kword_free(Kwordlist *list);




#endif
