/*
 Module Name: nlib
Programed By: Lynden Sherriff
 Tested Date: 20/1/99
	 Version: 2.0f
*/

#ifndef NLIB_HEADER
#	define NLIB_HEADER


#	include <stdarg.h>	

#	define	INIT_VALUE		32000

#	ifndef NULL
#		define NULL				0
#	endif
#	ifndef TRUE
#		define TRUE				1
#	endif
#	ifndef FALSE
#		define FALSE				0
#	endif

#ifdef WIN32
#	define	NEXT_LINE	"\r\n"
#else
#	define	NEXT_LINE	"\n"
#endif

#	ifdef WIN32
#		define SEP '\\'
#		define STR_SEP "\\"
#		define sleep(a) Sleep(a * 1000)
#		define msleep(a) Sleep(a)
#	endif

#	ifdef unix
#		define SEP '/'
#		define STR_SEP "/"
#		define msleep(a) usleep(a*1000)
	
#		ifndef _MAX_PATH
#			define _MAX_PATH 512
#		endif
#	endif

#	ifdef __MWERKS__
#		define mac TRUE
#	endif

#	ifdef mac
#		define SEP ':'
#		define STR_SEP ":"
#	endif


#	ifndef LBUFSZ
#		define	LBUFSZ		1024
#	endif
#	ifndef BFSZ
#		define	BFSZ		512
#	endif

/* General Global Functions */
	char *utoken_decode(char *str);
	char *utoken_encode(char *str);

	char *pass_user_encrypt(char *pass, char *user);
	char *pass_user_decrypt(char *encoded, char *user);
							/* Used to encrypt username/password for autologin */

#	ifndef remove_quote
		char *remove_quote(char *str, char *result);
#	endif

#	ifndef ncpy
		char *ncpy(char *dst, char *src, int len);
#	endif
#	ifndef ncat
		char *ncat(char *dst, char *src, int len);
#	endif

	char *lcpy(char *dst, char *src, int len);
	char *lcat(char *dst, char *src, int len);

	int lprintf( char *buffer, int bfsize, char *format, ... );
	int lvprintf( char *buffer, int bfsize, char *format, va_list arg_list);

#	ifdef NEED_STRERROR
		char *strerror(int x);
#	endif

/* These functions are to take care of the NULL enteries */
/* These replace the standard str functions */
	char *s_strcpy( char *string1, char *string2);


int s_strcmp( char *string1, char *string2 );
int s_stricmp( unsigned char *string1, unsigned char *string2);

	int s_strncmp( char *string1, char *string2, int count );
	int s_strnicmp( char *string1, char *string2, int count );
	char *s_strlwr( char *string );
	char *s_strupr( char *string );
	int s_strtrm(char *str);
	char *s_strcat(char *des, char *src,...);
	char *s_stradd(char *base, char *add);

	char *s_strstr( char *string1, char *string2 );
	char *s_strchr( char *string1, char string2 );
	char *s_stristr( char *string1, char *string2 );

	unsigned int s_strlen( char *string1 );

	char *s_strtoken( char *string, char *sep );
	char *mystrtok(char *start, char *toks, char **upto);

	int s_atoi( char *string );
	long s_atol( char *string );
	double s_atof( char *string );
#ifdef WIN32
#	define		s_itoa(x,y) itoa(x,y,10)
#else 
#	define		s_itoa(x,y) sprintf(y,"%d",x)
#endif

/* General Functions */
	char *valid_file(char *file);
	char *update_path(char *path);
	char *attach_path(char *work_path,char *file);
	char *attach_ext(char *path_file, char *ext);
	char *strip_filename(char *in);
	char *get_ext(char *in);
	char *strip_ext(char *in);
	char *strip_path(char *in, int rm_extend);
	char *remove_path(char *path, int want_remove);
	
#	define dot_replace(x)	char_replace(x, '.', '_')
	void char_replace(char *ptr, char search, char with);


	/*******************************************************************************/
	/*******************************************************************************/
	
	
