/*=========================================================================
*
* IniData.cpp	- general functions for saving, loading
*
* Version			1.0
* Revision date
*
*=======================================================================*/
#include <io.h>
#include <sys/stat.h>
#include "stdafx.h"

#include "IniData.h"

#include "print.h"


//--- Global defines --------------------------------------------------------

tdstWindowsPosSize				g_stWinPosSize;

tdstInitialValue				g_stInitialData;		
tdstInitialDirValue				g_stInitialDirData;		
tdstInitialSelectValue			g_stInitialSelectData;	
tdstInitialOptionValue			g_stInitialOptionData;	
	
char							g_szIniFile[MAX_PATH];
char							g_szIniConfDirFile[MAX_PATH];
char							g_szIniConfSelectFile[MAX_PATH];
char							g_szIniConfOptionFile[MAX_PATH];

//--- Global Statics --------------------------------------------------------

tdstInitialValue	g_stDefaultData =		// default value for data
					{
						"d:\\GameData",			// default temp path for game data(computed)
						"d:\\RawData",			// default temp path for raw data(computed)
						"Textures",				// default temp path for Textures(computed)
						"Default",				// default temp path for Default(computed)
						"Rayman",				// default temp for Version(computed)
						"Update ModifLst.txt",	// default temp for ModifLst(computed)
						"",						// default temp path for game specific1(computed)
						"",						// default temp path for game specific2(computed)
						"",						// default temp path for raw specific1(computed)
						"",						// default temp path for raw specific2(computed)
						"",						// default temp path for family(computed)
						"RawSpecific1",			// default temp name for raw specific1(computed)
						"RawSpecific2",			// default temp name for raw specific2(computed)
						"GameSpecific1",		// default temp name for game specific1(computed)	
						"GameSpecific2",		// default temp name for game specific2(computed)	
						"",		
						"",		
						"none",					// default game data path (computed if none)
						"none",					// default raw data path (computed if none)
						"none",					// default Textures path (computed if none)
						"none",					// default Default path (computed if none)
						"none",					// default Version (computed if none)
						"none",					// default ModifLst (computed if none)
						"none",					// default game data specific1 (computed if none)
						"none",					// default game data specific2 (computed if none)
						"none",					// default raw data specific1 (computed if none)
						"none",					// default raw data specific1 (computed if none)
						"none",					// default path for family (computed if none)
						"none",					// default name for raw specific1 (computed if none)
						"none",					// default name for raw specific2 (computed if none)
						"none",					// default name for game specific1 (computed if none)
						"none",					// default name for game specific2 (computed if none)
						"none",				
						"none",				
					};


tdstInitialDirValue	g_stDefaultDirData =	
{
	"",		
	"",	
	"",		
	"",	
	"",		
	"",
	"",		
	"",	
	"",		
	"",	
	"",	
	"",	
};

tdstInitialOptionValue	g_stDefaultOptionData =	
{	
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
};

tdstInitialSelectValue	g_stDefaultSelectData =	
{	
	"",
	"",
	"",
	"",
	"",
	"",
};
//--- Macros -----------------------------------------------------------


// Macro to bound size of a rect
#define M_vVerifyRect( _p_stRect, _iMinWidth, _iMinHeight )\
{\
	if (_p_stRect->right - _p_stRect->left + 1 < _iMinWidth )\
		_p_stRect->right = _p_stRect->left + _iMinWidth ;\
	if (_p_stRect->bottom - _p_stRect->top + 1 < _iMinHeight )\
		_p_stRect->bottom = _p_stRect->top + _iMinHeight;\
}



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



