#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "lib.h"
#include "hashlist.h"
#define DMID 14
#include "debug.h"

#ifndef FALSE
#define FALSE 0
#define TRUE (!FALSE)
#endif


Hlist *hlist_new(void)
{
	Hlist *h;
	h = calloc(1,sizeof(Hlist));
		
	return h;
}
int hlist_add_file(Hlist *h, char *fname)
{
	FILE *f;
	int nlines=0;
	char *s;
	char bf[BFSZ];

	f = fopen(fname,"r");
	if (f==NULL) {
		//imsg("Loading hlist_add_file failed %s %s\n",fname,strerror(errno));
		return FALSE;
	}
	for (;!feof(f);) {
		if (fgets(bf,BFSZ-1,f)==NULL) break;
		s = strchr(bf,'\n'); if (s!=NULL) *s = 0;
		s = strchr(bf,'\r'); if (s!=NULL) *s = 0;
		if (lib_strlen(bf)==0) continue;
		strlwr(bf); // not so sure bout this, but needed for top lvel domain names.
		hlist_add(h,bf);

		nlines++;
	}
	fclose(f);
	//imsg("Loading hlist_add_file %d lines %s\n",nlines,fname);
//	printf("Loading hlist_add_file %d lines %s\n",nlines,fname);
	return TRUE;
}
int hlist_del(Hlist *h, char *var);
int hlist_del_file(Hlist *h, char *fname)
{
	FILE *f;
	int nlines=0;
	char *s;
	char bf[BFSZ];
	f = fopen(fname,"r");
	if (f==NULL) {
		//imsg("Loading hlist_add_file failed %s %s\n",fname,strerror(errno));
		return FALSE;
	}
	for (;!feof(f);) {
		if (fgets(bf,BFSZ-1,f)==NULL) break;
		s = strchr(bf,'\n'); if (s!=NULL) *s = 0;
		s = strchr(bf,'\r'); if (s!=NULL) *s = 0;
		if (lib_strlen(bf)==0) continue;
		strlwr(bf); // not so sure bout this, but needed for top lvel domain names.
		hlist_del(h,bf);

		nlines++;
	}
	fclose(f);
	//imsg("Loading hlist_add_file %d lines %s\n",nlines,fname);
	return TRUE;
}
int hlist_add_r(Hlist *h, char *var, char *src, int line)
{
	Hlentry *e;
	int i; 
	i = lib_str_hash(var,MAX_HASHLIST);
	//e = d_callocid(DMID,1,sizeof(Hlentry),src,line);
	e = keep_calloc2(&h->keep,1,sizeof(Hlentry),src,line);
	e->var = keep_strdup(&h->keep,var);
	e->next = h->table[i];
//if (strstr(var,"test")!=NULL) printf("added entry %d (%s) \n",i,var);
	h->table[i] = e;
	h->tot++;
	return TRUE;
}

int hlist_del(Hlist *h, char *var)
{
	int i;
	Hlentry *e;
	if (strlen(var)==0) return FALSE;
	i = lib_str_hash(var,MAX_HASHLIST);

	if (h==NULL) return FALSE;
	for (e = h->table[i]; e!=NULL; e=e->next) {
		if (strcmp(var,e->var)==0) {
			strcpy(e->var,""); // blank is deleted.
			return TRUE;
		}
	}
	return FALSE;
}
int hlist_find(Hlist *h, char *var)
{
	int i;
	Hlentry *e;
	if (strlen(var)==0) return FALSE;
	i = lib_str_hash(var,MAX_HASHLIST);

	if (h==NULL) return FALSE;
	for (e = h->table[i]; e!=NULL; e=e->next) {
		if (strcmp(var,e->var)==0) {
			return TRUE;

		}
	}
	return FALSE;
}
int hlist_free(Hlist **ph)
{
	int i;
	Hlist *h = *ph;
	Hlentry *e,*next;
	for (i=0; i<MAX_HASHLIST; i++) {
	  for (e = h->table[i]; e!=NULL; e=next) {
		  next = e->next;
//		  free(e->var);
//		  free(e);
	  }
	}
	keep_freeall(&h->keep);
	free(h);
	*ph = NULL;

	return FALSE;
}
int hlist_show(Hlist *h)
{
	Hlentry *e,*next;
	int i;

	for (i=0; i<MAX_HASHLIST; i++) {
		for (e = h->table[i]; e!=NULL; e=next) {
			printf("hlist_show %s\n",e->var);
			next = e->next;
		}
	}
	return TRUE;
}