#	include <stdlib.h>
#	include <limits.h>

#	define MAXSTACK (sizeof(size_t) * CHAR_BIT)

	void myqsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));

	/*******************************************************************************/
	/*******************************************************************************/
	/*******************************************************************************/
	/*******************************************************************************/
	

/* MEMORY FUNCTIONS */
/* **************** */
#ifdef _DEBUG
//#	define	_MEMORY_DEBUG
//#	define	_FILE_DEBUG
#endif

#ifdef _MEMORY_DEBUG
	
#	define	MAX_MEM_SAFE_SIZE	20000

	typedef struct mem_item
	{
		void *memory;
		char file[BFSZ];
		int line;
	} MEM_ITEM;

	typedef struct mem_safe
	{
		int sort;
		int cleared;
		int quick_clear;

		int number;
		int max;
		unsigned long count;

		MEM_ITEM memory[MAX_MEM_SAFE_SIZE];
	} MEM_SAFE;

	void *ms_realloc(void *mem, int size, char *file, int line);
	void *ms_malloc(int amount, char *file, int line);
	void ms_free(void *mem, char *file, int line);
	void ms_info(void);
	
#		ifndef myrealloc
#			define myrealloc(x,y)	ms_realloc(x,y,__FILE__,__LINE__)
#		endif
#		ifndef mymalloc
#			define mymalloc(x)			ms_malloc(x,__FILE__,__LINE__)
#		endif
	
#		ifndef myfree
#			define myfree(ptr)			{ms_free(ptr,__FILE__,__LINE__); ptr = NULL;}
#		endif

#else
	/* my alloc requires the following Headers */
	/*	#include <stdlib.h>
		#include <malloc.h>  
	*/

#		ifndef myrealloc
#			define myrealloc		realloc
#		endif

#		ifndef mymalloc
#			define mymalloc(size)		malloc(size)
#		endif
/*
#		ifndef myfree
#			define myfree(ptr)			{free(ptr); ptr = NULL;}
#		endif
*/
#		ifndef myfree
/*			int	emsg		(char * arg_list, ...); */
#			define myfree(ptr)			{ /*emsg("free %p",ptr);*/ free(ptr); ptr = NULL;}
#		endif


#endif

#	ifdef	_FILE_DEBUG
#		include <stdio.h>

#		define	MAX_FILE_SAFE_SIZE	2000

		typedef struct file_item
		{
			FILE *stream;
			char path[BFSZ];
			char file[BFSZ];
			int line;
		} FILE_ITEM;

		typedef struct file_safe
		{
			int sort;
			int cleared;
			int quick_clear;

			int number;
			int max;
			unsigned long count;

			FILE_ITEM file[MAX_FILE_SAFE_SIZE];
		} FILE_SAFE;

		FILE *fs_open(char *path,char *mode, char *file, int line);
		void fs_close(FILE *mem, char *file, int line);
		void fs_info(void);

#	ifdef _FILE_DEBUG
#			ifndef myfopen
#				define myfopen(x,m)			fs_open(x,m,__FILE__,__LINE__)
#			endif
	
#			ifndef myfclose
#				define myfclose(ptr)		fs_close(ptr,__FILE__,__LINE__)
#			endif
#	else
#			ifndef myfopen
#				define myfopen(x,m)			fs_open(x,m,"Unknown",0)
#			endif
	
#			ifndef myfclose
#				define myfclose(ptr)		fs_close(ptr,"Unknown",0)
#			endif
#	endif

#	else

#		ifndef myfopen
#			define myfopen		fopen
#		endif
#		ifndef myfclose
#			define myfclose		fclose
#		endif

#	endif

#	ifndef mystrdup
#		define mystrdup(des,src) { if (src) { des = (char *)mymalloc(strlen(src)+1); if (des) strcpy(des,src); } else des = NULL; }
#	endif

#endif
