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

int mkfinit(mkf *mkf,char *filename) {
	mkf->magic=12345;
	mkf->head=0;
	mkf->tail=0;
	strncpy(mkf->filename,filename,MKF_SLEN);
	mkf->filename[MKF_SLEN-1]=(char)0;
	}

int mkfload(mkf *mkf) {
	mkflock(mkf);
	FILE *f;
	char buf[MKF_BIGSTRING];
	f=fopen(mkf->filename,"rt");
	if(!f) {mkfunlock(mkf); return 0;};
	while(fgets(buf,sizeof(buf),f)) {
		char *trimmer=&buf[strlen(buf)-1];
		if(*trimmer=='\n') *trimmer=(char)0;
		mkfwrite(mkf,buf);
		// printf("lecture et enregistrement %s OK \n",buf);
		};
	fclose(f);
	mkfunlock(mkf);
	return 1;
	}

int mkfcount(mkf *mkf) {
	mkflock(mkf);
	int ret=0;
	if(mkf->head <= mkf->tail) ret=mkf->tail - mkf->head;
	else ret= (MKF_MAXENTRIES - mkf->head) + mkf->tail +1;
	mkfunlock(mkf);
	return ret;
	}

int mkfsize(mkf *mkf) {
	mkflock(mkf);
	if(mkf->head==mkf->tail) {mkfunlock(mkf); return 0;};
	int slot;
	size_t ret=0;
	for(slot=mkf->head;slot!=mkf->tail;slot=(slot+1)%MKF_MAXENTRIES) 
		ret+=strlen(mkf->entry[slot]);
	mkfunlock(mkf);
	return ret;
	}

int mkfdebug(mkf *mkf) {
	fprintf(stderr,"magic=%d\nhead=%d\ntail=%d\n",mkf->magic,mkf->head,mkf->tail);
	}

int mkfdump(mkf *mkf, char*out,size_t n) {
	mkflock(mkf);
	*out=(char)0;
	int slot;
	int index=0;
	for(slot=mkf->head;slot!=mkf->tail;slot=(slot+1)%MKF_MAXENTRIES) 
		{
		char *ptr=mkf->entry[slot];
		int len=strlen(ptr);
		//plus assez de place
		if(2+index+len>n) {mkfunlock(mkf); return 0;};
		printf("\ntraitement slot %d",index);
		strcpy(&(out[index]),ptr);
		index+=len;
		out[index]=(char)10;
		out[++index]=(char)0;
		};
	mkfunlock(mkf);
	return 1;
	}

int mkfsave(mkf *mkf) {
	mkflock(mkf);
	FILE *f;
	f=fopen(mkf->filename,"wt");
	if(!f) {mkfunlock(mkf); return 0;};
	int slot;
	for(slot=mkf->head;slot!=mkf->tail;slot=(slot+1)%MKF_MAXENTRIES) 
		{fputs(mkf->entry[slot],f);fputc((char)10,f);};
	fclose(f);
	mkfunlock(mkf);
	return 1;
	}

int _mkfwrite(mkf *mkf,char *data) {
	int slot;
	slot=(mkf->head==mkf->tail)? mkf->head:(mkf->tail+1)%MKF_MAXENTRIES;
	printf("\ninsertion dans slot=%d",slot);
	if(slot==mkf->head) return 0;
	char* ptr=malloc(strlen(data)+1);
	strcpy(ptr,data);
	mkf->entry[slot]=ptr;
	mkf->tail=slot;
	mkfunlock(mkf);
	return 1;
	}

int mkfwrite(mkf *mkf,char *data) {
	mkflock(mkf);
	int ret=_mkfwrite(mkf,data);
	mkfunlock(mkf);
	return ret;
	}

int mkfwritehead(mkf *mkf,char *data) {
	mkflock(mkf);
	int slot;
	slot=(mkf->head==mkf->tail)? mkf->head:mkf->head-1;
	if(slot<0) slot=MKF_MAXENTRIES-1;
	if(slot==mkf->head) {mkfunlock(mkf);return 0;};
	char* ptr=malloc(strlen(data)+1);
	strcpy(ptr,data);
	mkf->entry[slot]=ptr;
	mkf->head=slot;
	mkfunlock(mkf);
	return 1;
	}

int mkfempty(mkf *mkf) {
	return (mkf->head==mkf->tail);
	}

int mkfread(mkf *mkf,char *buf,size_t n) {
	mkflock(mkf);
	if(mkf->head==mkf->tail) {mkfunlock(mkf); return 0;};
	char *ptr=mkf->entry[mkf->head];
	strncpy(buf,ptr,n);
	buf[n]=(char)0;
	mkf->head++;
	mkf->head%=MKF_MAXENTRIES;
	free(ptr);
	mkfunlock(mkf);
	return 1;
	}

int mkfreadtail(mkf *mkf,char *buf,size_t n) {
	mkflock(mkf);
	if(mkf->head==mkf->tail) {mkfunlock(mkf); return 0;};
	char *ptr=mkf->entry[mkf->tail];
	strncpy(buf,ptr,n);
	buf[n]=(char)0;
	mkf->tail--;
	if(mkf->tail<0) mkf->tail=MKF_MAXENTRIES-1;
	free(ptr);
	mkfunlock(mkf);
	return 1;
	}

char *mkfpeek(mkf *mkf) {
	if(mkf->head==mkf->tail) {return NULL;};
	return mkf->entry[mkf->head];
	}

char* mkfpeektail(mkf *mkf) {
	if(mkf->head==mkf->tail) {return NULL;};
	return mkf->entry[mkf->tail];
	}

int mkfflush(mkf *mkf) {
	mkflock(mkf);
	int slot;
	for(slot=mkf->head;slot!=mkf->tail;slot=(slot+1)%MKF_MAXENTRIES) 
		free(mkf->entry[slot]);
	mkf->head=mkf->tail;
	mkfunlock(mkf);
	return 1;
	}

int mkflock(mkf *mkf) {
	}

int mkfunlock(mkf *mkf) {
	}

