#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#define ISZI TRUE
#include <errno.h>
#include <iconv.h>
#include "lib.h"
#include "debug.h"
#include "ziconv.h"
static int tot;
#ifdef WIN32


size_t ziconv (ziconv_t cd, const char* * inbuf, size_t * inbytesleft, char* * outbuf, size_t * outbytesleft)
{
	return iconv(cd->cd,inbuf,inbytesleft,outbuf,outbytesleft);
}
ziconv_t ziconv_open (const char* tocode, const char* fromcode, char *file, int line)
{
	ziconv_t z;
	iconv_t cd = iconv_open(tocode,fromcode);
	if (cd==NULL) return NULL;
	if (cd==(iconv_t) -1) return (iconv_t) -1;
	z = d_mallocid(DMID,sizeof(ziconv_t),file,line);
	z->cd = cd;
	tot++;
	return z;
}
int ziconv_close (ziconv_t z)
{
	int r;
	if (z==NULL) return 0; // If null then don't try and free it..
	if (z==(ziconv_t)-1) return 0; // also don't try if it's -1 as it wasn't opened.
	tot--;
	r = iconv_close(z->cd);
	free(z);
	return r;
}
#endif

int ziconv_total(void)
{
	return tot;
}