/*=========================================================================
*
* SrfSave.c	- Saving functions for SRF file
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include <time.h>

#include "SrfSave.h"

#include "conventi.h"
#include "system.h"			// for matrix.h

#include "SpoSave.h"



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

/****************************************************************************
*		Description:	save header in SRF file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSrfHeader (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  xString sComment, sTime, sDate;

  SCR_fn_v_SvL1_DeleteSection(p_stFile);

  SCR_M_SvL0_SaveScriptFileHeader(p_stFile);
  sprintf(sComment, ".SRF (Surface Information) : %s", p_stFile->a_szFileName);
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  sprintf(sComment, "Generated by Max23Dos Moulinette");
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  _strtime(sTime);
  _strdate(sDate);
  sprintf(sComment, "Created date : %s %s", sDate, sTime);
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  sprintf(sComment, "Version directive, the version number is stored in file result 0");
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveDirective(p_stFile, M_SetCurrentFileDouble,SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%d,%.2f", 0, 1.0);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  sprintf(sComment, "Unit directive, the unit is stored in file result 1");
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  sprintf(sComment, "And correspond to the value of one unit exprimed in meter");
  SCR_M_SvL0_SaveComment(p_stFile, sComment);
  SCR_M_SvL0_SaveDirective(p_stFile, M_SetCurrentFileDouble,SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Scanf, 3, "%d,%.2f", 1, 1.0);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
  SCR_M_SvL0_SaveBlankLine(p_stFile);
}

/****************************************************************************
*		Description:	save surface in SRF file
*
*		Parameters:		p_stFile	: script file pointer
*						sFileName	: file name
*						p_vPtr		: data pointer
*						xAction		: script action
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSurface (SCR_tdst_File_Description *p_stFile, char *sFileName, void *p_vPtr, SCR_tde_Ntfy_Action xAction)
{
  xString			sFile, sSection, sIdent;
  MLT_tdstSurface	*p_stSrf;

  p_stSrf = (MLT_tdstSurface*)p_vPtr;

  if(xAction == SCR_EA_Ntfy_AddSection) 
  {
    SCR_fn_v_SvL1_ToEndSection(p_stFile);
    SCR_fn_v_SvL1_ToPrevLine(p_stFile);
  }
  else
    SCR_fn_v_SvL1_DeleteSection(p_stFile);

  MLT_vGetSurfaceName(sIdent, p_stSrf->sGeometric);
  SCR_fn_v_RdL0_ComputeSectionName(sSection, "*", M_SurfaceDescription, sIdent);
  
  SCR_M_SvL0_SaveBeginSection(p_stFile, MLT_p_cGetSectionName(sSection), SCR_CC_C_Cfg_EOL);

  sprintf(sFile, "%s%s.mod", g_sDirectorySave, g_sFileIn);
  SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_Geometric, p_stSrf->sGeometric);
  SCR_M_SvL0_SaveEntry(p_stFile, M_SRFActionVisual, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, sSection);

  MLT_vGetSurfaceName(sIdent, strstr(p_stSrf->stMatrix.sName, M_Surface));
  SCR_fn_v_RdL0_ComputeSectionName(p_stSrf->stMatrix.sName, "*", M_Matrix, sIdent);
  SCR_M_SvL0_SaveEntry(p_stFile, M_SRFActionVisualMatrix, SCR_CC_C_Cfg_NoChar);
  SCR_fn_v_SvL0_SaveParameters_MP(p_stFile, SCR_EF_SvL0_Normal, 1, p_stSrf->stMatrix.sName);

  SCR_M_SvL0_SaveEndSection(p_stFile, SCR_CC_C_Cfg_EOL);

  MLT_vSaveMatrix(p_stFile, &p_stSrf->stMatrix);
}

/****************************************************************************
*		Description:	save surface in SRF file
*
*		Parameters:		hRoot	: hierarchy root
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vSaveSrfFile (MLT_tdstSprObj *hRoot)
{
  MLT_tdstSector	*p_stSect;
  xString			sFile, sSection, sIdent;
  long				i,j;

  sprintf(sFile,"%s.srf", g_sFileIn);
  SCR_fn_v_SvL1_RegisterNotify(sFile, MLT_vSaveSrfHeader, NULL, SCR_EA_Ntfy_AddSection);
  
  // loop on the sector
  for(j=0; j<hRoot->lNbChild; j++)
  {
    p_stSect = &hRoot->d_hChild[j]->stSector;
    
    // loop on the surfaces
    for(i=0; i<p_stSect->lNbSurface; i++)
    {
      MLT_vGetSurfaceName(sIdent, p_stSect->a_stSurface[i].sGeometric);
      SCR_fn_v_RdL0_ComputeSectionName(sSection, sFile, M_SurfaceDescription, sIdent);
      SCR_fn_v_SvL1_RegisterNotify(sSection, MLT_vSaveSurface, &p_stSect->a_stSurface[i], SCR_EA_Ntfy_AddSection);
    }
  }
}

/****************************************************************************
*		Description:	get surface name
*
*		Parameters:		sName		: final name
*						sOldName	: source name
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void MLT_vGetSurfaceName(char *sName, char *sOldName)
{
  char *c;

  sprintf(sName, "%s", sOldName);
  c = sName;

  if ((c=strchr(c, '|')) != NULL)
  {
	  if (*c == '|') 
		  *c = 0;
  }
}



