#ifndef __MGACLIENT__
#define __MGACLIENT__

#ifndef MGAEXT_CARBON_LIB

#ifndef	__XNET__
#include	<xnelib.h>
#endif

#endif

#define	MGACLILIB_NAMESTR	"mgacli8"

#define	MGACLILIB_VERSION	0x00017700
#define	MGAEXTLIB_VERSION	MGACLILIB_VERSION		// per compatibilita'

#define	MGACLILIB_VERSION_MASK	0xffffff00
#define	MASKED_MGACLILIB_VERSION	(MGACLILIB_VERSION & MGACLILIB_VERSION_MASK)

#define MGAEXT_AVOID_INIT_LIB	1


#ifdef SYD_WIN

#define MGAEXT_EXTERN_CALL __declspec(dllexport)

#else	// SYD_MAC o SYD_OSX

#ifdef TARGET_API_MAC_CARBON

#ifdef MGAEXT_CARBON_LIB

#define MGAEXT_EXTERN_CALL	

#else

#define MGAEXT_EXTERN_CALL	extern

#endif // MGAEXT_CARBON_LIB

#else

#define MGAEXT_EXTERN_CALL	

#endif	// TARGET_API_MAC_CARBON

#endif	// SYD_WIN

#ifndef SYD_WIN
MGAEXT_EXTERN_CALL void ZeroSy2Image(register Sy2Image *d);
MGAEXT_EXTERN_CALL int Sy2DPConvert(register Sy2DataPointer *s, register Sy2DataPointer *d);
MGAEXT_EXTERN_CALL int Sy2ImageElemToDataPointerEx(register Sy2DataPointer *dp, register Sy2Image *imgp, Sy2ExtendedFieldSpec *efs, int variant);
MGAEXT_EXTERN_CALL void BufToBin(int blen, register char *s, register int n, register Sy2UBYTE *g, Sy2UBYTE *spare);
#endif	// SYD_WIN



MGAEXT_EXTERN_CALL void MGAClientInit(MGACallBackProc mgacbp);

MGAEXT_EXTERN_CALL int MGAOpen(int serverid);
MGAEXT_EXTERN_CALL MGALocalRequest *MGAInitNewReq(MGALocalRequest *mgahp);
MGAEXT_EXTERN_CALL int MGADestroyReq(MGALocalRequest *mgahp);
MGAEXT_EXTERN_CALL MGALocalRequest *MGAAppendData2Req(MGALocalRequest *hp, void *bufp, long bufsize);
MGAEXT_EXTERN_CALL MGALocalRequest *MGAAppendRAWData2Req(MGALocalRequest *hp, void *bufp, long bufsize);
MGAEXT_EXTERN_CALL char *MGAGetMasterPtr(MGALocalRequest *hp);
MGAEXT_EXTERN_CALL char *MGAGetDetailPtr(MGALocalRequest *hp);
MGAEXT_EXTERN_CALL char *MGAGetRAWDataPtr(MGALocalRequest *hp);
MGAEXT_EXTERN_CALL void MGAClose(void);
MGAEXT_EXTERN_CALL void MGAProcessReq(MGALocalRequest *reqp);
MGAEXT_EXTERN_CALL void MGAAsyncReq(MGALocalRequest *reqp);

