/*
 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 *  SCR_Cxt.h
 *  Scripts, Beaudet Christophe
 *%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 *
 */
 
#ifndef __SCR_Cxt_h__Types
#define __SCR_Cxt_h__Types
 
#ifndef __Only_Types__
#define __SCR_Cxt_h__Undef
#define __Only_Types__
#endif /* !__Only_Types__ */
 
/*
 *=================================================================================================
 *  Includes.
 *=================================================================================================
 */
 
#include "SCR_Cfg.h"
#include "SCR_DyAr.h"
#include "SCR_Bin.h"
 
#ifdef __SCR_Cxt_h__Undef
#undef __Only_Types__
#undef __SCR_Cxt_h__Undef
#endif /* __SCR_Cxt_h__Undef */
 
/*
 *=================================================================================================
 *  Constants.
 *=================================================================================================
 */
 
/*
 *  To export code.
 */
#undef CPA_EXPORT
#if defined(CPA_WANTS_IMPORT)
#define CPA_EXPORT __declspec(dllimport)
#elif defined(CPA_WANTS_EXPORT)
#define CPA_EXPORT __declspec(dllexport)
#else /* CPA_WANTS_IMPORT */
#define CPA_EXPORT
#endif /* CPA_WANTS_IMPORT */
 
/*
 *=================================================================================================
 *  Types.
 *=================================================================================================
 */
 
/*
 *  To define a current position in file.
 */
typedef struct SCR_tdst_Cxt_Position_
{
    unsigned int    uiLineInFile;           /* Line in script file */
    long            lAfterSeekInFile;       /* Position after line in file */
    unsigned int    uiLevel;                /* Level of section */
} SCR_tdst_Cxt_Position;
 
/*
 *  All buffers.
 *	Static ones.
 */ 
typedef struct SCR_tdst_Cxt_Buffers_
{
    char    a_szBufferCompleteName[SCR_CV_ui_Cfg_MaxLenName];
    char    a_szBufferName[SCR_CV_ui_Cfg_MaxLenAtomicName];
    char    a_szBufferNameExt[SCR_CV_ui_Cfg_MaxLenWord];
    char    a_szBufferFormat[SCR_CV_ui_Cfg_MaxLenFormat];
    char    a_szBufferParams[SCR_CV_ui_Cfg_MaxLenParams];
} SCR_tdst_Cxt_Buffers;
 
/* Dynmamic ones : When size of data inside won't change */
typedef struct SCR_tdst_Cxt_DynBuffers_
{
    char    *p_szBufferCompleteName;
    char    *p_szBufferName;
    char    *p_szBufferNameExt;
    char    a_szBufferFormat[SCR_CV_ui_Cfg_MaxLenFormat];		/* Always static */
    char    a_szBufferParams[SCR_CV_ui_Cfg_MaxLenParams];		/* Always static */
} SCR_tdst_Cxt_DynBuffers;
 
/* 
 *  Structure that describes a result.
 */
typedef struct SCR_tdst_Cxt_Values_
{
    unsigned long   a_ulValues[SCR_CV_ui_Cfg_MaxResults];       /* Unsigned long values */
    double          a_dfValues[SCR_CV_ui_Cfg_MaxResults];       /* Double values */
} SCR_tdst_Cxt_Values;
 
/*
 *  To describe what is in the buffers of parser.
 */
typedef enum SCR_tde_Cxt_ParseType_
{
    SCR_EPT_Cxt_None,
	SCR_EPT_Cxt_EOF, /* MP: BINARY*/
    SCR_EPT_Cxt_BeginSection,
    SCR_EPT_Cxt_EndSection,
    SCR_EPT_Cxt_Entry,
    SCR_EPT_Cxt_Directive,
} SCR_tde_Cxt_ParseType;
 
/*
 *  Structure that describes a context.
 */
typedef struct SCR_tdst_Cxt_Description_
{
	SCR_tdst_DyAr_Header				stHeader;				/* Header of element */
    char                                cForSection;            /* It's a file or a section */
    SCR_tdst_Cxt_Buffers                stBuffers;              /* All buffers */
    SCR_tdst_Cxt_Buffers                stBuffersCopy;          /* All buffers */
    SCR_tdst_Cxt_Position               stPosition;             /* Current position in file */
	struct SCR_tdst_File_Open_          *p_stOpenFile;          /* Current open file */
    struct SCR_tdst_Anl_SectionDes_     *p_stSectionDes;        /* Current section description */
    struct SCR_tdst_Sect_Open_          *p_stOpenSection;       /* Current open section */
    SCR_tdst_Cxt_Values                 stContextValues;        /* Results for that context */
    SCR_tdst_Cxt_Values                 stFileValues;           /* Results for open file */
    SCR_tdst_Cxt_Values                 stSectionValues;        /* Results for open section */
    char                                cFileValuesValid;       /* Values for file are valid */
    char                                cSectionValuesValid;    /* Values for section are valid */
    unsigned short                      uwDynamicFlags;         /* Current dynamic flags */
    SCR_tde_Cxt_ParseType               eParseType;             /* Type of parsing results */
} SCR_tdst_Cxt_Description;
 