/****************************************************************************
*		Description:	write the path for Game Data Directory in the .ini file
*
*		Parameters:		_szPath		: path to write
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetGameData( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "General directories", "PathGameData", _szPath, g_szIniFile );
}


/****************************************************************************
*		Description:	write the path for Game Specific1 Directory in the .ini file
*
*		Parameters:		_szPath		: path to write
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetGameSpecific1( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "General directories", "PathGameSpecific1", _szPath, g_szIniFile );
}


/****************************************************************************
*		Description:	write the path for Game Specific2 Directory in the .ini file
*
*		Parameters:		_szPath		: path to write
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetGameSpecific2( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "General directories", "PathGameSpecific2", _szPath, g_szIniFile );
}


/****************************************************************************
*		Description:	write the path for Raw Data Directory in the .ini file
*
*		Parameters:		_szPath		: path to write
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetRawData( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "General directories", "PathRawData", _szPath, g_szIniFile );
}


/****************************************************************************
*		Description:	write the path for Raw Specific1 Directory in the .ini file
*
*		Parameters:		_szPath		: path to write
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetRawSpecific1( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "General directories", "PathRawSpecific1", _szPath, g_szIniFile );
}


/****************************************************************************
*		Description:	write the path for Raw Specific2 Directory in the .ini file
*
*		Parameters:		_szPath		: path to write
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetRawSpecific2( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "General directories", "PathRawSpecific2", _szPath, g_szIniFile );
}



/****************************************************************************
*		Description:	Set the entry in the .ini file for the RawSpecific1
*
*		Parameters:		_szPath : name to write	
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetRawSp1( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "Name", "RawSpecific1", _szPath, g_szIniFile );
}


/****************************************************************************
*		Description:	Set the entry in the .ini file for the RawSpecific2
*
*		Parameters:		_szPath : name to write		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetRawSp2( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "Name", "RawSpecific2", _szPath, g_szIniFile );
}


/****************************************************************************
*		Description:	Set the entry in the .ini file for the GameSpecific1
*
*		Parameters:		_szPath : name to write		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetGameSp1( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "Name", "GameSpecific1", _szPath, g_szIniFile );
}


/****************************************************************************
*		Description:	Set the entry in the .ini file for the GameSpecific2
*
*		Parameters:		_szPath : name to write			
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetGameSp2( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "Name", "GameSpecific2", _szPath, g_szIniFile );
}





/****************************************************************************
*		Description:	Get all datas saved in the .ini file
*
*		Parameters:		_bFirst		: intialisation file
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vGetDatasFromFile( BOOL _bFirst )
{
	if (_bFirst)
	{
		GetCurrentDirectory( MAX_PATH, g_szIniFile );
		if (g_szIniFile[strlen(g_szIniFile) - 1] != '\\')
			strcat( g_szIniFile, "\\");
		strcat( g_szIniFile, C_szIniFile );
		// set all ini data with default data
		memcpy( &g_stInitialData, &g_stDefaultData, sizeof( tdstInitialValue ) );
	}

	// get the informations for name of the executable, local path, game data, rawdata, textures and default directories
	GetPrivateProfileString( "Name", "RawSpecific1", g_stDefaultData.szRawSp1, g_stInitialData.szRawSp1, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "Name", "RawSpecific2", g_stDefaultData.szRawSp2, g_stInitialData.szRawSp2, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "Name", "GameSpecific1", g_stDefaultData.szGameSp1, g_stInitialData.szGameSp1, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "Name", "GameSpecific2", g_stDefaultData.szGameSp2, g_stInitialData.szGameSp2, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "General directories", "PathRawData", g_stDefaultData.szRawData, g_stInitialData.szRawData, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "General directories", "PathRawSpecific1", g_stDefaultData.szRawSpecific1, g_stInitialData.szRawSpecific1, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "General directories", "PathRawSpecific2", g_stDefaultData.szRawSpecific2, g_stInitialData.szRawSpecific2, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "General directories", "PathGameData", g_stDefaultData.szGameData, g_stInitialData.szGameData, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "General directories", "PathGameSpecific1", g_stDefaultData.szGameSpecific1, g_stInitialData.szGameSpecific1, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "General directories", "PathGameSpecific2", g_stDefaultData.szGameSpecific2, g_stInitialData.szGameSpecific2, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "MainOptions", "Version", g_stDefaultData.szVersion, g_stInitialData.szVersion, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "SpecificFiles", "Family", g_stDefaultData.szFamily, g_stInitialData.szFamily, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "Configurations", "ConfigDirs", g_stDefaultData.szConfDirs, g_stInitialData.szConfDirs, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "Configurations", "ConfigSelects", g_stDefaultData.szConfSelects, g_stInitialData.szConfSelects, MAX_PATH, g_szIniFile );
	GetPrivateProfileString( "Configurations", "ConfigOptions", g_stDefaultData.szConfOptions, g_stInitialData.szConfOptions, MAX_PATH, g_szIniFile );

	// set executable name, local path, Game Data path, RawData path, Textures path, Default path
	HANDLE hHandle;
	WIN32_FIND_DATA stFindFileData;

	memset(&stFindFileData,0,sizeof(stFindFileData));
	hHandle = FindFirstFile(g_szIniFile, &stFindFileData);
	//return a boolean for telling if some files *.* exists in the directory or not
	if( hHandle == INVALID_HANDLE_VALUE)
	{   char _szMessage[50];
		sprintf(_szMessage, "The %s file does not exist, so the datas will be taken from the Default structure!", g_szIniFile);
		AfxMessageBox( _szMessage, MB_OK | MB_ICONSTOP );
	}
    FindClose(hHandle);

	if (strcmp( g_stInitialData.szGameData, g_stDefaultData.szGameData) == 0)
		strcpy( g_stInitialData.szGameData, g_stInitialData.szTempGameData);
	if (strcmp( g_stInitialData.szRawData, g_stDefaultData.szRawData) == 0)
		strcpy( g_stInitialData.szRawData, g_stInitialData.szTempRawData);
	if (strcmp( g_stInitialData.szTextures, g_stDefaultData.szTextures) == 0)
		strcpy( g_stInitialData.szTextures, g_stInitialData.szTempTextures);
	if (strcmp( g_stInitialData.szDefault, g_stDefaultData.szDefault) == 0)
		strcpy( g_stInitialData.szDefault, g_stInitialData.szTempDefault);
	if (strcmp( g_stInitialData.szVersion, g_stDefaultData.szVersion) == 0)
		strcpy( g_stInitialData.szVersion, g_stInitialData.szTempVersion);
	if (strcmp( g_stInitialData.szModifLst, g_stDefaultData.szModifLst) == 0)
		strcpy( g_stInitialData.szModifLst, g_stInitialData.szTempModifLst);

	if (strcmp( g_stInitialData.szGameSpecific1, g_stDefaultData.szGameSpecific1) == 0)
		strcpy( g_stInitialData.szGameSpecific1, g_stInitialData.szTempGameSpecific1);
	if (strcmp( g_stInitialData.szGameSpecific2, g_stDefaultData.szGameSpecific2) == 0)
		strcpy( g_stInitialData.szGameSpecific2, g_stInitialData.szTempGameSpecific2);

	if (strcmp( g_stInitialData.szRawSpecific1, g_stDefaultData.szRawSpecific1) == 0)
		strcpy( g_stInitialData.szRawSpecific1, g_stInitialData.szTempRawSpecific1);
	if (strcmp( g_stInitialData.szRawSpecific2, g_stDefaultData.szRawSpecific2) == 0)
		strcpy( g_stInitialData.szRawSpecific2, g_stInitialData.szTempRawSpecific2);

	if (strcmp( g_stInitialData.szFamily, g_stDefaultData.szFamily) == 0)
		strcpy( g_stInitialData.szFamily, g_stInitialData.szTempFamily);

	if (strcmp( g_stInitialData.szRawSp1, g_stDefaultData.szRawSp1) == 0)
		strcpy( g_stInitialData.szRawSp1, g_stInitialData.szTempRawSp1);
	if (strcmp( g_stInitialData.szRawSp2, g_stDefaultData.szRawSp2) == 0)
		strcpy( g_stInitialData.szRawSp2, g_stInitialData.szTempRawSp2);
	if (strcmp( g_stInitialData.szGameSp1, g_stDefaultData.szGameSp1) == 0)
		strcpy( g_stInitialData.szGameSp1, g_stInitialData.szTempGameSp1);
	if (strcmp( g_stInitialData.szGameSp2, g_stDefaultData.szGameSp2) == 0)
		strcpy( g_stInitialData.szGameSp2, g_stInitialData.szTempGameSp2);

	if (strcmp( g_stInitialData.szConfDirs, g_stDefaultData.szConfDirs) == 0)
		strcpy( g_stInitialData.szConfDirs, g_stInitialData.szTempConfDirs);

	if (strcmp( g_stInitialData.szConfSelects, g_stDefaultData.szConfSelects) == 0)
		strcpy( g_stInitialData.szConfSelects, g_stInitialData.szTempConfSelects);

	if (strcmp( g_stInitialData.szConfOptions, g_stDefaultData.szConfOptions) == 0)
		strcpy( g_stInitialData.szConfOptions, g_stInitialData.szTempConfOptions);

	g_bRaymanII = stricmp(g_stInitialData.szVersion, "Rayman");

	// Get the position and the size of the main window
	fn_IniData_vGetWindowPosSize();

	// Get the options
	char szOption[10];
	
	// main options
	GetPrivateProfileString( "MainOptions", "UpdateLst", "FALSE", szOption, 10, g_szIniFile );
	g_bModifLst = !stricmp(szOption, "TRUE");
	GetPrivateProfileString( "MainOptions", "IgnoreMdf", "FALSE", szOption, 10, g_szIniFile );
	g_bEraseMdf = !stricmp(szOption, "TRUE");

	// ignore options
	GetPrivateProfileString( "Options", "IgnoreZoo", "TRUE", szOption, 10, g_szIniFile );
	g_bIgnoreZoo = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Options", "IgnoreSct", "TRUE", szOption, 10, g_szIniFile );
	g_bIgnoreSct = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Options", "IgnoreSpo", "TRUE", szOption, 10, g_szIniFile );
	g_bIgnoreSpo = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Options", "IgnoreGmt", "TRUE", szOption, 10, g_szIniFile );
	g_bIgnoreGmt = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Options", "IgnoreTex", "TRUE", szOption, 10, g_szIniFile );
	g_bIgnoreTex = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Options", "IgnoreMod", "TRUE", szOption, 10, g_szIniFile );
	g_bEraseMdf = stricmp(szOption, "FALSE");

	// conversion options
	GetPrivateProfileString( "Conversion", "CopyAnims", "TRUE", szOption, 10, g_szIniFile );
	g_bCopyAnims = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Conversion", "CopyTextures", "TRUE", szOption, 10, g_szIniFile );
	g_bCopyTextures = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Conversion", "GenerateRLI", "TRUE", szOption, 10, g_szIniFile );
	g_bGenerateRLI = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Conversion", "UpdateGameDSC", "TRUE", szOption, 10, g_szIniFile );
	g_bUpdateGameDSC = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Conversion", "CreateBasicFamily", "FALSE", szOption, 10, g_szIniFile );
	g_bCreateBasicFamily = !stricmp(szOption, "TRUE");

	// erase options
	GetPrivateProfileString( "Erase", "EraseDuplicated", "FALSE", szOption, 10, g_szIniFile );
	g_bEraseDuplicated = !stricmp(szOption, "TRUE");
	GetPrivateProfileString( "Erase", "EraseUnused", "FALSE", szOption, 10, g_szIniFile );
	g_bEraseUnused = !stricmp(szOption, "TRUE");

	// family options
	GetPrivateProfileString( "Family", "CommonFamily", "TRUE", szOption, 10, g_szIniFile );
	g_bCommonFamily = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Family", "SpecificFamily", "TRUE", szOption, 10, g_szIniFile );
	g_bSpecificFamily = stricmp(szOption, "FALSE");
	GetPrivateProfileString( "Family", "AskFamily", "TRUE", szOption, 10, g_szIniFile );
	g_bAskFamily = stricmp(szOption, "FALSE");
}





/****************************************************************************
*		Description:	Get data from the Configs.ini file
*
*		Parameters:		_bFirst	: TRUE if first time
*						szTag	: name of the config
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vGetDatasFromConfigDirsFile( BOOL _bFirst, char *szTag )
{
	if (_bFirst)
	{
		GetCurrentDirectory( MAX_PATH, g_szIniConfDirFile );
		if (g_szIniConfDirFile[strlen(g_szIniConfDirFile) - 1] != '\\')
			strcat( g_szIniConfDirFile, "\\");
		strcat( g_szIniConfDirFile, C_szIniConfDirFile );
		// set all ini data with default data
		memcpy( &g_stInitialDirData, &g_stDefaultDirData, sizeof( tdstInitialDirValue ) );
	}

	GetPrivateProfileString( szTag, "Anims", g_stDefaultDirData.szAnims, g_stInitialDirData.szAnims, MAX_PATH, g_szIniConfDirFile );
	GetPrivateProfileString( szTag, "Banks", g_stDefaultDirData.szBanks, g_stInitialDirData.szBanks, MAX_PATH, g_szIniConfDirFile );
	GetPrivateProfileString( szTag, "Classes", g_stDefaultDirData.szClasses, g_stInitialDirData.szClasses, MAX_PATH, g_szIniConfDirFile );
	GetPrivateProfileString( szTag, "Families", g_stDefaultDirData.szFamilies, g_stInitialDirData.szFamilies, MAX_PATH, g_szIniConfDirFile );
	GetPrivateProfileString( szTag, "Levels", g_stDefaultDirData.szLevels, g_stInitialDirData.szLevels, MAX_PATH, g_szIniConfDirFile );
	GetPrivateProfileString( szTag, "Textures", g_stDefaultDirData.szTextures, g_stInitialDirData.szTextures, MAX_PATH, g_szIniConfDirFile );

	HANDLE hHandle;
	WIN32_FIND_DATA stFindFileData;

	memset(&stFindFileData,0,sizeof(stFindFileData));
	hHandle = FindFirstFile(g_szIniConfDirFile, &stFindFileData);
	//return a boolean for telling if some files *.* exists in the directory or not
	if( hHandle == INVALID_HANDLE_VALUE)
	{   char _szMessage[50];
		sprintf(_szMessage, "The %s file does not exist, so the datas will be taken from the Default structure!", g_szIniConfDirFile);
		AfxMessageBox( _szMessage, MB_OK | MB_ICONSTOP );
	}
    FindClose(hHandle);

	if (strcmp( g_stInitialDirData.szAnims, g_stDefaultDirData.szAnims) == 0)
		strcpy( g_stInitialDirData.szAnims, g_stInitialDirData.szTempAnims);
	if (strcmp( g_stInitialDirData.szBanks, g_stDefaultDirData.szBanks) == 0)
		strcpy( g_stInitialDirData.szBanks, g_stInitialDirData.szTempBanks);
	if (strcmp( g_stInitialDirData.szClasses, g_stDefaultDirData.szClasses) == 0)
		strcpy( g_stInitialDirData.szClasses, g_stInitialDirData.szTempClasses);
	if (strcmp( g_stInitialDirData.szFamilies, g_stDefaultDirData.szFamilies) == 0)
		strcpy( g_stInitialDirData.szFamilies, g_stInitialDirData.szTempFamilies);
	if (strcmp( g_stInitialDirData.szLevels, g_stDefaultDirData.szLevels) == 0)
		strcpy( g_stInitialDirData.szLevels, g_stInitialDirData.szTempLevels);
	if (strcmp( g_stInitialDirData.szTextures, g_stDefaultDirData.szTextures) == 0)
		strcpy( g_stInitialDirData.szTextures, g_stInitialDirData.szTempTextures);
}


/****************************************************************************
*		Description:	Set the Config entry in the GIData.ini file
*
*		Parameters:		_szPath		: names of the configs ("Name1,Name2,Name3,...")		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetConfigDirNames( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "Configurations", "ConfigDirs", _szPath, g_szIniFile );
}

/****************************************************************************
*		Description:	Set the dir entry in the Configs.ini file
*
*		Parameters:		szConf		: name of the config
*						szDirName	: name of the type
*						zsPath		: path to register
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetConfigDirPath( char* szConf, char *szDirName, char *_szPath )
{
	if (_access( g_szIniConfDirFile, 2) == -1)
		_chmod ( g_szIniConfDirFile, _S_IWRITE );
	WritePrivateProfileString( szConf, szDirName, _szPath, g_szIniConfDirFile );
}


/****************************************************************************
*		Description:	Get data from ConfigControls file
*
*		Parameters:		_bFirst	: TRUE if first time
*						szTag	: name of the config
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vGetDatasFromConfigSelectFile( BOOL _bFirst, char *szTag )
{
	if (_bFirst)
	{
		GetCurrentDirectory( MAX_PATH, g_szIniConfSelectFile );
		if (g_szIniConfSelectFile[strlen(g_szIniConfSelectFile) - 1] != '\\')
			strcat( g_szIniConfSelectFile, "\\");
		strcat( g_szIniConfSelectFile, C_szIniConfSelectFile );
		// set all ini data with default data
		memcpy( &g_stInitialSelectData, &g_stDefaultSelectData, sizeof( tdstInitialSelectValue ) );
	}
	GetPrivateProfileString( szTag, "Anims", g_stDefaultSelectData.szAnims, g_stInitialSelectData.szAnims, MAX_PATH, g_szIniConfSelectFile );
	GetPrivateProfileString( szTag, "Banks", g_stDefaultSelectData.szBanks, g_stInitialSelectData.szBanks, MAX_PATH, g_szIniConfSelectFile );
	GetPrivateProfileString( szTag, "Classes", g_stDefaultSelectData.szClasses, g_stInitialSelectData.szClasses, MAX_PATH, g_szIniConfSelectFile );
	GetPrivateProfileString( szTag, "Families", g_stDefaultSelectData.szFamilies, g_stInitialSelectData.szFamilies, MAX_PATH, g_szIniConfSelectFile );
	GetPrivateProfileString( szTag, "Levels", g_stDefaultSelectData.szLevels, g_stInitialSelectData.szLevels, MAX_PATH, g_szIniConfSelectFile );
	GetPrivateProfileString( szTag, "Textures", g_stDefaultSelectData.szTextures, g_stInitialSelectData.szTextures, MAX_PATH, g_szIniConfSelectFile );
}

/****************************************************************************
*		Description:	Set the ConfigControls entry in the GIData.ini file
*
*		Parameters:		szPath	:	names of the configs ("Name1,Name2,Name3,...")	
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetSelectConfigNames( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "Configurations", "ConfigSelects", _szPath, g_szIniFile );
}

/****************************************************************************
*		Description:	Set the type entry in the ConfigSelects.ini file
*
*		Parameters:		szName		:	section name
*						szType		:	name of the type
*						szPath		:	names to register ("Name1,Name2,Name3,...")
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetSelectConfigValues( char *szName, char *szType, char *szPath )
{
	if (_access( g_szIniConfOptionFile, 2) == -1)
		_chmod ( g_szIniConfOptionFile, _S_IWRITE );
	WritePrivateProfileString( szName, szType, szPath, g_szIniConfSelectFile );

}


/****************************************************************************
*		Description:	Get data from ConfigControls file
*
*		Parameters:		_bFirst	: TRUE if first time
*						szTag	: name of the config
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vGetDatasFromConfigOptionsFile( BOOL _bFirst, char *szTag )
{
	char szOption[10];

	if (_bFirst)
	{
		GetCurrentDirectory( MAX_PATH, g_szIniConfOptionFile );
		if (g_szIniConfOptionFile[strlen(g_szIniConfOptionFile) - 1] != '\\')
			strcat( g_szIniConfOptionFile, "\\");
		strcat( g_szIniConfOptionFile, C_szIniConfOptionFile );
		// set all ini data with default data
		memcpy( &g_stInitialOptionData, &g_stDefaultOptionData, sizeof( tdstInitialOptionValue ) );
	}
	GetPrivateProfileString( szTag, "IgnoreZoo", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bIgnoreZoo = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "IgnoreSct", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bIgnoreSct = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "IgnoreSpo", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bIgnoreSpo = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "IgnoreGmt", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bIgnoreGmt = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "IgnoreTex", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bIgnoreTex = stricmp(szOption, "FALSE");

	GetPrivateProfileString( szTag, "CopyAnims", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bCopyAnims = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "CopyTextures", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bCopyTextures = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "GenerateRLI", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bGenerateRLI = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "UpdateGameDSC", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bUpdateGameDSC = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "CreateBasicFamily", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bCreateBasicFamily = stricmp(szOption, "FALSE");

	GetPrivateProfileString( szTag, "EraseDuplicated", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bEraseDuplicated = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "EraseUnused", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bEraseUnused = stricmp(szOption, "FALSE");

	GetPrivateProfileString( szTag, "CommonFamily", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bCommonFamily = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "SpecificFamily", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bSpecificFamily = stricmp(szOption, "FALSE");
	GetPrivateProfileString( szTag, "AskFamily", "TRUE", szOption, 10, g_szIniConfOptionFile );
	g_bAskFamily = stricmp(szOption, "FALSE");
}

/****************************************************************************
*		Description:	Set the ConfigControls entry in the GIData.ini file
*
*		Parameters:		szPath	:	names of the configs ("Name1,Name2,Name3,...")	
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetOptionConfigNames( char *_szPath )
{
	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );
	WritePrivateProfileString( "Configurations", "ConfigOptions", _szPath, g_szIniFile );
}

/****************************************************************************
*		Description:	Set the entry in the ConfigControls.ini file
*
*		Parameters:		bSetIgnore	:	state of the control
*						szName		:	name of the control
*						szNew		:	section name
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vSetOptionConfigValues( BOOL bSetIgnore, char *szName, char *szNew )
{
	if (_access( g_szIniConfOptionFile, 2) == -1)
		_chmod ( g_szIniConfOptionFile, _S_IWRITE );
	WritePrivateProfileString( szNew, szName, bSetIgnore ? "TRUE" : "FALSE", g_szIniConfOptionFile );
}



/****************************************************************************
*		Description:	Get the position and the size of the main window from the .ini file
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vGetWindowPosSize( void )
{
	char szString[100];

	GetPrivateProfileString( "WinPref", "Main", "", szString, 100, g_szIniFile );
	fn_IniData_vStudyRect( szString, &g_stWinPosSize.stMainPos );
	M_vVerifyRect( (&g_stWinPosSize.stMainPos), C_iMainWindowWidth, C_iMainWindowHeight );
}


/****************************************************************************
*		Description:	Save the new position and size of the main window
*
*		Parameters:		
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vWriteWindowPosSize( void )
{
	char szValue[50];
	RECT *p_stRect;

	if (_access( g_szIniFile, 2) == -1)
		_chmod ( g_szIniFile, _S_IWRITE );

	//writing the new values for the position and size of the main window
	p_stRect = &g_stWinPosSize.stMainPos;
	sprintf( szValue, "(%4d,%4d,%4d,%4d)", p_stRect->left, p_stRect->top, p_stRect->right, p_stRect->bottom );
	WritePrivateProfileString( "WinPref", "Main    ", szValue, g_szIniFile );
}


/****************************************************************************
*		Description:	Verify that the values are correct (can be integers)
*
*		Parameters:		_szString	: string containing the values
*						_p_stRect	: result rectangle	
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_IniData_vStudyRect( char *_szString, RECT *_p_stRect )
{
	char *p_szValue[5],  *p_szEnd;
	char cVal;
	
	//study if the values can be integers
	memset( _p_stRect, 0, sizeof( RECT ) );

	if ( (p_szValue[0] = strchr(_szString, '(' )) == NULL)
		return;

	if ( (p_szValue[1] = strchr (++p_szValue[0], ',')) == NULL)
		return;
	if ( (p_szValue[2] = strchr (++p_szValue[1], ',')) == NULL)
		return;
	if ( (p_szValue[3] = strchr (++p_szValue[2], ',')) == NULL)
		return;
	p_szValue[3] ++;

	p_szValue[4] = _szString + strlen( _szString );

	for (cVal = 0; cVal < 4; cVal ++)
	{
		while ( !isdigit(*p_szValue[cVal]) )
		{
			if (p_szValue[cVal] == p_szValue[cVal+1])
				return;
			p_szValue[cVal]++;
		}
		p_szEnd = p_szValue[cVal] + 1;
		while ( isdigit(*p_szEnd))
		{
			if (p_szEnd == p_szValue[cVal+1])
				return;
			p_szEnd++;
		}
		*p_szEnd = 0;
	}

	//convert to integer the ascii values
	_p_stRect->left   = atoi( p_szValue[0] );
	_p_stRect->top    = atoi( p_szValue[1] );
	_p_stRect->right  = atoi( p_szValue[2] );
	_p_stRect->bottom = atoi( p_szValue[3] );
}





/****************************************************************************
*		Description:	Delete all the text in the rich edir box control
*
*		Parameters:		_oRichEdit	: pointer to the control
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_ComRes_vDeleteLines( CRichEditCtrl *_oRichEdit)
{
	//delete the content of the rich edit control (result and command)
	_oRichEdit->SetWindowText( "" );
}


/****************************************************************************
*		Description:	Delete all the text in the rich edir box control
*
*		Parameters:		szLine			: line to write
*						cLineType		: type of the line (normal, warning, error)
*						_oRichEdit		: pointer to the control
*						_stCharFormat	: format of the line
*---------------------------------------------------------------------------
*		Revision date:                                      Author:         
*****************************************************************************/
void fn_ComRes_vWriteLine( char *szLine, char cLineType ,CRichEditCtrl* _oRichEdit, CHARFORMAT* _stCharFormat)
{
	long	lOldLength;
	long	lNewLength;
	long	lSize;

	_stCharFormat->crTextColor = gs_a_xLineColor[ cLineType ];
	_stCharFormat->dwMask = CFM_BOLD | CFM_COLOR;
	if (cLineType == C_ComRes_cTitleLine)
		_stCharFormat->dwEffects |= CFE_BOLD;
	else
		_stCharFormat->dwEffects &= ~CFE_BOLD;

	// write a line in the rich edit control 
	// with different color (red or black) for Ok or for Error
	lOldLength = _oRichEdit->GetTextLength();
	lNewLength = lOldLength + strlen( szLine );
	lSize = _oRichEdit->GetLimitText();
	if (lNewLength > lSize)
	{
		lSize += 10000;
		_oRichEdit->LimitText( lSize );
	}
	_oRichEdit->SetSel( lOldLength, lOldLength ); 
	_oRichEdit->ReplaceSel( szLine );
	
	_oRichEdit->SetSel( lOldLength, -1 );
	_oRichEdit->SetSelectionCharFormat( *_stCharFormat );
	_oRichEdit->SetSel( lNewLength, lNewLength ); 
	_oRichEdit->RedrawWindow(NULL,NULL, RDW_INVALIDATE |RDW_FRAME| RDW_UPDATENOW );
}



