#ifndef HTML_HEADER
#define HTML_HEADER

#include <stddef.h>
#include "lib.h"
#include "mpair.h"

#define NAMELEN 72

typedef struct tag {
	char name[NAMELEN];
	int isendtag;
	MPairList *attributes;
	Wordlist *flags;
} Tag;

typedef struct html {
	char *text;
	int text_len;

	char *raw;
	int raw_len;

	Tag tag;

	char *work;
	int size;
	int bytes;
	int upto;

	size_t(*read)(char *,size_t,size_t,void *);
	size_t(*write)(const char *,size_t,size_t,void *);
	int(*error)(void *);
	int(*eof)(void *);

	char *logmsg;
} Html;

#define html_new(a) html_new_r(a,__FILE__,__LINE__)
Html *html_new_r(int _bytes, char *file, int line);
void html_reset(Html *h);
void html_free(Html **h);

void html_logmsg(Html *h, char *logmsg);

int html_scan(Html *h, FILE *f);

void html_setup(Html *h, size_t(*read)(char *,size_t,size_t,void *), size_t(*write)(const char *,size_t,size_t,void *), int(*error)(void *), int(*eof)(void *));
int html_scan_ex(Html *h, void *d);

/* return values (must match strings in html.c) */ 
enum {
	HTML_PARAM,   //param error
	HTML_FILE,    //file error
	HTML_BUF,     //buffer too small
	HTML_END,     //finished
	HTML_DOCTYPE, //document type
	HTML_TAG,     //tag found
	HTML_TEXT,    //text found
	HTML_COMMENT, //comment found
	HTML_SPACE,   //whitespace
	HTML_EOL,     //end of line
	HTML_MORE,	  //more data required
	HTML_INVALID_EMAIL,	//Various forms of special things that we try and correct
	HTML_SCRIPT	  // Whole script section
};

//static char *html_strings[]={"HTML_PARAM","HTML_FILE","HTML_BUF","HTML_END",
//		"HTML_DOCTYPE","HTML_TAG","HTML_TEXT","HTML_COMMENT",
//		"HTML_SPACE","HTML_EOL","HTML_MORE","HTML_INVALID_EMAIL","HTML_SCRIPT"};

#ifdef DO_DEFS
char *html_strings[]={"HTML_PARAM","HTML_FILE","HTML_BUF","HTML_END",
    "HTML_DOCTYPE","HTML_TAG","HTML_TEXT","HTML_COMMENT",
    "HTML_SPACE","HTML_EOL","HTML_MORE","HTML_INVALID_EMAIL","HTML_SCRIPT"};
#else
extern char *html_strings[];
#endif

void html_write_tag(Tag *tag, FILE *f, void(*fn)(FILE *,Tag *,char *,char *,int));
void html_write_tag_simple(Html *h, Tag *tag, void *d);


void html_tag_copy(Tag *dst, Tag *t);
void html_tag_free(Tag *tag);

struct BUFFER;
void html_log(Html *h,char*txt, char*tag, int k,  struct BUFFER *buffer, int fn(const char * arg_list, ...));

#endif

