#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"*/
#include "ldt.h"
 
 
 
/*****************************************************************
Function name   : fn_iCreateTextString
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iCreateTextString( LDT_tdst_Link *pLink )
{
    char *szAction;
    short wLanguageId;
    /*allocate the pointer of lists of languages*/
    register unsigned short uwLanguage = 0;
 
    szAction = LDT_szGetSectionName();
 
 
    /* by default if any problem*/
    wLanguageId = 0;
    do
    {
      if ( !stricmp(szAction, g_stEngineStructure.p_stLanguageTable[uwLanguage].szLanguageCode) )
      {
          wLanguageId = uwLanguage;
          uwLanguage  = g_stEngineStructure.ucNbLanguages;
      }
    } while( uwLanguage++<g_stEngineStructure.ucNbLanguages );
 
    LDT_SetLinkValue( pLink, (unsigned long)wLanguageId );
 
 
      /*allocate a new language definition structure if necessary*/
      if ( FON_g_stGeneral.uwNumberOfLanguages <= 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,
                  (wLanguageId + 1) * sizeof(tdstLanguage)
                  );
          }
          else
          {
              FON_g_stGeneral.d_sLanguageArray = (tdstLanguage *) FON_fn_p_vAllocInBloc(
                  E_ucFONTextMemoryBlock,
                  (wLanguageId + 1) * sizeof(tdstLanguage)
                  );
          }
 
          /*initialize all new allocated structures*/
          for ( lIndex = FON_g_stGeneral.uwNumberOfLanguages; lIndex < 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) (wLanguageId + 1);
      }
 
      /*update the size of the string pointers array*/
      FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwMaxNumberOfTexts += (unsigned short) (atoi(LDT_szGetParam(1)));
      if ( FON_g_stGeneral.d_sLanguageArray[wLanguageId].d_pTextsArray )    {
          FON_g_stGeneral.d_sLanguageArray[wLanguageId].d_pTextsArray = (char/*struct FON_stText_*/ **) FON_fn_p_vReallocInBloc(
              E_ucFONTextMemoryBlock,
              FON_g_stGeneral.d_sLanguageArray[wLanguageId].d_pTextsArray,
              FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwMaxNumberOfTexts * sizeof(char/*struct FON_stText_*/ *)
              );
      }
      else
      {
          FON_g_stGeneral.d_sLanguageArray[wLanguageId].d_pTextsArray = (char/*struct FON_stText_*/ **) FON_fn_p_vAllocInBloc(
              E_ucFONTextMemoryBlock,
              FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwMaxNumberOfTexts * sizeof(char/*struct FON_stText_*/ *)
              );
      }
 
 
 
    return 0;
}
 
 
/*****************************************************************
Function name   : fn_iLoadTextString
Description     : 
Author          : Ovidiu Scripa (oscripa@ubisoft.ro) - ROMTEAM
Creation Date   : 13-Oct-98
Modified        : 
Return type     : int 
Argument        : LDT_tdst_Link *pLink
*****************************************************************/
int fn_iLoadTextString( LDT_tdst_Link *pLink )
{
  LDT_tdeParseResult result=ParseResult_BeginSection;
  short wLanguageId = (short)LDT_GetLinkValue(pLink);
 
  while( result!=ParseResult_EndSection ) 
  {
  	result=LDT_GetNextEntry();
  	switch( result )
  	{
  	  case ParseResult_Entry: /* an entry */
  	  {
  	    char *szEntry=LDT_szGetEntryName();
        if (  !stricmp(szEntry, FON_C_SectionNewString) || !stricmp(szEntry, FON_C_SectionNewUpperString) )
        {
          char *hText;
          if (FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts >= FON_g_stGeneral.d_sLanguageArray[wLanguageId].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(LDT_szGetParam(2)) == 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[FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts];
              if ( hText )
                break;
            }
          }
          else
          {
 
            hText = FON_fn_p_vAllocInBloc(E_ucFONTextMemoryBlock,strlen(LDT_szGetParam(2))+1);
            strcpy(hText, LDT_szGetParam(2));
            if ( !stricmp(szEntry,FON_C_SectionNewUpperString) )
              strupr(hText);
 
            /* 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, LDT_szGetParam(1), FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts);
          }
 
          FON_g_stGeneral.d_sLanguageArray[wLanguageId].d_pTextsArray[FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts] = hText;
          FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts ++;
        }
        else if (!stricmp(szEntry, FON_C_SectionNewStringLength))
        {
          char *hText;
          if (FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts >= FON_g_stGeneral.d_sLanguageArray[wLanguageId].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(LDT_szGetParam(2)) == 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[FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts];
              if ( hText )
                break;
            }
          }
          else
          {
		    long	lSize;
		    lSize = atoi(LDT_szGetParam(2));
 
            /* allocate a string and fill it with whitespaces (so that strlen() is non-zero)*/
            hText = FON_fn_p_vAllocInBloc(E_ucFONTextMemoryBlock,lSize + 1);
            memset(hText, 0, lSize + 1);
            memset(hText, ' ', lSize);
 
            *(short *) (hText + lSize) = (short) lSize;
 
            /* 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, LDT_szGetParam(1), FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts);
          }
 
          FON_g_stGeneral.d_sLanguageArray[wLanguageId].d_pTextsArray[FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts] = hText;
          FON_g_stGeneral.d_sLanguageArray[wLanguageId].uwNumberOfTexts ++;
        }
 
  	  }
  	}
  }
 
 
  return 0;
}
 
