[saco] Implement and match all CConfig member functions

This commit is contained in:
RD42 2024-04-16 22:42:10 +08:00
parent 5177bdcb11
commit d86fdfc4eb
2 changed files with 416 additions and 114 deletions

View File

@ -1,19 +1,24 @@
#include "main.h"
#include <stdio.h>
#include <string.h>
//----------------------------------------------------------
CConfig::CConfig(char* a2)
CConfig::CConfig(char* szFileName)
{
for(int i = 0; i != MAX_CONFIG_ENTRIES; i++) {
memset(&field_0[i], 0, sizeof(UNNAMED_STRUCT_1));
field_7A00[i] = 0;
for(int i = 0; i != MAX_CONFIG_VARIABLES; i++)
{
memset(&m_Variables[i], 0, sizeof(ConfigVariable_s));
m_bVariableSlotState[i] = FALSE;
}
field_8305 = 0;
m_iUpperIndex = 0;
if(a2 && strlen(a2)) {
strcpy(field_8200, a2);
if(szFileName && strlen(szFileName))
{
strcpy(m_szConfigFileName, szFileName);
ReadFile();
}
@ -21,10 +26,368 @@ CConfig::CConfig(char* a2)
//----------------------------------------------------------
CConfig::~CConfig()
{
int i = 0;
while(i != MAX_CONFIG_VARIABLES)
{
if(m_bVariableSlotState[i])
{
if(m_Variables[i].szVarValue)
free(m_Variables[i].szVarValue);
}
m_bVariableSlotState[i] = FALSE;
i++;
}
}
//----------------------------------------------------------
void CConfig::RecalcSize()
{
int iNewIndex = 0;
int i = 0;
do
{
if(m_bVariableSlotState[i])
iNewIndex = i;
if(m_bVariableSlotState[i+1])
iNewIndex = i+1;
if(m_bVariableSlotState[i+2])
iNewIndex = i+2;
if(m_bVariableSlotState[i+3])
iNewIndex = i+3;
i += 4;
}
while(i < MAX_CONFIG_VARIABLES);
m_iUpperIndex = iNewIndex + 1;
}
//----------------------------------------------------------
int CConfig::FindVariableIndex(char *szName)
{
strlwr(szName);
int i = 0;
while(i < m_iUpperIndex)
{
if(m_bVariableSlotState[i])
{
if(!strcmp(szName, m_Variables[i].szVarName))
{
return i;
}
}
i++;
}
return -1;
}
//----------------------------------------------------------
bool CConfig::IsVariableExist(char *szName)
{
if(FindVariableIndex(szName) >= 0) return true;
return false;
}
//----------------------------------------------------------
int CConfig::AddVariable(char *szName)
{
int iIndex = 0;
int i = 0;
do
{
if(!m_bVariableSlotState[i])
{
break;
}
if(!m_bVariableSlotState[i+1])
{
iIndex += 1;
break;
}
if(!m_bVariableSlotState[i+2])
{
iIndex += 2;
break;
}
if(!m_bVariableSlotState[i+3])
{
iIndex += 3;
break;
}
i += 4;
iIndex += 4;
}
while(i < MAX_CONFIG_VARIABLES);
if(iIndex == MAX_CONFIG_VARIABLES) return -1;
if(strlen(szName) > MAX_CONFIG_VARIABLE_NAME) return -1;
strlwr(szName);
strcpy(m_Variables[iIndex].szVarName, szName);
m_Variables[iIndex].bReadOnly = FALSE;
m_Variables[iIndex].VarType = CONFIG_VARTYPE_NONE;
m_bVariableSlotState[iIndex] = TRUE;
RecalcSize();
return iIndex;
}
//----------------------------------------------------------
int CConfig::GetIntVariable(char *szName)
{
int iIndex = FindVariableIndex(szName);
if(iIndex < 0) return 0;
if(m_Variables[iIndex].VarType != CONFIG_VARTYPE_INT) return 0;
return m_Variables[iIndex].iVarValue;
}
//----------------------------------------------------------
char *CConfig::GetStringVariable(char *szName)
{
int iIndex = FindVariableIndex(szName);
if(iIndex < 0) return NULL;
if(m_Variables[iIndex].VarType != CONFIG_VARTYPE_STRING) return NULL;
return m_Variables[iIndex].szVarValue;
}
//----------------------------------------------------------
float CConfig::GetFloatVariable(char *szName)
{
int iIndex = FindVariableIndex(szName);
if(iIndex < 0) return 0.0f;
if(m_Variables[iIndex].VarType != CONFIG_VARTYPE_FLOAT) return 0.0f;
return m_Variables[iIndex].fVarValue;
}
//----------------------------------------------------------
BOOL CConfig::RemoveVariable(char *szName)
{
int iIndex = FindVariableIndex(szName);
if(iIndex < 0) return FALSE;
if(m_Variables[iIndex].szVarValue )
free(m_Variables[iIndex].szVarValue);
memset(&m_Variables[iIndex],0,sizeof(ConfigVariable_s));
m_bVariableSlotState[iIndex] = FALSE;
RecalcSize();
return TRUE;
}
//----------------------------------------------------------
CONFIG_VARTYPE CConfig::GetVariableType(char *szName)
{
int iIndex = FindVariableIndex(szName);
if(iIndex < 0) return CONFIG_VARTYPE_NONE;
return m_Variables[iIndex].VarType;
}
//----------------------------------------------------------
ConfigVariable_s *CConfig::GetVariableAtIndex(int iIndex)
{
if(iIndex >= 0 && iIndex < MAX_CONFIG_VARIABLES)
{
if(m_bVariableSlotState[iIndex])
{
return &m_Variables[iIndex];
}
}
return NULL;
}
//----------------------------------------------------------
CONFIG_VARTYPE CConfig::DetermineDataType(char *szData)
{
if(!szData || !strlen(szData)) return CONFIG_VARTYPE_NONE;
if(szData[0] == '"' && szData[strlen(szData)-1] == '"')
return CONFIG_VARTYPE_STRING;
else if(strchr(szData, '.') != NULL)
return CONFIG_VARTYPE_FLOAT;
else
return CONFIG_VARTYPE_INT;
}
//----------------------------------------------------------
BOOL CConfig::WriteFile()
{
FILE *fWriteFile = fopen(m_szConfigFileName, "w");
if(!fWriteFile) return FALSE;
int i = 0;
do
{
if(m_bVariableSlotState[i])
{
switch(m_Variables[i].VarType)
{
case CONFIG_VARTYPE_INT:
fprintf(fWriteFile, "%s=%d\n", m_Variables[i].szVarName, m_Variables[i].iVarValue);
break;
case CONFIG_VARTYPE_STRING:
if(m_Variables[i].szVarValue)
fprintf(fWriteFile, "%s=\"%s\"\n", m_Variables[i].szVarName, m_Variables[i].szVarValue);
break;
case CONFIG_VARTYPE_FLOAT:
fprintf(fWriteFile, "%s=%f\n", m_Variables[i].szVarName, m_Variables[i].fVarValue);
break;
}
}
i++;
}
while(i != MAX_CONFIG_VARIABLES);
fclose(fWriteFile);
return TRUE;
}
//----------------------------------------------------------
BOOL CConfig::SetIntVariable(char *szName, int iValue, BOOL bReadOnly)
{
int iIndex = FindVariableIndex(szName);
if(iIndex < 0)
{
iIndex = AddVariable(szName);
if (iIndex < 0)
return FALSE;
}
if(!bReadOnly)
{
if(m_Variables[iIndex].bReadOnly)
{
return FALSE;
}
}
else
m_Variables[iIndex].bReadOnly = TRUE;
m_Variables[iIndex].iVarValue = iValue;
m_Variables[iIndex].VarType = CONFIG_VARTYPE_INT;
WriteFile();
return TRUE;
}
//----------------------------------------------------------
BOOL CConfig::SetStringVariable(char *szName, char *szValue, BOOL bReadOnly)
{
int iIndex = FindVariableIndex(szName);
if(iIndex < 0)
{
iIndex = AddVariable(szName);
if(iIndex < 0)
return FALSE;
}
if(!bReadOnly)
{
if(m_Variables[iIndex].bReadOnly)
{
return FALSE;
}
}
else
m_Variables[iIndex].bReadOnly = TRUE;
m_Variables[iIndex].VarType = CONFIG_VARTYPE_STRING;
if(m_Variables[iIndex].szVarValue)
free(m_Variables[iIndex].szVarValue);
m_Variables[iIndex].szVarValue = (char *)calloc(1, strlen(szValue) + 1);
strcpy(m_Variables[iIndex].szVarValue, szValue);
WriteFile();
return TRUE;
}
//----------------------------------------------------------
BOOL CConfig::SetFloatVariable(char *szName, float fValue, BOOL bReadOnly)
{
int iIndex = FindVariableIndex(szName);
if(iIndex < 0)
{
iIndex = AddVariable(szName);
if (iIndex < 0)
return FALSE;
}
if(!bReadOnly)
{
if(m_Variables[iIndex].bReadOnly)
{
return FALSE;
}
}
else
m_Variables[iIndex].bReadOnly = TRUE;
m_Variables[iIndex].fVarValue = fValue;
m_Variables[iIndex].VarType = CONFIG_VARTYPE_FLOAT;
WriteFile();
return TRUE;
}
//----------------------------------------------------------
void CConfig::AddConfigEntry(char *szName, char *szData)
{
// TODO: CConfig::AddConfigEntry 100661E0
size_t len;
switch(DetermineDataType(szData))
{
case CONFIG_VARTYPE_INT:
SetIntVariable(szName, atoi(szData));
break;
case CONFIG_VARTYPE_STRING:
len = strlen(szData);
strncpy(szData, szData + 1, len - 1);
szData[len - 2] = 0;
SetStringVariable(szName, szData);
break;
case CONFIG_VARTYPE_FLOAT:
SetFloatVariable(szName, atof(szData));
break;
}
}
//----------------------------------------------------------
BOOL CConfig::ReadFile()
{
@ -36,7 +399,7 @@ BOOL CConfig::ReadFile()
int iDirectiveLength;
int iDirectiveDataLength;
FILE *fReadFile = fopen(field_8200,"r");
FILE *fReadFile = fopen(m_szConfigFileName,"r");
if(!fReadFile) return FALSE;
@ -123,78 +486,5 @@ BOOL CConfig::ReadFile()
return TRUE;
}
void CConfig::sub_10066180()
{
// TODO: CConfig::sub_10066180 10066180
}
void CConfig::sub_100660E0()
{
// TODO: CConfig::sub_100660E0 100660E0
}
void CConfig::sub_10065FD0()
{
// TODO: CConfig::sub_10065FD0 10065FD0
}
void CConfig::sub_10065F30()
{
// TODO: CConfig::sub_10065F30 10065F30 (unused)
}
void CConfig::sub_10065C90()
{
// TODO: CConfig::sub_10065C90 10065C90
}
void CConfig::sub_10065D30()
{
// TODO: CConfig::sub_10065D30 10065D30
}
void CConfig::sub_10065D50()
{
// TODO: CConfig::sub_10065D50 10065D50
}
void CConfig::sub_10065C40()
{
// TODO: CConfig::sub_10065C40 10065C40
}
void CConfig::sub_10065E10()
{
// TODO: CConfig::sub_10065E10 10065E10
}
void CConfig::sub_10065C00()
{
// TODO: CConfig::sub_10065C00 10065C00
}
void CConfig::sub_10065E40()
{
// TODO: CConfig::sub_10065E40 10065E40
}
void CConfig::sub_10065E70()
{
// TODO: CConfig::sub_10065E70 10065E70 (unused)
}
void CConfig::sub_10065EA0()
{
// TODO: CConfig::sub_10065EA0 10065EA0 (unused)
}
void CConfig::sub_10065F00()
{
// TODO: CConfig::sub_10065F00 10065F00 (unused)
}
void CConfig::sub_10066080()
{
// TODO: CConfig::sub_10066080 10066080
}
//----------------------------------------------------------
// EOF

View File

@ -2,41 +2,53 @@
#pragma once
#define MAX_CONFIG_STRSIZE 256
#define MAX_CONFIG_ENTRIES 512
#define MAX_CONFIG_VARIABLES 512
#define MAX_CONFIG_VARIABLE_NAME 40
typedef struct _UNNAMED_STRUCT_1 {
char _gap0[61];
} UNNAMED_STRUCT_1;
enum CONFIG_VARTYPE
{
CONFIG_VARTYPE_NONE = 0,
CONFIG_VARTYPE_INT,
CONFIG_VARTYPE_STRING,
CONFIG_VARTYPE_FLOAT,
};
struct ConfigVariable_s
{
char szVarName[MAX_CONFIG_VARIABLE_NAME+1];
BOOL bReadOnly;
CONFIG_VARTYPE VarType;
int iVarValue;
float fVarValue;
char* szVarValue;
};
class CConfig
{
private:
UNNAMED_STRUCT_1 field_0[MAX_CONFIG_ENTRIES];
int field_7A00[MAX_CONFIG_ENTRIES];
char field_8200[MAX_PATH+1];
int field_8305;
void AddConfigEntry(char * szName, char * szData);
ConfigVariable_s m_Variables[MAX_CONFIG_VARIABLES];
BOOL m_bVariableSlotState[MAX_CONFIG_VARIABLES];
CHAR m_szConfigFileName[MAX_PATH+1];
int m_iUpperIndex;
public:
CConfig(char* a2);
~CConfig();
void RecalcSize();
int FindVariableIndex(char *szName);
bool IsVariableExist(char *szName);
int AddVariable(char *szName);
int GetIntVariable(char *szName);
char *GetStringVariable(char *szName);
float GetFloatVariable(char *szName);
BOOL RemoveVariable(char *szName);
CONFIG_VARTYPE GetVariableType(char *szName);
ConfigVariable_s *GetVariableAtIndex(int iIndex);
CONFIG_VARTYPE DetermineDataType(char *szData);
BOOL WriteFile();
BOOL SetIntVariable(char *szName, int iValue, BOOL bReadOnly = FALSE);
BOOL SetStringVariable(char *szName, char *szValue, BOOL bReadOnly = FALSE);
BOOL SetFloatVariable(char *szName, float fValue, BOOL bReadOnly = FALSE);
void AddConfigEntry(char *szName, char *szData);
BOOL ReadFile();
void sub_10066180();
void sub_100660E0();
void sub_10065FD0();
void sub_10065F30();
void sub_10065C90();
void sub_10065D30();
void sub_10065D50();
void sub_10065C40();
void sub_10065E10();
void sub_10065C00();
void sub_10065E40();
void sub_10065E70();
void sub_10065EA0();
void sub_10065F00();
void sub_10066080();
CConfig(char* szFileName);
};