reman3/Rayman_X/cpa/tempgrp/SCR/SCR_SvL0.c

471 lines
17 KiB
C

/*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* SCR_Save.c
* Functions for saving, level 0.
*
* Scripts, Beaudet Christophe
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/
/*
*=================================================================================================
* Includes.
*=================================================================================================
*/
#include <direct.h>
#include <io.h>
#include <stdarg.h>
#include "SCR.h"
/*
*=================================================================================================
* Global variables.
*=================================================================================================
*/
/*
* To save current indentation for save functions.
*/
unsigned int SCR_g_ui_SvL0_IndentationLevel = 0;
/*
*=================================================================================================
* Functions.
*=================================================================================================
*/
/*
*-------------------------------------------------------------------------------------------------
* To write a character to a file.
* _p_stFile : File to write character.
* _cChar : Character to write.
* Returns character read.
*-------------------------------------------------------------------------------------------------
*/
void fn_v_SvL0_fputc(SCR_tdst_File_Description *_p_stFile, char _cChar)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
int iRes;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_M_Dbg_Assert_D(_p_stFile != NULL);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if(_p_stFile->d_stFirstPage == NULL)
{
if(_cChar == '\n')
{
SCR_M_fputc('\r', _p_stFile, iRes);
if(iRes == EOF)
{
sprintf(g_st_Err_GlobalError.a_szBufferTmp1, "File is \"%s\".", _p_stFile->a_szFileName);
*g_st_Err_GlobalError.a_szBufferTmp2 = '\0';
SCR_M_Err_RaiseError(SCR_EI_Err_WriteFile);
}
}
SCR_M_fputc(_cChar, _p_stFile, iRes);
if(iRes == EOF)
{
sprintf(g_st_Err_GlobalError.a_szBufferTmp1, "File is \"%s\".", _p_stFile->a_szFileName);
*g_st_Err_GlobalError.a_szBufferTmp2 = '\0';
SCR_M_Err_RaiseError(SCR_EI_Err_WriteFile);
}
}
else
{
if(_cChar == '\n')
fn_v_Page_InsertChar(_p_stFile, '\r');
fn_v_Page_InsertChar(_p_stFile, _cChar);
}
}
/*
*-------------------------------------------------------------------------------------------------
* To write a string to a file.
* _p_stFile : File to write string.
* _p_szString : String to write.
* Returns character read.
*-------------------------------------------------------------------------------------------------
*/
void fn_v_SvL0_fputs(SCR_tdst_File_Description *_p_stFile, char *_p_szString)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_M_Dbg_Assert_D(_p_stFile != NULL);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
while(*_p_szString)
fn_v_SvL0_fputc(_p_stFile, *_p_szString++);
}
/*===============================================================================================*/
/*
*-------------------------------------------------------------------------------------------------
* To save a line in a script file with a list of string parameters.
* _p_stFile : File to save.
* _uiNumPars : Number of parameters.
* _ap_szParams : Array of parameters.
*-------------------------------------------------------------------------------------------------
*/
void fn_v_SvL0_SaveNormalFormat
(
SCR_tdst_File_Description *_p_stFile,
unsigned int _uiNumPars,
char *_ap_szParams[]
)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
char cAddDel;
unsigned int uiIndex;
char *p_szTemp;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_M_Dbg_Assert_D(_p_stFile != NULL);
SCR_M_Dbg_Assert_D(_uiNumPars != 0);
SCR_M_Dbg_Assert_D(_ap_szParams != NULL);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
cAddDel = 0;
for(uiIndex = 0; uiIndex < _uiNumPars; uiIndex++)
{
/*
* Add " if there is a space character in parameter ?
*/
if(_ap_szParams[uiIndex][0] != '\"')
{
p_szTemp = _ap_szParams[uiIndex];
if(*p_szTemp != SCR_CC_c_Cfg_VarMark)
{
while(*p_szTemp)
{
if(!gs_a256_cIsCharValidForWord[*p_szTemp])
{
cAddDel = 1;
break;
}
p_szTemp++;
}
}
}
/*
* Parameter.
*/
if(cAddDel)
fn_v_SvL0_fputc(_p_stFile, '"');
fn_v_SvL0_fputs(_p_stFile, _ap_szParams[uiIndex]);
if(cAddDel)
fn_v_SvL0_fputc(_p_stFile, '"');
cAddDel = 0;
/* Write parameter separator */
if(uiIndex < (_uiNumPars - 1))
fn_v_SvL0_fputc(_p_stFile, SCR_CC_c_Cfg_ParamSeparator);
}
}
/*
*-------------------------------------------------------------------------------------------------
* To save a line in a script file with an array of parameters.
* _p_stFile : File to save.
* _eFormat : Format of parameters.
* _ap_szParams : Array of parameters.
*-------------------------------------------------------------------------------------------------
*/
void fn_v_SvL0_SaveArrayFormat
(
SCR_tdst_File_Description *_p_stFile,
SCR_tde_SvL0_Format _eFormat,
char *_ap_szParams[]
)
{
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
unsigned long ulIndex, ulEnd;
char *p_szData, *p_szString;
char a_szBuffer[SCR_CV_ui_Cfg_MaxLenLine];
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_M_Dbg_Assert_D(_p_stFile != NULL);
SCR_M_Dbg_Assert_D(_ap_szParams != NULL);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Get number of values in array at index 0 */
ulEnd = (long) _ap_szParams[0];
/* Get first value at index 1 */
p_szData = _ap_szParams[1];
/* Save all entries of array */
for(ulIndex = 0; ulIndex < ulEnd; ulIndex++)
{
switch(_eFormat)
{
case SCR_EF_SvL0_ArrayByte:
sprintf(a_szBuffer, "%d", p_szData[ulIndex]);
break;
case SCR_EF_SvL0_ArrayShort:
sprintf(a_szBuffer, "%d", ((short *) p_szData)[ulIndex]);
break;
case SCR_EF_SvL0_ArrayLong:
sprintf(a_szBuffer, "%ld", ((long *) p_szData)[ulIndex]);
break;
case SCR_EF_SvL0_ArrayFloat:
sprintf(a_szBuffer, "%f", ((float *) p_szData)[ulIndex]);
break;
case SCR_EF_SvL0_ArrayDouble:
sprintf(a_szBuffer, "%lf", ((double *) p_szData)[ulIndex]);
break;
case SCR_EF_SvL0_ArrayInt:
sprintf(a_szBuffer, "%d", ((int *) p_szData)[ulIndex]);
break;
case SCR_EF_SvL0_ArrayBoolean:
if(p_szData[ulIndex] == 0)
p_szString = "False";
else
p_szString = "True";
sprintf(a_szBuffer, "%s", p_szString);
break;
case SCR_EF_SvL0_ArrayDisEna:
if(p_szData[ulIndex] == 0)
p_szString = "Disable";
else
p_szString = "Enable";
sprintf(a_szBuffer, "%s", p_szString);
break;
}
/* Write formatted string */
fn_v_SvL0_fputs(_p_stFile, a_szBuffer);
/* Write parameter separator */
if(ulIndex < (ulEnd - 1))
fn_v_SvL0_fputc(_p_stFile, SCR_CC_c_Cfg_ParamSeparator);
}
}
/*
*=================================================================================================
*
* PUBLIC
*
*=================================================================================================
*/
/*
*-------------------------------------------------------------------------------------------------
* To save the first word of a line.
* _p_stFile : File to save.
* _cFirstChar : The character to save before the line (can be '\0' to ignore).
* _p_szWord : Word to save (can be NULL to ignore).
* _cLastChar : Character to write after the word (can be '\0' to ignore).
*-------------------------------------------------------------------------------------------------
*/
void SCR_fn_v_SvL0_SaveWord
(
SCR_tdst_File_Description *_p_stFile,
char _cFirstChar,
char *_p_szWord,
char _cLastChar
)
SCR_BEGIN
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_M_Dbg_Assert_D(_p_stFile != NULL);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* _cFirstChar */
if(_cFirstChar != '\0')
fn_v_SvL0_fputc(_p_stFile, _cFirstChar);
/* Word */
if(_p_szWord)
fn_v_SvL0_fputs(_p_stFile, _p_szWord);
/* _cLastChar */
if(_cLastChar != '\0')
fn_v_SvL0_fputc(_p_stFile, _cLastChar);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_CEND(;)
/*
*-------------------------------------------------------------------------------------------------
* To save a format specifier.
* _p_stFile : File to save.
* _eFormat : Format of parameters.
*-------------------------------------------------------------------------------------------------
*/
void SCR_fn_v_SvL0_SaveFormat
(
SCR_tdst_File_Description *_p_stFile,
SCR_tde_SvL0_Format _eFormat
)
SCR_BEGIN
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_M_Dbg_Assert_D(_p_stFile != NULL);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if(_eFormat == SCR_EF_SvL0_Normal)
SCR_RETURN(;);
fn_v_SvL0_fputc(_p_stFile, SCR_CC_c_Cfg_FormatBegMark);
switch(_eFormat)
{
case SCR_EF_SvL0_ArrayByte:
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArray);
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArrayByte);
break;
case SCR_EF_SvL0_ArrayShort:
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArray);
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArrayShort);
break;
case SCR_EF_SvL0_ArrayLong:
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArray);
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArrayLong);
break;
case SCR_EF_SvL0_ArrayFloat:
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArray);
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArrayFloat);
break;
case SCR_EF_SvL0_ArrayDouble:
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArray);
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArrayDouble);
break;
case SCR_EF_SvL0_ArrayInt:
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArray);
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArrayInt);
break;
case SCR_EF_SvL0_ArrayBoolean:
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArray);
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArrayBoolean);
break;
case SCR_EF_SvL0_ArrayDisEna:
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArray);
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArrayDisEna);
break;
case SCR_EF_SvL0_ArrayReferences:
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArray);
fn_v_SvL0_fputc(_p_stFile, SCR_CF_c_Cfg_FormatArrayReferences);
break;
}
fn_v_SvL0_fputc(_p_stFile, SCR_CC_c_Cfg_FormatEndMark);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_CEND(;)
/*
*-------------------------------------------------------------------------------------------------
* To save parameters with an array of parameters.
* _p_stFile : File to save.
* _eFormat : Format of parameters.
* _uiNumParams : Number of parameters in array.
* _ap_szParams : Array of parameters.
*-------------------------------------------------------------------------------------------------
*/
void SCR_fn_v_SvL0_SaveParameters_AP
(
SCR_tdst_File_Description *_p_stFile,
SCR_tde_SvL0_Format _eFormat,
unsigned int _uiNumParams,
char *_ap_szParams[]
)
SCR_BEGIN
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_M_Dbg_Assert_D(_p_stFile != NULL);
SCR_M_Dbg_Assert_D(_uiNumParams != 0);
SCR_M_Dbg_Assert_D(_ap_szParams != NULL);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
fn_v_SvL0_fputc(_p_stFile, SCR_CC_c_Cfg_ParamBegMark);
if(_eFormat == SCR_EF_SvL0_Normal)
fn_v_SvL0_SaveNormalFormat(_p_stFile, _uiNumParams, _ap_szParams);
else
fn_v_SvL0_SaveArrayFormat(_p_stFile, _eFormat, _ap_szParams);
fn_v_SvL0_fputc(_p_stFile, SCR_CC_c_Cfg_ParamEndMark);
fn_v_SvL0_fputc(_p_stFile, '\n');
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_CEND(;)
/*
*-------------------------------------------------------------------------------------------------
* To save a line in a script file with multi parameters on line.
* _p_stFile : File to save.
* _eFormat : Format of parameters.
* _uiNumPars : Number of optionnal parameters.
* ... : All parameters.
*-------------------------------------------------------------------------------------------------
*/
void SCR_fn_v_SvL0_SaveParameters_MP
(
SCR_tdst_File_Description *_p_stFile,
SCR_tde_SvL0_Format _eFormat,
unsigned int _uiNumPars,
...
)
SCR_BEGIN
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
va_list xArgList;
char a_szBuffer[SCR_CV_ui_Cfg_MaxLenLine];
char *ap_szParams[SCR_CV_ui_Cfg_MaxNumPars];
char *p_szFormat, *p_szSearch;
unsigned int uiIndex;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_M_Dbg_Assert_D(_p_stFile != NULL);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
va_start(xArgList, _uiNumPars);
/*
* Format = scanf ?
* First parameter must be string format.
* Convert all other parameters.
*/
uiIndex = 0;
if(_eFormat == SCR_EF_SvL0_Scanf)
{
p_szFormat = va_arg(xArgList, char *);
SCR_M_Dbg_Verify_D(vsprintf(a_szBuffer, p_szFormat, xArgList) >= 0);
/*
* Convert a_szBuffer to an array of pointers to parameters.
* First parameter is scanf format.
*/
p_szFormat = a_szBuffer;
_uiNumPars = 1;
do
{
ap_szParams[uiIndex++] = p_szFormat;
p_szSearch = strchr(p_szFormat, SCR_CF_c_Cfg_ScanfSeparator);
if(p_szSearch)
{
*p_szSearch = '\0';
p_szFormat = p_szSearch + 1;
_uiNumPars++;
}
} while(p_szSearch);
ap_szParams[uiIndex] = NULL;
_eFormat = SCR_EF_SvL0_Normal;
}
/*
* Else we construct an array of parameters with list.
*/
else
{
for(uiIndex = 0; uiIndex < _uiNumPars; uiIndex++)
ap_szParams[uiIndex] = va_arg(xArgList, char *);
}
/*
* Save parameters.
*/
SCR_fn_v_SvL0_SaveParameters_AP
(
_p_stFile,
_eFormat,
_uiNumPars,
ap_szParams
);
va_end(xArgList);
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
SCR_CEND(;)