/*=========================================================================
*
* LgtLoad.h	- Loading functions for Lgt file
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include "LgtLoad.h"

#include <time.h>

#include "conventi.h"
#include "print.h"
#include "system.h"


//--- Global statics --------------------------------------------------------

MLT_tdstLGT	**gs_hLGTInFile = NULL;
long		  gs_lLGTIndex	= 0;

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

/****************************************************************************
*		Description:	register the loading function for the LGT file
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vInitLGTLoad ()
{
  // Init des scripts et lecture du fichier
  SCR_fn_v_RdL0_RegisterCallback(M_Light,MLT_xLoadLGT, SCR_CRC_c_RdL0_ForSection);
  SCR_fn_v_RdL0_RegisterCallback(M_FichierLGT,MLT_xLoadLGTCallback, SCR_CRC_c_RdL0_ForFile);
}


/****************************************************************************
*		Description:	load LGT section
*
*		Parameters:		p_stFile	: script file pointer
*						szAction	: section or entry name
*						szParams	: parameters
*						cType		: action type
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
SCR_tde_Anl_ReturnValue MLT_xLoadLGT (SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstLGT *p_stLGT;
  MLT_tdstMatrix *p_stMatrix;
  SCR_tdst_Cxt_Values *p_stVal;
  xString sFile, sAction, sIdent;

  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_stLGT=(MLT_tdstLGT*)malloc(sizeof(MLT_tdstLGT));
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_stLGT);
      SCR_M_RdL0_SetContextLong(0,0,(long)p_stLGT);
      sprintf(p_stLGT->sName,"%s",SCR_M_RdL0_GetCompleteSectionNameR(0));
      break;
                
    case SCR_EA_Anl_Entry:
      // Recuperation du LoadGeometric a remplir
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstLGT*,p_stLGT);

      // Loading of object's
      if (strcmp(szAction,M_LGTActionSetMatrix)==0)
      {
	      sprintf(p_stLGT->sSetMatrix,"%s",szParams[0]);

		  SCR_fn_v_RdL0_SplitSectionName(szParams[0], sFile, sAction, sIdent);
		  SCR_fn_v_RdL0_ComputeSectionName(szParams[0], "*", sAction, sIdent);
		  
		  p_stVal = SCR_fnp_st_RdL0_AnalyseSection(szParams[0], SCR_CDF_uw_Anl_Normal);
		  p_stMatrix = (MLT_tdstMatrix*)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);

		  p_stLGT->p_stMatrix = p_stMatrix;
      } 
      break;

    case SCR_EA_Anl_EndSection:
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}

/****************************************************************************
*		Description:	load LGT file
*
*		Parameters:		p_stFile	: script file pointer
*						szAction	: section or entry name
*						szParams	: parameters
*						cType		: action type
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
SCR_tde_Anl_ReturnValue MLT_xLoadLGTCallback (SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
  MLT_tdstLGT **p_xLGTInFile;
  long xNbLGT;
  
  switch (cType)
  {
    case SCR_EA_Anl_BeginSection:
      p_xLGTInFile = (MLT_tdstLGT**)malloc(sizeof(MLT_tdstLGT*));
    
      SCR_M_RdL0_SetContextLong(0,0,(long)p_xLGTInFile);
      SCR_M_RdL0_SetContextLong(0,1,(long)0);
      break;

    case SCR_EA_Anl_EndSubSection:
      if (strcmp(szParams[0],M_Light)==0)
      {
        MLT_tdstLGT *hLGT;

        SCR_M_RdL0_GetContextLong(0,0,MLT_tdstLGT**,p_xLGTInFile);
        SCR_M_RdL0_GetContextLong(0,1,long,xNbLGT);

        p_xLGTInFile = (MLT_tdstLGT**)realloc(p_xLGTInFile,(++xNbLGT)*sizeof(MLT_tdstLGT*));

        SCR_M_RdL0_GetSectionLong(1,0,MLT_tdstLGT*,hLGT);
        p_xLGTInFile[xNbLGT-1]=hLGT;

        SCR_M_RdL0_SetContextLong(0,0,(long)p_xLGTInFile);
        SCR_M_RdL0_SetContextLong(0,1,(long)xNbLGT);
      }
      break;

    case SCR_EA_Anl_EndSection:
      SCR_M_RdL0_GetContextLong(0,0,MLT_tdstLGT**,p_xLGTInFile);
      SCR_M_RdL0_GetContextLong(0,1,long,xNbLGT);
      SCR_M_RdL0_SetSectionLong(0,0,(long)p_xLGTInFile);
      SCR_M_RdL0_SetSectionLong(0,1,(long)xNbLGT);
      break;
  }
  return  SCR_ERV_Anl_NormalReturn;
}



/****************************************************************************
*		Description:	free all the LGT in the library
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vEraseLGTLib()
{
  int i;

  if(gs_hLGTInFile == NULL) return;

  for(i=0; i<gs_lLGTIndex; i++)
  {
    if(gs_hLGTInFile[i] == NULL) 
		continue;
    free(gs_hLGTInFile[i]);
  }

  gs_lLGTIndex = 0;
  free(gs_hLGTInFile);
  gs_hLGTInFile = NULL;
}


/****************************************************************************
*		Description:	load LGT file in library
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vLoadLGTFileInLib()
{
  SCR_tdst_Cxt_Values	*p_stVal;
  xString				sDirectory;
  xString				szFileName;

  // save directory
  GetCurrentDirectory(256, sDirectory);

  //Inits 
  sprintf(szFileName, "%s.lgt", g_sFileIn);

  if(SCR_fn_c_RdL0_IsSectionExists(szFileName))
  {
	p_stVal = SCR_fnp_st_RdL0_AnalyseSection(szFileName, SCR_CDF_uw_Anl_ForceAnalyse);

	gs_hLGTInFile = (MLT_tdstLGT **)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,0);
	gs_lLGTIndex = (long)SCR_M_ul_RdL0_ExtractLongValue(p_stVal,1);
  }

  // restore directory
  SetCurrentDirectory(sDirectory); 
}

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