MGAEXT_EXTERN_CALL int MGAGetRecordFromRecID(long archname, long recn, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAGetVariantFromRecID(long archname, long recn, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAGetRecordFromKey(long archname, long keyname, void *keyp, int keylen, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGADBDecode(long archname, long keyname, void *keyp, int keylen, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAGetRecDataFromKey(long archname, long keyname, void *keyp, int keylen, void *bufp, int buflen, long *recidp);
MGAEXT_EXTERN_CALL int MGAGetArchFromRecID(long archname, long recn, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAGetArchFromKey(long archname, long keyname, void *keyp, int keylen, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAGetArchDataFromKey(long archname, long keyname, void *keyp, int keylen, void *bufp, int buflen, long *recidp);
MGAEXT_EXTERN_CALL int MGAGetArchFromREFRecID(long archname, long recn, long ref, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAGetArchFromREFKey(long archname, long keyname, void *keyp, int keylen, long ref, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAGetArchDataFromREFKey(long archname, long keyname, void *keyp, int keylen, long ref, void *bufp, int buflen, long *recidp);
MGAEXT_EXTERN_CALL int MGAPutRecordFromRecID(long name, long recn, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAPutArchFromRecID(long name, long recn, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAPutArchFromREFRecID(long name, long recn, void *bufp, int buflen, long ref);
MGAEXT_EXTERN_CALL long MGAKey2RecSpec(long dbname, long nkey, void *datap, int datalen, RecSpec *rsp);
MGAEXT_EXTERN_CALL long MGAKeyLocateRecSpec(long dbname, long nkey, void *datap, int datalen, RecSpec *rsp);
MGAEXT_EXTERN_CALL long MGAKey2RecID(long dbname, long nkey, void *datap, int datalen);
MGAEXT_EXTERN_CALL int MGAKeyExists(long dbname, long nkey, void *datap, int datalen);
MGAEXT_EXTERN_CALL void MGAUpdate_Local_CmmnData(void *cmmndatap, int cmmndatalen);
MGAEXT_EXTERN_CALL void MGAWrite_CmmnData(void *cmmndatap, int cmmndatalen);
MGAEXT_EXTERN_CALL void MGAUpdate_Local_Systatus(void *systatusp, int systatuslen);
MGAEXT_EXTERN_CALL void MGAWrite_Systatus(void *systatusp, int systatuslen);
MGAEXT_EXTERN_CALL void MGAUpdate_Local_CoFix(void *cofixp, int cofixlen);
MGAEXT_EXTERN_CALL long MGAFindPartialKeys(long archname, long keyname, void *keyp, int keylen, int startswith, DDSRef *dhp, RecSpec *rsp);

struct _offsrc_header;
MGAEXT_EXTERN_CALL long MGAFindFromSD(long archname, struct _offsrc_header *shp, int sdlen, DDSRef *dhp, RecSpec *rsp);
MGAEXT_EXTERN_CALL void MGAFillSimpleHeader(MGALocalRequest *hp, long name, short action, long rec_id);
MGAEXT_EXTERN_CALL long MGAScanOpen(long dbname, long keyname);
MGAEXT_EXTERN_CALL long MGAScanArchOpen(long dbname, long keyname);
MGAEXT_EXTERN_CALL long MGAScanArchOpenREF(long dbname, long keyname, long ref);
MGAEXT_EXTERN_CALL long MGAScanClose(long scanid);
MGAEXT_EXTERN_CALL int MGAScanNext(long dbname, long scanid, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAScanNextVariant(long dbname, long scanid, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAScanLocate(long dbname, long scanid, void *keyp, int keylen, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAScanFind(long dbname, long scanid, void *keyp, int keylen, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAScanArchNext(long dbname, long scanid, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAScanArchLocate(long dbname, long scanid, void *keyp, int keylen, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAGetNthDetail(long archname, long ref, short nrow, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAGetNthVariantDetail(long archname, long ref, short nrow, void *bufp, int buflen);
MGAEXT_EXTERN_CALL int MGAWrite_Notes(long archname, long recid, void *cp, long codesize, long numint, int isnew, DDSRef *ndhp);
MGAEXT_EXTERN_CALL int MGARead_Notes(long archname, long recid, DDSRef *ndhp);
MGAEXT_EXTERN_CALL int MGARead_Notes2(long archname, char *verocodep, int verocodelen, DDSRef *ndhp);

MGAEXT_EXTERN_CALL int MGAKillArchFromRecID(long archname, long recn);
MGAEXT_EXTERN_CALL int MGALock(long archname, long recid);
MGAEXT_EXTERN_CALL void MGAUnlock(long archname, long recid);
MGAEXT_EXTERN_CALL int MGALockEx(long archname, long recid, long *sessionid_lock_ownerp);
MGAEXT_EXTERN_CALL int MGAAuthenticateUser(char *username, char *passw, long *rec_idp);
MGAEXT_EXTERN_CALL int MGANeedsUserName(void);
MGAEXT_EXTERN_CALL int MGAKillDeleting(long archname, long recn);
MGAEXT_EXTERN_CALL int MGASendMessage(char *s, short destination);
MGAEXT_EXTERN_CALL int MGAClientValidateConfPGM(void *cli_confpgmp, int confpgmlen);
MGAEXT_EXTERN_CALL int MGAClientSetConfPGM(void *cli_confpgmp, int confpgmlen);
MGAEXT_EXTERN_CALL int MGAClientGetConfPGM(void *cli_confpgmp, int confpgmlen);


MGAEXT_EXTERN_CALL void MGASetMessagesMode(int mode);
MGAEXT_EXTERN_CALL void MGASetLoginState(int enable);
MGAEXT_EXTERN_CALL int MGAUpdateULXX(long ulxx, int is_mg);
MGAEXT_EXTERN_CALL int MGADeclareClientState(long state);
MGAEXT_EXTERN_CALL int MGAGetServerStats(MGAServerStats *mgassp);
MGAEXT_EXTERN_CALL int MGAGetClientStats(MGAClientStats *mgacsp);
MGAEXT_EXTERN_CALL int MGAUpdateArch(long dbname, int variant, void *masterp, int masterlen, long recid, int checkonly);

MGAEXT_EXTERN_CALL int MGAAddMasterDetailEx(long dbname, int variant, void *masterp, int masterlen, void *detailp, int detaillen, int numdetails, int checkonly, long keyname, long *nip);
MGAEXT_EXTERN_CALL int MGAAddMasterDetail(long dbname, int variant, void *masterp, int masterlen, void *detailp, int detaillen, int numdetails, int checkonly, long *nip);
MGAEXT_EXTERN_CALL int MGAGetKeyInfo(long archname, void *kip, long kipsize, long *kilenp);
MGAEXT_EXTERN_CALL int MGAGetDBStatus(long archname, int azienda, struct db_status *dbstatp);
MGAEXT_EXTERN_CALL int MGACLQuery(char *querystr, DDSRef *dhp, int async);


MGAEXT_EXTERN_CALL long MGASqlQueryOpen(char *querystrp);
MGAEXT_EXTERN_CALL int MGASqlQueryClose(long queryid);
MGAEXT_EXTERN_CALL long MGANumRowsInQuery(long queryid);
MGAEXT_EXTERN_CALL int MGAFetchNextRowFromQuery(long queryid, MGASqlRow *sqlrp);
MGAEXT_EXTERN_CALL MGASqlRow *MGANewSqlRow();
MGAEXT_EXTERN_CALL int MGADisposeSqlRow(MGASqlRow *sqlrp);
MGAEXT_EXTERN_CALL int MGAGetFieldFromSqlRow(int n, char *s,MGASqlRow *sqlrp, int maxlen);

MGAEXT_EXTERN_CALL long MGALogin(char *actkeyp, unsigned short thepgm_type, unsigned short thepgm_vers);
MGAEXT_EXTERN_CALL long MGALoginToFirstServer(char *actkeyp, XNETQuerySpinProc spinproc, long par, unsigned short thepgm_type, unsigned short thepgm_vers);
MGAEXT_EXTERN_CALL int MGAGetServerList(XNETLocalNameServerInfo *bcip, int max, int querytime, XNETQuerySpinProc spinproc, int mga_tcp_max_multiprograms, int mga_tcp_offset_multiprograms, long par);
MGAEXT_EXTERN_CALL void MGASetServer(XNETLocalNameServerInfo *bcip);
MGAEXT_EXTERN_CALL int MGALogout(void);
MGAEXT_EXTERN_CALL int MGAClientNum(void);
MGAEXT_EXTERN_CALL int MGAClientDataFromNum(struct XNETClientEntry *cep, int num, int mode);

MGAEXT_EXTERN_CALL int MGAClientStart(int mode, int key_num_pp, int flags);
MGAEXT_EXTERN_CALL void MGAClientEnd(void);
MGAEXT_EXTERN_CALL void MGAClientSetUserID(long user_id);
MGAEXT_EXTERN_CALL void MGAClientSetREF(long ref);
MGAEXT_EXTERN_CALL int MGAClientSetCurrentAzienda(int azienda);	// 1 based

MGAEXT_EXTERN_CALL void MGAClientIdle(void);

// questi per compatibilita' con i vecchi nomi
MGAEXT_EXTERN_CALL void MGAInit(MGACallBackProc mgacbp);
MGAEXT_EXTERN_CALL int MGAStart(int mode, int key_num_pp, int flags);
MGAEXT_EXTERN_CALL void MGAEnd(void);
MGAEXT_EXTERN_CALL int MGASetCurrentAzienda(int azienda);	// 1 based

MGAEXT_EXTERN_CALL void MGADeleteAndFix(long archname, int azienda, long recid);


MGAEXT_EXTERN_CALL long MGAExtOpenServices(int mode);
MGAEXT_EXTERN_CALL int MGAExtCloseServices(void);
MGAEXT_EXTERN_CALL long MGAExtLogin(char *actkeyp);


MGAEXT_EXTERN_CALL int MGAConfirmDOC(long archname, long numint);
MGAEXT_EXTERN_CALL void MGASetCurrentClientType(int type);
MGAEXT_EXTERN_CALL long MGAClientGetLibVersion(void);
MGAEXT_EXTERN_CALL void MGAClientGetLibVersionCString(char *s, int maxsize);

#define MGAExtLogout()	MGALogout()


MGAEXT_EXTERN_CALL int MGAStringToDate(char *p, int len, int comply2000, Sy2Date *pdate);
MGAEXT_EXTERN_CALL int MGADateToString(Sy2Date dat, register char *p, int mode);
MGAEXT_EXTERN_CALL int MGATimeToString(Sy2Time xtim, register char *p, int mode);
MGAEXT_EXTERN_CALL int MGAStringToTime(char *p, int len, Sy2Time *rp);
MGAEXT_EXTERN_CALL void MGABufToFixed(int fixlen, int decimals, int mode, register char *s, register int n, register Sy2UBYTE *g);
MGAEXT_EXTERN_CALL int MGAFixedToCString(int fixlen, Sy2UBYTE *fp, register char *s, int maxlen);
MGAEXT_EXTERN_CALL int MGACopyFixed(Sy2UBYTE *a, int la, Sy2UBYTE *b, int lb);
MGAEXT_EXTERN_CALL void MGABufToBin(int blen, register char *s, register int n, register Sy2UBYTE *g, Sy2UBYTE *spare);
MGAEXT_EXTERN_CALL int MGABinToCString(int blen, Sy2UBYTE *g, register char *s, int maxlen, Sy2UBYTE *spare);
MGAEXT_EXTERN_CALL Sy2UDWord MGAOpenSimpleStopWatch(Sy2SimpleStopWatchData *swdata, int domicrosec);
MGAEXT_EXTERN_CALL int MGAStartSimpleStopWatch(Sy2SimpleStopWatchData *swdata);
MGAEXT_EXTERN_CALL Sy2DWord MGAStopSimpleStopWatch(Sy2SimpleStopWatchData *swdata);
MGAEXT_EXTERN_CALL Sy2DWord MGAGetSimpleStopWatch(Sy2SimpleStopWatchData *swdata);
MGAEXT_EXTERN_CALL void MGACloseSimpleStopWatch(Sy2SimpleStopWatchData *swdata);
MGAEXT_EXTERN_CALL int MGAGetAsyncModifiers(void);

MGAEXT_EXTERN_CALL void MGALongToBin(int blen, long l, Sy2UBYTE *v1);
MGAEXT_EXTERN_CALL int MGAFixedToBin(int mode, Sy2UBYTE *fp, int fixlen, Sy2UBYTE *bp, int blen);
MGAEXT_EXTERN_CALL int MGALongToFixed(int decimals, long n, Sy2UBYTE *fp, int fixlen);
MGAEXT_EXTERN_CALL int MGADDSCreate(DDSRef *dhp);
MGAEXT_EXTERN_CALL void MGADDSDispose(DDSRef *dhp);
MGAEXT_EXTERN_CALL void *MGADDSLock(DDSRef *dhp, long offset);
MGAEXT_EXTERN_CALL void MGADDSUnlock(DDSRef *dhp);



#endif	/* __MGACLIENT__ */
