started working on ini parser

This commit is contained in:
2025-08-20 01:27:25 +03:00
parent 5635cd1d69
commit eff681ca33
47 changed files with 1465 additions and 112 deletions

31
tier2/__build.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include "helper.h"
#include "c.h"
#include "ld.h"
#include "tier1/utlstring.h"
#include "tier1/commandline.h"
CUtlVector<CUtlString> tier2_CompiledFiles = {
"tier2/fileformats/ini.cpp",
"tier2/fileformats/json.cpp",
"tier2/filesystem.cpp",
"tier2/filesystem_libc.cpp",
};
CUtlString tier2_lib;
DECLARE_BUILD_STAGE(tier2)
{
CProject_t compileProject = {};
LinkProject_t ldProject = {};
compileProject.m_szName = "tier2";
compileProject.files = tier2_CompiledFiles;
compileProject.includeDirectories = all_IncludeDirectories;
compileProject.bFPIC = true;
ldProject = ccompiler->Compile(&compileProject);
ldProject.linkType = ELINK_STATIC_LIBRARY;
CUtlString outputProject = linker->Link(&ldProject);
tier2_lib = outputProject;
return 0;
};

View File

@@ -1,7 +1,27 @@
#include "tier2/fileformats/ini.h"
#include "tier0/platform.h"
#include "tier1/utlstring.h"
#include "tier1/utlvector.h"
#include "tier0/lib.h"
enum ESectionType
{
SECTIONTYPE_String,
SECTIONTYPE_Boolean,
SECTIONTYPE_Float,
};
struct SectionData_t
{
ESectionType m_eSectionType;
CUtlString m_szKey;
bool m_bData;
float m_fData;
CUtlString m_szData;
};
abstract_class CINISection: public IINISection
{
@@ -11,27 +31,247 @@ public:
virtual const char *GetStringValue( const char *szKeyName ) override;
virtual CUtlString GetUTLStringValue( const char *szKeyName ) override;
CUtlVector<CUtlString> m_values;
CUtlVector<SectionData_t> m_values;
CUtlString m_szSectionName;
};
bool CINISection::GetBoolValue( const char *szKeyName )
{
}
int CINISection::GetIntValue( const char *szKeyName )
{
}
const char *CINISection::GetStringValue( const char *szKeyName )
{
return 0;
}
CUtlString CINISection::GetUTLStringValue( const char *szKeyName )
{
}
class CINIFile: public IINIFile
{
virtual void GetSection( const char *szSectionName ) override;
public:
virtual IINISection *GetSection( const char *szSectionName ) override;
CUtlVector<CINISection*> m_sections;
};
IINISection *CINIFile::GetSection( const char *szSectionName )
{
for ( auto &section: m_sections )
{
if ( section->m_szSectionName == szSectionName )
return (IINISection*)&section;
}
return 0;
}
class CINIManager: public IINIManager
{
public:
virtual IINIFile *ReadString( const char *psz ) override;
bool IsWordSymbol( char c );
CUtlVector<CUtlString> TokenizeString();
};
IINIFile *CINIManager::ReadString( const char *psz )
{
CINIFile *pFile = new CINIFile;
CINIFile *pFile = 0;
CINIFile file = {};
bool bIsQuoted;
bool bIsSlash;
bool bIsVarName;
bool bIsVarValue;
bool bIsSectionName;
CINISection currentSection;
SectionData_t currentSectionData;
size_t i = 0;
char c;
CUtlString szStringValue;
CUtlVector<CUtlString> tokens;
uint32_t nCurrentLine = 0;
while (true)
{
c = psz[i];
i++;
if (c == '\0')
break;
if (c == '\n')
nCurrentLine++;
if (bIsQuoted)
{
if (bIsSlash)
{
bIsSlash = false;
switch (c)
{
case '\\':
szStringValue.AppendTail('\\');
continue;
case '"':
szStringValue.AppendTail('"');
continue;
case 'n':
szStringValue.AppendTail('\n');
continue;
case 't':
szStringValue.AppendTail('\n');
continue;
case '\n':
szStringValue.AppendTail('\n');
continue;
default:
szStringValue.AppendTail(c);
continue;
}
} else {
switch (c)
{
case '\\':
bIsSlash = true;
continue;
case '\n':
V_printf("line %i: new line was found but the string wasn't finished, did you forget to place \" in the end of the line?\n", nCurrentLine);
case '"':
bIsQuoted = false;
if (szStringValue == 0)
continue;
tokens.AppendTail(szStringValue);
szStringValue = 0;
continue;
default:
szStringValue.AppendTail(c);
continue;
}
}
} else {
if (c == '"')
{
bIsQuoted = true;
}
if (IsWordSymbol(c))
{
szStringValue.AppendTail(c);
continue;
} else {
if (szStringValue != 0)
tokens.AppendTail(szStringValue);
szStringValue = 0;
if (V_isgraph(c) && c != '"')
tokens.AppendTail(CUtlString("%c", c));
}
}
};
currentSectionData.m_eSectionType = SECTIONTYPE_String;
for ( int i = 0; i < tokens.GetSize(); i++ )
{
if (tokens[i] == "]")
{
V_printf("Error: unexpected ]\n");
return 0;
}
if (tokens[i] == "[")
{
i++;
if (i>=tokens.GetSize())
{
V_printf("Error: Trailing section start\n");
return 0;
}
currentSection = {};
currentSection.m_szSectionName = tokens[i];
V_printf("[%s]\n",currentSection.m_szSectionName.GetString());
i++;
if (i>=tokens.GetSize())
{
V_printf("Error: Trailing section name\n");
return 0;
}
if (tokens[i] != "]")
{
V_printf("Error: expected ]\n");
return 0;
}
continue;
}
if (tokens[i] == "=")
{
V_printf("Error: expected key\n");
return 0;
}
currentSectionData = {};
currentSectionData.m_szKey = tokens[i];
i++;
if (i>=tokens.GetSize())
{
V_printf("Error: trailing key\n");
return 0;
}
if (tokens[i] != "=")
{
V_printf("Error: expected =\n");
return 0;
}
i++;
if (i>=tokens.GetSize())
{
V_printf("Error: expected value\n");
return 0;
}
currentSectionData.m_szData = tokens[i];
currentSection.m_values.AppendTail(currentSectionData);
}
pFile = new CINIFile;
*pFile = file;
return (IINIFile*)pFile;
}
bool CINIManager::IsWordSymbol( char c )
{
if (V_isalnum(c))
return true;
switch (c)
{
case '-':
case '_':
case '.':
return true;
default:
return false;
}
}
CUtlVector<CUtlString> TokenizeString();
IINIManager *INIManager()
{

104
tier2/filesystem.cpp Normal file
View File

@@ -0,0 +1,104 @@
#include "tier2/ifilesystem.h"
#include "tier1/interface.h"
#include "tier0/lib.h"
size_t IFileHandle::Write( const void *pData, size_t nDataSize )
{
return m_pFileSystem->Write(this, pData, nDataSize);
}
size_t IFileHandle::Read( void *pData, size_t nDataSize )
{
return m_pFileSystem->Read(this, pData, nDataSize);
}
size_t IFileHandle::Seek( ESeekMode eSeekMode, size_t nOffset )
{
return m_pFileSystem->Seek(this, eSeekMode, nOffset);
}
size_t IFileHandle::Tell( void )
{
return m_pFileSystem->Tell(this);
}
size_t IFileHandle::Size( void )
{
return m_pFileSystem->Size(this);
}
void IFileHandle::Close( void )
{
m_pFileSystem->Close(this);
}
class CFileSystem : public IFileSystem
{
public:
virtual void Init() override {
};
virtual void Shutdown() override {
};
virtual IFileHandle *Open( const char *szFileName, int eOpCode ) override
{
IFileSystem *pFileSystem;
IFileHandle *pHandle = NULL;
for ( pFileSystem = g_pFileSystems; pFileSystem; pFileSystem = pFileSystem->m_pNext )
{
pHandle = pFileSystem->Open(szFileName, eOpCode);
if (pHandle) return pHandle;
}
return NULL;
}
virtual size_t Write( IFileHandle *pFile, const void *pData, size_t nDataSize ) override
{
return pFile->Write(pData, nDataSize);
}
virtual size_t Read( IFileHandle *pFile, void *pData, size_t nDataSize ) override
{
return pFile->Read(pData, nDataSize);
}
virtual size_t Seek( IFileHandle *pFile, ESeekMode eSeekMode, size_t nOffset ) override
{
return pFile->Seek(eSeekMode, nOffset);
}
virtual size_t Tell( IFileHandle *pFile ) override
{
return pFile->Tell();
}
virtual size_t Size( IFileHandle *pFile ) override
{
return pFile->Size();
}
virtual void Close( IFileHandle *pFile ) override
{
}
};
static CFileSystem s_fileSystem;
IFileSystem *filesystem = &s_fileSystem;
IFileSystem *g_pFileSystems;
CFileSystemRegistry::CFileSystemRegistry( InstantiateFileSystemFn fn, const char *szFileSystem )
{
IFileSystem *pFileSystem = fn();
pFileSystem->RegisterFileSystem();
}
void IFileSystem::RegisterFileSystem()
{
m_pNext = g_pFileSystems;
g_pFileSystems = this;
};

112
tier2/filesystem_libc.cpp Normal file
View File

@@ -0,0 +1,112 @@
#include "tier2/ifilesystem.h"
#include "tier1/interface.h"
#include "tier0/lib.h"
class CLIBCFileHandle : public IFileHandle
{
public:
FILE *m_pFile;
size_t m_nSize;
};
class CLIBCFileSystem : public IFileSystem
{
public:
virtual void Init() override {
};
virtual void Shutdown() override {};
virtual IFileHandle *Open( const char *szFileName, int eOpCode ) override
{
const char *szOperation;
FILE *pFile;
CLIBCFileHandle *pHandle = NULL;
switch (eOpCode)
{
case FILEMODE_READ:
szOperation = "rb";
break;
default:
V_printf("Operation is not supported\n");
break;
}
pFile = V_fopen(szFileName, szOperation);
if (!pFile)
return NULL;
pHandle = new CLIBCFileHandle;
pHandle->m_pFileSystem = this;
pHandle->m_pFile = pFile;
pHandle->Seek(SEEKMODE_RELATIVE_START, 0);
pHandle->m_nSize = pHandle->Tell();
pHandle->Seek(SEEKMODE_RELATIVE_END, 0);
return pHandle;
}
virtual size_t Write( IFileHandle *pFile, const void *pData, size_t nDataSize ) override
{
return 0;
}
virtual size_t Read( IFileHandle *pFile, void *pData, size_t nDataSize ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
if (!pHandle)
return 0;
return V_fread( pData, 1, nDataSize, pHandle->m_pFile );
}
virtual size_t Seek( IFileHandle *pFile, ESeekMode eSeekMode, size_t nOffset ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
int eLibcSeekMode = 0;
if (!pHandle)
return 0;
switch (eSeekMode) {
case SEEKMODE_RELATIVE_CURRENT:
eLibcSeekMode = SEEK_CUR;
break;
case SEEKMODE_RELATIVE_END:
eLibcSeekMode = SEEK_END;
break;
case SEEKMODE_RELATIVE_START:
eLibcSeekMode = SEEK_SET;
break;
}
return V_fseek( pHandle->m_pFile, nOffset, eLibcSeekMode );
}
virtual size_t Tell( IFileHandle *pFile ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
if (!pHandle)
return 0;
return V_ftell(pHandle->m_pFile);
}
virtual size_t Size( IFileHandle *pFile ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
if (!pHandle)
return 0;
return pHandle->m_nSize;
}
virtual void Close( IFileHandle *pFile ) override
{
CLIBCFileHandle *pHandle = (CLIBCFileHandle*)pFile;
if (!pHandle)
return;
V_fclose(pHandle->m_pFile);
delete pHandle;
}
};
EXPOSE_FILESYSTEM(CLIBCFileSystem, "sysfs");