/*=========================================================================
* mnuload.c : Script interpretation for the menus
*
* Version              1.0
* Creation date        2./05/97
* Revision date
*
* That file needs to be compatible for all platforms.
*
* (c) Ubi R&D 1997
*=======================================================================*/
/*
*=======================================================================
Modifications:
*=======================================================================*/
/*#include "fonhdls.h"*/
 
#include "ACP_Base.h"
#include "ToolsCPA.h"
 
/*#include "SCR.h"*/
/*#include "DPT.h"*/
/*#include "GEO.h"*/
/*#include "GLI.h"*/
/*#include "TEX.h"*/
/*#include "SCR.h"*/
/*#include "VIG.h"*/
#include "RND.h"
/*#include "LST.h"*/
 
#include "fonerm.h"
#include "fonmem.h"
#include "Structur/EngMode.h"
 
/*#include "fonpub.h"*/
/*#include "FON_Eff.h"*/
#include "fonfont.h"
#include "fonload.h"
/*#include "fontext.h"*/

static BOOL s_bIncoherenceInTxtFiles = FALSE;
 
/* Global array of fonts*/
#if !defined(U64)
/*SCR_tdst_Link_Table FON_g_stFontLinkTable; // For binarisation purpose only*/
 
SCR_tdst_Link_Table *FON_fn_p_stGetLinkTable()
{
  return &FON_g_stTextLinkTable;
}
#endif /* U64 */
 
/*************************************************************************/
void FON_fn_vInitLoad()
{
#if !defined(U64)
	SCR_fn_v_RdL0_RegisterCallback(FON_C_SectionAddNewStrings,FON_fn_eLoadTextString,SCR_CRC_c_RdL0_ForSection);
 
 /*SCR_fn_v_Link_InitTable(&FON_g_stFontLinkTable); // for binarisation purposes only*/
#endif /* U64 */
}
/*************************************************************************/
 
#if !defined(U64)


#if 0

/* convert lowercase to uppercase, and accented characters to their unaccented version... */
void fn_vConvertStringChars ( char *_p_szString )
{
	int j = strlen ( _p_szString ) , i ;

	for ( i = 0 ; i < j ; i ++ )
	{
		#if 1
		if ( _p_szString [ i ] >= 'a' && _p_szString [ i ] <= 'z' )
			_p_szString [ i ] = toupper ( _p_szString [ i ] ) ;
		#else
		switch  ( _p_szString [ i ] )
		{
		case 'ç':
			_p_szString [ i ] = 'c' ;
			break ;

		case 'é':
		case 'è':
		case 'ë':
		case 'ê':
			_p_szString [ i ] = 'e' ;
			break ;

		case 'à':
		case 'â':
		case 'ä':
			_p_szString [ i ] = 'a' ;
			break ;

		case 'ö':
		case 'ò':
		case 'ô':
			_p_szString [ i ] = 'o' ;
			break ;

		case 'ï':
		case 'î':
			_p_szString [ i ] = 'i' ;
			break ;

		case 'ù' :
		case 'ü' :
		case 'ú' :
		case 'û' :
			_p_szString [ i ] = 'u' ;
			break ;
		}
		#endif
	}
}

#endif /* 0 */


 
/*************************************************************************/
#ifdef M_CheckScriptParamNumber
    #undef M_CheckScriptParamNumber
#endif
 
#define M_CheckScriptParamNumber(ulNumber) \
{ \
 if (SCR_fn_uc_RdL0_GetNumberOfParameters(szParams)!=ulNumber) \
  M_FONFatalError(E_uwFontScriptBadNumberOfArg); \
}
 