#endif /* !__SCR_Cxt_h__Types */
 
/*
 *=================================================================================================
 *  Global variables.
 *=================================================================================================
 */
 
#if !defined(__SCR_Cxt_h__Globals) && !defined(__Only_Types__)
#define __SCR_Cxt_h__Globals
 
CPA_EXPORT extern SCR_tdst_DyAr_Description     SCR_g_st_Cxt_Array;
 
#endif /* !__SCR_Cxt_h__Globals && !__Only_Types__ */
 
/*
 *=================================================================================================
 *  Macros.
 *=================================================================================================
 */
 
#if !defined(__SCR_Cxt_h__Macros) && !defined(__Only_Types__)
#define __SCR_Cxt_h__Macros
 
/*
 *  Copy a buffer to another.
 */
#define SCR_M_Cxt_CopyBuffers(_Dest, _Src)\
{\
    strcpy((_Dest)->a_szBufferCompleteName, (_Src)->a_szBufferCompleteName);\
    strcpy((_Dest)->a_szBufferName, (_Src)->a_szBufferName);\
    strcpy((_Dest)->a_szBufferNameExt, (_Src)->a_szBufferNameExt);\
    memcpy((_Dest)->a_szBufferFormat, (_Src)->a_szBufferFormat, SCR_CV_ui_Cfg_MaxLenLine * sizeof(char));\
    memcpy((_Dest)->a_szBufferParams, (_Src)->a_szBufferParams, SCR_CV_ui_Cfg_MaxLenLine * sizeof(char));\
}
 
/* With allocations */
#define SCR_M_Cxt_CopyBuffersToDyn(_Dest, _Src)\
{\
	SCR_M_Mem_Alloc(char, (_Dest)->p_szBufferCompleteName, strlen((_Src)->a_szBufferCompleteName) + 1);\
	SCR_M_Mem_Alloc(char, (_Dest)->p_szBufferName, strlen((_Src)->a_szBufferName) + 1);\
	SCR_M_Mem_Alloc(char, (_Dest)->p_szBufferNameExt, strlen((_Src)->a_szBufferNameExt) + 1);\
    strcpy((_Dest)->p_szBufferCompleteName, (_Src)->a_szBufferCompleteName);\
    strcpy((_Dest)->p_szBufferName, (_Src)->a_szBufferName);\
    strcpy((_Dest)->p_szBufferNameExt, (_Src)->a_szBufferNameExt);\
    memcpy((_Dest)->a_szBufferFormat, (_Src)->a_szBufferFormat, SCR_CV_ui_Cfg_MaxLenLine * sizeof(char));\
    memcpy((_Dest)->a_szBufferParams, (_Src)->a_szBufferParams, SCR_CV_ui_Cfg_MaxLenLine * sizeof(char));\
}
 
#define SCR_M_Cxt_FreeDynBuffers(_Src)\
{\
	SCR_M_Mem_Free((_Src)->p_szBufferCompleteName);\
	SCR_M_Mem_Free((_Src)->p_szBufferName);\
	SCR_M_Mem_Free((_Src)->p_szBufferNameExt);\
}
 
#define SCR_M_Cxt_CopyDynToBuffers(_Dest, _Src)\
{\
    strcpy((_Dest)->a_szBufferCompleteName, (_Src)->p_szBufferCompleteName);\
    strcpy((_Dest)->a_szBufferName, (_Src)->p_szBufferName);\
    strcpy((_Dest)->a_szBufferNameExt, (_Src)->p_szBufferNameExt);\
    memcpy((_Dest)->a_szBufferFormat, (_Src)->a_szBufferFormat, SCR_CV_ui_Cfg_MaxLenLine * sizeof(char));\
    memcpy((_Dest)->a_szBufferParams, (_Src)->a_szBufferParams, SCR_CV_ui_Cfg_MaxLenLine * sizeof(char));\
}
 
/*
 *  Get current context.
 */
#define SCR_M_Cxt_GetCurrentContext() ((int) (SCR_g_st_Cxt_Array.uiNumValues - 1))
 
#endif /* !__SCR_Cxt_h__Macros && !__Only_Types__ */
 
/*
 *=================================================================================================
 *  Protos.
 *=================================================================================================
 */
 
#if !defined(__SCR_Cxt_h__Protos) && !defined(__Only_Types__)
#define __SCR_Cxt_h__Protos
 
extern void                                 fn_v_Cxt_InitModule(void);
extern void                                 fn_v_Cxt_CloseModule(void);
 
extern SCR_tdst_Cxt_Description             *fnp_st_Cxt_Add(void);
extern void                                 fn_v_Cxt_DeleteLast(void);
 
CPA_EXPORT extern SCR_tdst_Cxt_Description  *fnp_st_Cxt_Compute(int);
 
#endif /* !__SCR_Cxt_h__Protos && !__Only_Types__ */
