diff --git a/inc/ini.h b/inc/ini.h index dfcee82..e3f073c 100644 --- a/inc/ini.h +++ b/inc/ini.h @@ -12,6 +12,7 @@ typedef struct } INIFILE; void ini_create(INIFILE* ini, char* filename); +BOOL ini_section_exists(INIFILE* ini, LPCSTR section); DWORD ini_get_string(INIFILE* ini, LPCSTR section, LPCSTR key, LPCSTR def, LPSTR buf, DWORD size); BOOL ini_get_bool(INIFILE* ini, LPCSTR section, LPCSTR key, BOOL def); int ini_get_int(INIFILE* ini, LPCSTR section, LPCSTR key, int def); diff --git a/src/ini.c b/src/ini.c index b71f275..3304000 100644 --- a/src/ini.c +++ b/src/ini.c @@ -67,6 +67,33 @@ void ini_create(INIFILE* ini, char* filename) } } +BOOL ini_section_exists(INIFILE* ini, LPCSTR section) +{ + if (!ini || !ini->sections || !section || strlen(section) == 0) + { + return FALSE; + } + + char s[MAX_PATH]; + strncpy(s, section, sizeof(s) - 1); + s[sizeof(s) - 1] = 0; + + for (char* p = s; *p; ++p) + *p = tolower(*p); + + unsigned long hash = Crc32_ComputeBuf(0, s, strlen(s)); + + for (int i = 0; ini->sections[i].hash; i++) + { + if (ini->sections[i].hash == hash) + { + return TRUE; + } + } + + return FALSE; +} + DWORD ini_get_string(INIFILE* ini, LPCSTR section, LPCSTR key, LPCSTR def, LPSTR buf, DWORD size) { if (!buf || size == 0) @@ -88,7 +115,7 @@ DWORD ini_get_string(INIFILE* ini, LPCSTR section, LPCSTR key, LPCSTR def, LPSTR char s[MAX_PATH]; strncpy(s, section, sizeof(s) - 1); - buf[sizeof(s) - 1] = 0; + s[sizeof(s) - 1] = 0; for (char* p = s; *p; ++p) *p = tolower(*p);