SCR_tde_Anl_ReturnValue FON_fn_eLoadTextString(SCR_tdst_File_Description *p_fFile, char *szAction, char *szParams[], SCR_tde_Anl_Action cType)
{
	/*enum FON_tdeTextEffect_ _eTextEffect = FON_e_Nothing;*/
	/*static tdeLanguageDefinition wLanguageId;*/
	static short s_wLanguageId ;
	static tdstLanguage * s_p_stLoadedLanguage ;

	switch (cType)
	{
	case SCR_EA_Anl_BeginSection:
		{
			/*allocate the pointer of lists of languages*/
			register unsigned short uwLanguage = 0;

			if ( !stricmp(szAction, "Common") )
			{
				/* special value for common language strings */
				s_wLanguageId = -1 ;
				if ( ! FON_g_stGeneral . p_stCommonLanguage )
				{
					FON_g_stGeneral.p_stCommonLanguage = (tdstLanguage *) FON_fn_p_vAllocInBloc (
						E_ucFONTextMemoryBlock ,
						sizeof ( tdstLanguage )
					);
				}
				/* the subsequent loaded strings are to be associated with this language definition */
				s_p_stLoadedLanguage = FON_g_stGeneral . p_stCommonLanguage ;
			}
			else
			{
				/* by default if any problem */
				s_wLanguageId = 0;
				do
				{
					if ( !stricmp(szAction, g_stEngineStructure.p_stLanguageTable[uwLanguage].szLanguageCode) )
					{
						s_wLanguageId = uwLanguage;
						uwLanguage  = g_stEngineStructure.ucNbLanguages;
					}
				} while ( uwLanguage ++ < g_stEngineStructure . ucNbLanguages ) ;

				/*allocate a new language definition structure if necessary*/
				if ( FON_g_stGeneral.uwNumberOfLanguages <= s_wLanguageId )
				{
					long lIndex;

					if ( FON_g_stGeneral.d_sLanguageArray )
					{
						FON_g_stGeneral.d_sLanguageArray = (tdstLanguage *) FON_fn_p_vReallocInBloc(
							E_ucFONTextMemoryBlock,
							FON_g_stGeneral.d_sLanguageArray,
							(s_wLanguageId + 1) * sizeof(tdstLanguage)
						);
					}
					else
					{
						FON_g_stGeneral.d_sLanguageArray = (tdstLanguage *) FON_fn_p_vAllocInBloc(
							E_ucFONTextMemoryBlock,
							(s_wLanguageId + 1) * sizeof(tdstLanguage)
						);
					}

					/*initialize all new allocated structures*/
					for ( lIndex = FON_g_stGeneral.uwNumberOfLanguages; lIndex < s_wLanguageId + 1; lIndex ++ )
					{
						FON_g_stGeneral.d_sLanguageArray[lIndex].d_pTextsArray = NULL;
						FON_g_stGeneral.d_sLanguageArray[lIndex].uwMaxNumberOfTexts = 0;
						FON_g_stGeneral.d_sLanguageArray[lIndex].uwNumberOfTexts = 0;
					}
					FON_g_stGeneral.uwNumberOfLanguages = (unsigned short) (s_wLanguageId + 1);
				}
				/* the subsequent loaded strings are to be associated with this language definition */
				s_p_stLoadedLanguage = FON_g_stGeneral . d_sLanguageArray + s_wLanguageId ;
			}

			/*update the size of the string pointers array*/
			s_p_stLoadedLanguage -> uwMaxNumberOfTexts += (unsigned short) (atoi(szParams[0]));
			if ( s_p_stLoadedLanguage -> d_pTextsArray )
			{
				s_p_stLoadedLanguage -> d_pTextsArray = (char/*struct FON_stText_*/ **) FON_fn_p_vReallocInBloc (
					E_ucFONTextMemoryBlock,
					s_p_stLoadedLanguage -> d_pTextsArray,
					s_p_stLoadedLanguage -> uwMaxNumberOfTexts * sizeof(char/*struct FON_stText_*/ *)
				);
			}
			else
			{
				s_p_stLoadedLanguage -> d_pTextsArray = (char/*struct FON_stText_*/ **) FON_fn_p_vAllocInBloc (
					E_ucFONTextMemoryBlock,
					s_p_stLoadedLanguage -> uwMaxNumberOfTexts * sizeof(char/*struct FON_stText_*/ *)
				);
			}
		}
 
      /*FON_g_stGeneral.d_stFontAndEffectArray = (struct FON_tdstFontAndEffect_ **)FON_fn_p_vReallocInBloc(E_ucFONMemoryBlock,FON_g_stGeneral.d_stFontAndEffectArray,FON_g_stGeneral.ulMaxNumberOfTexts*sizeof(struct FON_tdstFontAndEffect_ *));*/
      break;
 
  case SCR_EA_Anl_Entry :
    if (  ! stricmp( szAction , FON_C_SectionNewString ) || !stricmp( szAction , FON_C_SectionNewUpperString ) )
    {
      char *hText = NULL;
      if (s_p_stLoadedLanguage -> uwNumberOfTexts >= s_p_stLoadedLanguage -> uwMaxNumberOfTexts)
      {
        M_FONFatalError(E_uwFONNotEnoughStringSpace);
      }
 
      /* if the text is empty, try to find a non-empty version in any other language (the string is common to all)*/
      if ( strlen(szParams[1]) == 0 )
      {
        long lIndex;
        for (lIndex  = 0; lIndex < FON_g_stGeneral.uwNumberOfLanguages; lIndex ++)
        if ( FON_g_stGeneral.d_sLanguageArray[lIndex].d_pTextsArray )
        {
          /* get the text in the other language*/
          hText = FON_g_stGeneral.d_sLanguageArray[lIndex].d_pTextsArray[s_p_stLoadedLanguage -> uwNumberOfTexts];
          if ( hText )
            break;
        }
      }
      else
      {
		SCR_tdst_Link_Value * p_stRegisteredValue ;
		char				szRegisteredString [ 256 ] ;
		long	lSize = strlen ( szParams [ 1 ] ) ;

        hText = FON_fn_p_vAllocInBloc(
			E_ucFONTextMemoryBlock ,
			lSize + 1
#ifdef ACTIVE_EDITOR
			+ 2 /* 2 bytes for the string length */
#endif
		) ;
#ifdef ACTIVE_EDITOR
		/* for debug purposes (only for certain AI calls), store the size of the string after it */
		* ( short * ) ( & hText [ lSize + 1 ] ) = ( short ) lSize ;
#endif
        strcpy ( hText , szParams [ 1 ] ) ;
		/* convert strange characters to more normal characters */
		#if 0
		fn_vConvertStringChars ( hText ) ;
		#endif
		/* upper ? */
        if ( ! stricmp ( szAction , FON_C_SectionNewUpperString ) )
          strupr ( hText ) ;
 

		/* First we check if the string is already registered with a different index */
		if (!s_bIncoherenceInTxtFiles)
		{
			p_stRegisteredValue = SCR_fnp_st_Link_SearchKey(&FON_g_stTextLinkTable, szParams[0]);
			if ( p_stRegisteredValue != NULL )
			{
				long lRegisteredIndex = SCR_M_ul_Link_GetValue(p_stRegisteredValue);

				/* common strings have special indexes to identify them */
				if ( s_p_stLoadedLanguage == FON_g_stGeneral . p_stCommonLanguage )
					lRegisteredIndex -= FON_C_HandleOffsetForCommonLanguage ;

				if ( lRegisteredIndex != s_p_stLoadedLanguage -> uwNumberOfTexts )
				{
					char szMessage[256];
					
					sprintf(szMessage, "There is an incoherence in the text file %s, at the string %s", p_fFile->a_szFileName, szParams[0]);
					MessageBox(NULL,szMessage,"Incoherence in a TXT file",MB_OK|MB_ICONWARNING);
					s_bIncoherenceInTxtFiles = TRUE;
					#if defined ( ACTIVE_EDITOR )
					fn_vForceEditor();
					#endif /* ! ACTIVE_EDITOR */
				}
			}
		}
		/* Then we check if the index is already registered with a different string */
		if ( ! s_bIncoherenceInTxtFiles )
		{
			long lRegisteredIndex = s_p_stLoadedLanguage -> uwNumberOfTexts ;
			/* common strings have special indexes to identify them */
			if ( s_p_stLoadedLanguage == FON_g_stGeneral . p_stCommonLanguage )
				lRegisteredIndex += FON_C_HandleOffsetForCommonLanguage ;

			p_stRegisteredValue = SCR_fnp_st_Link_SearchValue(&FON_g_stTextLinkTable, lRegisteredIndex ) ;
			if ( p_stRegisteredValue != NULL )
			{
				strcpy(szRegisteredString, SCR_M_p_sz_Link_GetKey(p_stRegisteredValue));
				if (stricmp(szRegisteredString, szParams[0]))
				{
					char szMessage[256];
					
					sprintf(szMessage, "There is an incoherence in the text file %s, at the string %s", p_fFile->a_szFileName, szParams[0]);
					MessageBox(NULL,szMessage,"Incoherence in a TXT file",MB_OK|MB_ICONWARNING);
					s_bIncoherenceInTxtFiles = TRUE;
					#if defined ( ACTIVE_EDITOR )
					fn_vForceEditor();
					#endif /* ! ACTIVE_EDITOR */
				}
			}
 		}
		/* normally this line should occur once per text per language -> everything will be fine if a given text name*/
        /* always has the same index in the section, because only one linktable entry may exist with a given name*/
		SCR_fnp_st_Link_SetValue(
			&FON_g_stTextLinkTable,
			szParams[0],
			s_p_stLoadedLanguage -> uwNumberOfTexts
			+ (
				( s_p_stLoadedLanguage == FON_g_stGeneral . p_stCommonLanguage )
				? FON_C_HandleOffsetForCommonLanguage
				: 0
			)
		) ;
      }
 
      s_p_stLoadedLanguage -> d_pTextsArray[ s_p_stLoadedLanguage -> uwNumberOfTexts ] = hText ;
      s_p_stLoadedLanguage -> uwNumberOfTexts ++;
    }
    else if ( !stricmp(szAction, FON_C_SectionNewStringLength) )
    {
      char *hText = NULL;
      if ( s_p_stLoadedLanguage -> uwNumberOfTexts >= s_p_stLoadedLanguage -> uwMaxNumberOfTexts )
      {
        M_FONFatalError(E_uwFONNotEnoughStringSpace);
      }
 
      /* if the text is empty, try to find a non-empty version in any other language (the string is common to all)*/
      if ( atoi(szParams[1]) == 0 )
      {
        long lIndex;
        for (lIndex  = 0; lIndex < FON_g_stGeneral.uwNumberOfLanguages; lIndex ++)
        if ( FON_g_stGeneral.d_sLanguageArray[lIndex].d_pTextsArray )
        {
          /* get the text in the other language*/
          hText = FON_g_stGeneral.d_sLanguageArray[lIndex].d_pTextsArray[ s_p_stLoadedLanguage -> uwNumberOfTexts ] ;
          if ( hText )
            break;
        }
      }
      else
      {
		SCR_tdst_Link_Value *p_stRegisteredValue;
		char				szRegisteredString[256];
		long				lRegisteredIndex;

		long	lSize;
		lSize = atoi(szParams[1]);
        /* allocate a string and fill it with whitespaces (so that strlen() is non-zero)*/
        hText = FON_fn_p_vAllocInBloc(
			E_ucFONTextMemoryBlock,
			lSize + 1 /* string + zero terminator */
#ifdef ACTIVE_EDITOR
			+ 2 /* 2 bytes for the string length */
#endif
		);
		/* fill with whitespaces for clarity */
        memset(hText, ' ', lSize  );
		/* don't forget to set the zero terminator */
		hText [ lSize ] = 0 ;

#ifdef ACTIVE_EDITOR
		/* for debug purposes (only for certain AI calls), store the size of the string after it */
		* ( short * ) ( & hText [ lSize + 1 ] ) = ( short ) lSize ;
#endif

		/* First we check if the string is already registered with a different index */
		if (!s_bIncoherenceInTxtFiles)
		{
			p_stRegisteredValue = SCR_fnp_st_Link_SearchKey(&FON_g_stTextLinkTable, szParams[0]);
			if (p_stRegisteredValue!=NULL)
			{
				lRegisteredIndex = SCR_M_ul_Link_GetValue(p_stRegisteredValue);

				/* common strings have special indexes to identify them */
				if ( s_p_stLoadedLanguage == FON_g_stGeneral . p_stCommonLanguage )
					lRegisteredIndex -= FON_C_HandleOffsetForCommonLanguage ;

				if ( lRegisteredIndex != s_p_stLoadedLanguage -> uwNumberOfTexts )
				{
					char szMessage[256];
					
					sprintf(szMessage, "There is an incoherence in the text file %s, at the string %s", p_fFile->a_szFileName, szParams[0]);
					MessageBox(NULL,szMessage,"Incoherence in a TXT file",MB_OK|MB_ICONWARNING);
					s_bIncoherenceInTxtFiles = TRUE;
					#if defined ( ACTIVE_EDITOR )
					fn_vForceEditor();
					#endif /* ! ACTIVE_EDITOR */
				}
			}
		}
		/* Then we check if the index is already registered with a different string */
		if (!s_bIncoherenceInTxtFiles)
		{
			long lRegisteredIndex = s_p_stLoadedLanguage -> uwNumberOfTexts ;
			/* common strings have special indexes to identify them */
			if ( s_p_stLoadedLanguage == FON_g_stGeneral . p_stCommonLanguage )
				lRegisteredIndex += FON_C_HandleOffsetForCommonLanguage ;

			p_stRegisteredValue = SCR_fnp_st_Link_SearchValue(&FON_g_stTextLinkTable, lRegisteredIndex ) ;
			if ( p_stRegisteredValue != NULL )
			{
				strcpy(szRegisteredString, SCR_M_p_sz_Link_GetKey(p_stRegisteredValue));
				if (stricmp(szRegisteredString, szParams[0]))
				{
					char szMessage[256];
					
					sprintf(szMessage, "There is an incoherence in the text file %s, at the string %s", p_fFile->a_szFileName, szParams[0]);
					MessageBox(NULL,szMessage,"Incoherence in a TXT file",MB_OK|MB_ICONWARNING);
					s_bIncoherenceInTxtFiles = TRUE;
					#if defined ( ACTIVE_EDITOR )
					fn_vForceEditor();
					#endif /* ! ACTIVE_EDITOR */
				}
			}
		}
        /* normally this line should occur once per text per language -> everything will be fine if a given text name*/
        /* always has the same index in the section, because only one linktable entry may exist with a given name*/
        SCR_fnp_st_Link_SetValue(
			&FON_g_stTextLinkTable,
			szParams[0],
			s_p_stLoadedLanguage -> uwNumberOfTexts
			+ (
				( s_p_stLoadedLanguage == FON_g_stGeneral . p_stCommonLanguage )
				? FON_C_HandleOffsetForCommonLanguage
				: 0
			)
		);
      }
 
      s_p_stLoadedLanguage -> d_pTextsArray[ s_p_stLoadedLanguage -> uwNumberOfTexts ] = hText ;
      s_p_stLoadedLanguage -> uwNumberOfTexts ++ ;
    }
    break;
 
  case SCR_EA_Anl_EndSection:
    break;
  }
 
  return SCR_ERV_Anl_NormalReturn;
}
#endif /* U64 */
 
 
/*************************************************************************/
/* Binarisation functions*/
/* See with Alain Robin for any problem or question*/
 
 
/*char* FON_fn_p_cAlloc(unsigned long _ulSize)
{
  return (char*)FON_fn_p_vAllocInBloc(E_ucFONMemoryBlock,_ulSize);
}*/
