//	ggstd.h		gg630504 hg9ieg linux.ubuntu.gcc
//	2009-10-10 - 2025-11-09

#ifndef _ggstd_
#define _ggstd_

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


#define NullPtrChk	1

#define szPrgr		"gg630504.hg9ieg"

enum { fFalse, fTrue, fLim };	// flag, logical

enum {
lanHun,	// hungarian
lanEng,	// english
lanLim
};

#define chSwitch	'-'

#define ctZA		('Z'-'A'+1)
#define szSwitch	"-"

#define chEol		'\n'	// end of line
#define chEos		'\0'	// end of string

#define cuEof		EOF	// end of file
#define cuEol		'\n'	// end of line
#define cuEos		0	// end of string

#define mcEof		EOF	// end of file
#define mcEol		'\n'	// end of line
#define mcEos		0	// end of string

#define u1Min	0
#define u1Max	255
#define w1Max	127
#define w1Min	(-128)

#define u2Min	0
#define u2Max	65535
#define w2Max	32767
#define w2Min	(-32768)


#define u4Min	0
#define u4Max	4294967295
#define w4Max	2147483647
#define w4Min	(-2147483648)

#define u8Min	0
#define u8Max	18446744073709551615
#define w8Max	9223372036854775807
#define w8Min	(-9223372036854775808)




#define rSucceed	0	// succeed
#define rEof		EOF

#define ctLinLim	65537		// length of text line
#define ctLinUtfLim	(4*(ctLinLim))	// length of utf-8 decoded text line
//CH		chLinMp[ctLinLim];
//CH		chLinMp[ctLinUtfLim];
//		(saLin = malloc( ctLinLim ))
//		(saLin = malloc( ctLinUtfLim ))

#define ixFldLim	32	// csv, xml


enum {
rFail=-1000,	// fail

rClose,		// error in file close, closed
rFound,		// found
rMkdir,		// make directory
rNotDir,	// is not directory
rNotFound,	// not found
rNotMemo,	// no enought memory
rNotRx,		// no receive
rNotTx,		// no transmit
rNull,		// NULL
rOpen,		// file open, opened
rParam,		// parameter error
rRead,		// file read
rRecursive,	// recursion
rRename,	// file rename
rSeek,		// file seek
rTimeOut,	// time out
rType,		// type error
rWrite		// file write
};

#define XBitGetXBbCt( x_, bb_, ct_ )		( ((x_)&((bb_)<<(ct_)))>>(ct_) )
#define XBitSetXYBbCt( x_, y_, bb_, ct_ )	( ((xx_)&~((bb_)<<(ct_)) | (((y_)<<(ct_))&(bb_)) )
#define XBitUpdXYBbCt( x_, y_, bb_, ct_ )	( (xx_) = ( ((xx_)&~((bb_)<<(ct_)) | (((y_)<<(ct_))&((bb_)<<(ct_)) ) )


#if BYTE_ORDER==LITTLE_ENDIAN

	// host from big endian ( swap )
#define EndianPh2Pb2( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[1];\
	((PU1)pd_)[1] = ((PU1)ps_)[0];}

#define EndianPh3Pb3( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[2];\
	((PU1)pd_)[1] = ((PU1)ps_)[1];\
	((PU1)pd_)[2] = ((PU1)ps_)[0];}

#define EndianPh4Pb3( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[2];\
	((PU1)pd_)[1] = ((PU1)ps_)[1];\
	((PU1)pd_)[2] = ((PU1)ps_)[0];\
	((PU1)pd_)[3] = 0;}

#define EndianPh4Pb4( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[3];\
	((PU1)pd_)[1] = ((PU1)ps_)[2];\
	((PU1)pd_)[2] = ((PU1)ps_)[1];\
	((PU1)pd_)[3] = ((PU1)ps_)[0];}

	// big endian from host ( swap )
#define EndianPb2Ph2( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[1];\
	((PU1)pd_)[1] = ((PU1)ps_)[0];}

#define EndianPb3Ph3( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[2];\
	((PU1)pd_)[1] = ((PU1)ps_)[1];\
	((PU1)pd_)[2] = ((PU1)ps_)[0];}

#define EndianPb4Ph4( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[3];\
	((PU1)pd_)[1] = ((PU1)ps_)[2];\
	((PU1)pd_)[2] = ((PU1)ps_)[1];\
	((PU1)pd_)[3] = ((PU1)ps_)[0];}

#else	// BYTE_ORDER==BIG_ENDIAN

	// host from big endian ( no swap )
#define EndianPh2Pb2( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[0];\
	((PU1)pd_)[1] = ((PU1)ps_)[1];}

#define EndianPh3Pb3( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[0];\
	((PU1)pd_)[1] = ((PU1)ps_)[1];\
	((PU1)pd_)[2] = ((PU1)ps_)[2];}

#define EndianPh4Pb4( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[0];\
	((PU1)pd_)[1] = ((PU1)ps_)[1];\
	((PU1)pd_)[2] = ((PU1)ps_)[2];\
	((PU1)pd_)[3] = ((PU1)ps_)[3];}

	// big endian from host ( no swap )
#define EndianPb2Ph2( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[0];\
	((PU1)pd_)[1] = ((PU1)ps_)[1];}

#define EndianPb3Ph3( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[0];\
	((PU1)pd_)[1] = ((PU1)ps_)[1];\
	((PU1)pd_)[2] = ((PU1)ps_)[2];}

#define EndianPb4Ph4( pd_, ps_ ) {\
	((PU1)pd_)[0] = ((PU1)ps_)[0];\
	((PU1)pd_)[1] = ((PU1)ps_)[1];\
	((PU1)pd_)[2] = ((PU1)ps_)[2];\
	((PU1)pd_)[3] = ((PU1)ps_)[3];}





