#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "lib.h"
#define USE_SOL 1
#include "solfile.h"

int main(int argc, char *argv)
{
	SOLFILE *f;
	int i;
	int n;
	int data[32000];
	char bf[BFSZ];
	sol_init(printf);
	f = solfopen("test.txt","wb");
	if (f==NULL) lib_abort();
	solfprintf(f,"This is a test file1\n");
	solfprintf(f,"This is a test file2\n");
	solfclose(f);

	f = solfopen("test.txt","ab");
	if (f==NULL) lib_abort();
	solfprintf(f,"This is a test file3\n");
	solfprintf(f,"This is a test file4\n");
	solfclose(f);

	f = solfopen("test.txt","rb");
	if (f==NULL) lib_abort();
	for (;!solfeof(f);) {
		if (solfgets(bf,BFSZ-1,f)==NULL) break;
		printf("Got back (%s)\n",bf);
	}
	solfseek(f,0,SEEK_SET);
	for (;!solfeof(f);) {
		if (solfgets(bf,BFSZ-1,f)==NULL) break;
		printf("Got back (%s)\n",bf);
	}
	solfclose(f);
exit(0);
	//Things to test:

	f = solfopen("test.txt","wb");
	if (f==NULL) lib_abort();
	for (i=0; i<8000; i++)  data[i] = i;
	n = solfwrite(data,sizeof(int),8000,f);
	printf("wrote %d blocks\n",n);
	if (solfflush(f)!=0) lib_abort();
	if (solfclose(f)!=0) lib_abort();
	for (i=0; i<8000; i++)  data[i] = 0;

	f = solfopen("test.txt","rb");
	if (f==NULL) lib_abort();
	n = solfread(data,sizeof(int),8000,f);
	printf("Read %d  blocks\n",n);
	for (i=0; i<8000; i++)  if (data[i] != i) lib_abort();
	solfclose(f);
	f = solfopen("test.txt","rb");
	solsetvbuf(f,NULL,_IOFBF,4000);
	if (f==NULL) lib_abort();
	for (i=8000-1;i>=0;i--) {
		int p;
		if (solfseek(f,i*sizeof(int),SEEK_SET)!=0) lib_abort();
		n = solfread(data,sizeof(int),1,f);
		p = solftell(f);
		if (p!=i*(int) sizeof(int)+(int) sizeof(int)) lib_abort();
		if (n!=1) lib_abort();
		if (data[0] != i) lib_abort();
	}
	solfclose(f);
	f = soltmpfile();
	if (f==NULL) perror("tmpfile open failed");
	printf("wrote to temp file %s\n",f->_tmpfname);
	solfprintf(f,"wrote to temp file %s\n",f->_tmpfname);
	solfclose(f);

	return 0;
}
int emsg(void)
{
	printf("emsg called\n");
	return 0;
}