#ifndef DSTRING_HEAD
#define DSTRING_HEAD done

/*-------------- Word lists ----------------------------------- */

typedef struct WLIST {
	char *s;
	struct WLIST *next;
	char *source; int line;
	struct WLIST *dnext,*dlast;
	char id;
} Wlist;
/*-------------- Word lists end ----------------------------------- */

typedef struct  DSTR {
	int len, total; char *s;
	int nodisk; // If true then always keep in memory.
	char *source; int line;
	struct DSTR *dnext,*last;
	char id;
	int flen;
	int failed;
	int read_at;
	FILE *f; // Temp file we are writing the thing to.
	int isfile; 
	char fname[100];
} Dstr;
void dstr_nodisk(Dstr *s, int x);
void dstr_startup(int maxlen, void (*fn)(char *s));
void dstr_exit(void);
int dstr_addfile(Dstr *s, char *fname);
int dstr_total(void);
void dstr_lock(Dstr *s);
void dstr_unlock(Dstr *s);
int dstr_append(Dstr *xitem, char *fname);
Dstr *dstr_newsz(int sz);
void dstr_clear(Dstr *dst); 
void dstr_clearbig(Dstr *dst); 
void dstr_new_d(Dstr **ss, char *source, int line);
void dstr_newsz_d(Dstr **ss, int sz, char *source, int line);
char *dstr_ss(Dstr *s);
#ifndef DSTR_LIB
#define dstr_new(ss) dstr_new_d(ss,__FILE__,__LINE__)
#define dstr_newsz(ss,sz) dstr_newsz_d(ss,sz,__FILE__,__LINE__)
#endif

int dstr_set_mutex(int (*cb)(int vm, int isget, int seconds), int m);
int dstr_printf(Dstr *s, const char * arg_list, ...);
int dstr_iprintf(Dstr *dst, int indent, const char * arg_list, ...);
void dstr_init(Dstr *s);
int dstr_len(Dstr *s);
void dstr_adds(Dstr *dst, char *s, int len);
void dstr_add(Dstr *dst, Dstr *src);
void dstr_expand(Dstr *s, int len);
void dstr_setlen(Dstr *s, int len);
void dstr_setsize(Dstr *s, int len);
void dstr_copys(Dstr *dst, char *src, int len);
void dstr_copy(Dstr *dst, Dstr *src);
void dstr_free(Dstr **s);
void dstr_add_rest(Dstr *dst, Dstr *src, int from);
int dstr_write(Dstr *xitem, char *fname);
int dstr_read(Dstr *s, char *fname);
void dstr_addsn(Dstr *a, char *s);
size_t dstr_fwrite(char *s, size_t size, size_t count, Dstr *a);
void dstr_copysn(Dstr *a, char *s);
void dstr_right(Dstr *a, int r);
int dstr_getline(Dstr *item, int *upto, char *bf, int bfsz);
int dstr_fopen(Dstr *str);
char *dstr_fgets(Dstr *str, char *bf, int bfsz);
int dstr_fread_exact(void *bf, size_t size, size_t count, Dstr *str);
int dstr_fread(Dstr *str, char *bf, int bfsz);
int dstr_ferror(Dstr *str);
int dstr_feof(Dstr *str);
int dstr_fclose(Dstr *str);

#define dstr_plain() dstr_plain_d(__FILE__,__LINE__)
Dstr *dstr_plain_d(char *source, int line);
void dstr_ffree(Dstr **s);
int dstr_bfsz(Dstr *dst);
void dstr_big_enough(Dstr *s,int len);
void dstr_ncat(Dstr *s, char *txt);
void dstr_ncat_sep(Dstr *s, char*sep, char*txt);
void dstr_reset(Dstr *s);
int dstr_filewrite(Dstr *s, char *filename);


#endif