#endif

// <complex.h>			I;
// complex			C;

typedef char signed		W1;	// 1 byte
typedef char unsigned		U1;
typedef char unsigned		B1;	// bits

typedef int short signed	W2;	// 2 byte
typedef int short unsigned	U2;
typedef int short unsigned	B2;

typedef int signed		W4;	// 4 byte
typedef int unsigned		U4;
typedef int unsigned		B4;

typedef int long long signed	W8;	// 8 byte
typedef int long long unsigned	U8;
typedef int long long unsigned	B8;


typedef int			BW;	// 4	bit word


typedef char			CH;	// 1	char
typedef char signed		CHS;	// 1	signed char
typedef char unsigned		F;	// 1	flag
typedef char unsigned		CHU;	// 1	unsigned char
typedef double			D;	// 8	double
typedef int			CU;	// 4	unicode char
typedef int			LAN;	// 4	language
typedef int long long		WL;	//LW;	// 8	long word
typedef int long long unsigned	WLLU;	//LU;	// 8	unsigned long word
typedef int			MC;	// 4	metachar = char + chEof
typedef int			R;	// 4	return code, < 0 error, >= 0 ok
typedef int			SC;	// 4	sort code
typedef int short		WS;	//SW;	// 2	short word
typedef int short unsigned	WSU;	//SU;	// 2	unsigned short int
typedef int unsigned		WU;	//U;	// 4	unsigned int
typedef int			W;	// 4	word
typedef off_t			FS;	// 8	file size stat.st_size
//typedef long long int		LI;	// 8	long index
typedef size_t			SU;	// 4	size, unsigned
typedef ssize_t			CCH;	// 4	count of char
typedef ssize_t			CT;	// 4	count ( ! c komplex.h )
typedef ssize_t			ICH;	// 4	index of char
typedef ssize_t			IX;	// 4	index ( ! I komplex.h )
typedef ssize_t			SS;	// 4	size, signed
typedef void			V;	// 0	void


//      *                          4
typedef BW *		PBW;	//
typedef CH *		PCH;	// pointer to char		karakterre mutató
typedef CH *		SA;	// memory allocated string	lefoglalt szöveg
typedef CH *		SZ;	// zero terminated string	nullával lezárt szöveg
typedef CU *		PCU;	// pointer to unicode		unikódra mutató
typedef CU *		SZU;	// zero terminated unicode	nullával lezárt unikód szöveg
typedef CU *		SUA;	// memory allocated zero terminated unicode	nullával lezárt unikód szöveg
typedef F *		PF;	// pointer to flag
typedef IX *		PIX;	// pointer to index		indexre mutató
typedef IX *		PIXZ;	//
//typedef WLLU *		PWLLU; //PLU;
typedef CHS *		SSZ;	// zero terminated signed string
typedef SC *		PSCZ;	// pointer to sort code
typedef WSU *		PWSU;
typedef U1 *		PU1;	
typedef U2 *		PU2;	
typedef U4 *		PU4;
typedef U8 *		PU8;
typedef WU *		PU;	// pointer to unsigned int	előjel nélküli egészre mutató
typedef V *		PV;	// pointer to void		típustalanra mutató
typedef W *		PW;	// pointer to word		szóra mutató
typedef W *		PWZ;	//





typedef SZ *		PSZ;	// pointer to zero terminated string
typedef SA *		PSA;
typedef SZU *		PSZU;	// pointer to zero terminated unicode
typedef SSZ *		PSSZ;	//


//-----------------------------------------------------------------------
typedef struct		{
SA			sa;	// lefoglalt szöveg
SS			s;	// hossz
}			SAS;

typedef SAS *		PSAS;

SA	SaMallocPsasS( PSAS, SS );	// 
SA	SaMallocSz( SZ );		// strcpy( malloc() )

//-----------------------------------------------------------------------

typedef struct		{
PV			pv;
SS			ss;		// size of an element
IX			ixMax;		// number of elements used
IX			ixLim;		// number of allocated elements
}			RA;

typedef RA *		PRA;

#define PvPraIx( pra_, ix_ ) ((char *)((pra_)->pv)+((ix_)*(pra_)->ss))

R	RFreePra( PRA );
R	RInitPraSs( PRA, SS );
R	RPushPraPv( PRA, PV );


//-----------------------------------------------------------------------

typedef int		WSortPvPv( const void *, const void * );	// for sort()

extern SZ		szEmpty;	// ""
extern LAN		lanCur;		// { lanHun, hanEng }

//----------------------------------------------------------------------

D	DSqrD( D );			// d^2
SZ	SzErrorR( R );


U2	U2HomePu2host( PU1 );
U4	U4HomePu3host( PU1 );
U4	U4HomePu4host( PU1 );

int	main( int, char**, char** );

//----------------------------------------------------------------------

#if NullPtrChk
#define retnull( p_ )		{ if( (p_)==NULL )\
	{ fprintf( stderr, "[%s:%d %s()] " #p_ " NULL\n", __FILE__, __LINE__, __func__ );\
	r=rNull; goto ret;} }
#else
#define retnull( p_ )
#endif

#define retparam( x_ )		{ if( x_ )\
	{ fprintf( stderr, "[%s:%d %s()] " #x_ " Param\n", __FILE__, __LINE__, __func__ );\
	r=rParam; goto ret;} }

#define ifmalloc( p_, c_ )	if( ((p_)=malloc(c_))==NULL ) { r = rNotMemo; goto ret; }


#define ifsamalloc( d_, s_ )	if( ((d_)=SaMallocSz(s_))==NULL) { r = rNotMemo; goto ret; }

#define iffree( p_ )		{ if( (p_)!=NULL ) { free(p_); (p_)=NULL;} }

#endif

