2014-03-25 02:20:34 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* .ini config file
|
|
|
|
*
|
|
|
|
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
#include <errno.h>
|
2017-08-29 10:09:38 +03:00
|
|
|
#include <winpr/wtypes.h>
|
2014-03-25 02:20:34 +04:00
|
|
|
#include <winpr/crt.h>
|
2021-05-25 20:27:13 +03:00
|
|
|
#include <winpr/file.h>
|
2014-03-25 02:20:34 +04:00
|
|
|
|
|
|
|
#include <winpr/ini.h>
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
struct _wIniFileKey
|
2016-02-10 18:51:27 +03:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
char* name;
|
|
|
|
char* value;
|
|
|
|
};
|
|
|
|
typedef struct _wIniFileKey wIniFileKey;
|
|
|
|
|
|
|
|
struct _wIniFileSection
|
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
size_t nKeys;
|
|
|
|
size_t cKeys;
|
|
|
|
wIniFileKey** keys;
|
|
|
|
};
|
|
|
|
typedef struct _wIniFileSection wIniFileSection;
|
|
|
|
|
|
|
|
struct _wIniFile
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
char* line;
|
|
|
|
char* nextLine;
|
|
|
|
size_t lineLength;
|
|
|
|
char* tokctx;
|
|
|
|
char* buffer;
|
|
|
|
char* filename;
|
|
|
|
BOOL readOnly;
|
|
|
|
size_t nSections;
|
|
|
|
size_t cSections;
|
|
|
|
wIniFileSection** sections;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BOOL IniFile_Load_NextLine(wIniFile* ini, char* str)
|
|
|
|
{
|
|
|
|
size_t length = 0;
|
2016-02-10 18:51:27 +03:00
|
|
|
ini->nextLine = strtok_s(str, "\n", &ini->tokctx);
|
|
|
|
|
|
|
|
if (ini->nextLine)
|
2019-10-29 13:54:55 +03:00
|
|
|
length = strlen(ini->nextLine);
|
2016-02-10 18:51:27 +03:00
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
if (ini->nextLine[length - 1] == '\r')
|
|
|
|
{
|
|
|
|
ini->nextLine[length - 1] = '\0';
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length < 1)
|
|
|
|
ini->nextLine = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ini->nextLine) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static BOOL IniFile_Load_String(wIniFile* ini, const char* iniString)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t fileSize;
|
|
|
|
if (!ini || !iniString)
|
|
|
|
return FALSE;
|
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
ini->line = NULL;
|
|
|
|
ini->nextLine = NULL;
|
|
|
|
ini->buffer = NULL;
|
2019-10-29 13:54:55 +03:00
|
|
|
fileSize = strlen(iniString);
|
2014-03-25 06:41:30 +04:00
|
|
|
|
|
|
|
if (fileSize < 1)
|
2019-10-29 13:54:55 +03:00
|
|
|
return FALSE;
|
2014-03-25 06:41:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
ini->buffer = (char*)malloc(fileSize + 2);
|
2014-10-03 04:16:30 +04:00
|
|
|
|
|
|
|
if (!ini->buffer)
|
2019-10-29 13:54:55 +03:00
|
|
|
return FALSE;
|
2014-10-03 04:16:30 +04:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
CopyMemory(ini->buffer, iniString, fileSize);
|
|
|
|
ini->buffer[fileSize] = '\n';
|
|
|
|
ini->buffer[fileSize + 1] = '\0';
|
2016-02-10 18:51:27 +03:00
|
|
|
IniFile_Load_NextLine(ini, ini->buffer);
|
2019-10-29 13:54:55 +03:00
|
|
|
return TRUE;
|
2014-03-25 06:41:30 +04:00
|
|
|
}
|
|
|
|
|
2019-11-05 12:31:09 +03:00
|
|
|
static BOOL IniFile_Open_File(wIniFile* ini, const char* filename)
|
2014-03-25 06:41:30 +04:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
if (!ini || !filename)
|
2019-11-05 12:31:09 +03:00
|
|
|
return FALSE;
|
2019-10-29 13:54:55 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (ini->readOnly)
|
2021-05-25 20:27:13 +03:00
|
|
|
ini->fp = winpr_fopen(filename, "rb");
|
2014-03-25 06:41:30 +04:00
|
|
|
else
|
2021-05-25 20:27:13 +03:00
|
|
|
ini->fp = winpr_fopen(filename, "w+b");
|
2014-03-25 06:41:30 +04:00
|
|
|
|
|
|
|
if (!ini->fp)
|
2019-11-05 12:31:09 +03:00
|
|
|
return FALSE;
|
2014-03-25 06:41:30 +04:00
|
|
|
|
2019-11-05 12:31:09 +03:00
|
|
|
return TRUE;
|
2014-10-03 04:16:30 +04:00
|
|
|
}
|
|
|
|
|
2019-11-05 12:31:09 +03:00
|
|
|
static BOOL IniFile_Load_File(wIniFile* ini, const char* filename)
|
2014-10-03 04:16:30 +04:00
|
|
|
{
|
2017-08-11 11:07:46 +03:00
|
|
|
INT64 fileSize;
|
2014-10-03 04:16:30 +04:00
|
|
|
|
2019-11-05 12:31:09 +03:00
|
|
|
if (!IniFile_Open_File(ini, filename))
|
|
|
|
return FALSE;
|
2014-10-03 04:16:30 +04:00
|
|
|
|
2017-08-11 11:07:46 +03:00
|
|
|
if (_fseeki64(ini->fp, 0, SEEK_END) < 0)
|
2015-06-26 16:58:01 +03:00
|
|
|
goto out_file;
|
2016-02-10 18:51:27 +03:00
|
|
|
|
2017-08-11 11:07:46 +03:00
|
|
|
fileSize = _ftelli64(ini->fp);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-06-26 16:58:01 +03:00
|
|
|
if (fileSize < 0)
|
|
|
|
goto out_file;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2017-08-11 11:07:46 +03:00
|
|
|
if (_fseeki64(ini->fp, 0, SEEK_SET) < 0)
|
2015-06-26 16:58:01 +03:00
|
|
|
goto out_file;
|
2014-03-25 02:20:34 +04:00
|
|
|
|
|
|
|
ini->line = NULL;
|
|
|
|
ini->nextLine = NULL;
|
|
|
|
ini->buffer = NULL;
|
|
|
|
|
|
|
|
if (fileSize < 1)
|
2015-06-26 16:58:01 +03:00
|
|
|
goto out_file;
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
ini->buffer = (char*)malloc((size_t)fileSize + 2);
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2014-10-03 04:16:30 +04:00
|
|
|
if (!ini->buffer)
|
2015-06-26 16:58:01 +03:00
|
|
|
goto out_file;
|
2014-10-03 04:16:30 +04:00
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
if (fread(ini->buffer, (size_t)fileSize, 1, ini->fp) != 1)
|
2015-06-26 16:58:01 +03:00
|
|
|
goto out_buffer;
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2014-10-03 04:16:30 +04:00
|
|
|
fclose(ini->fp);
|
|
|
|
ini->fp = NULL;
|
2014-03-25 02:20:34 +04:00
|
|
|
ini->buffer[fileSize] = '\n';
|
|
|
|
ini->buffer[fileSize + 1] = '\0';
|
2016-02-10 18:51:27 +03:00
|
|
|
IniFile_Load_NextLine(ini, ini->buffer);
|
2019-11-05 12:31:09 +03:00
|
|
|
return TRUE;
|
2015-06-26 16:58:01 +03:00
|
|
|
out_buffer:
|
|
|
|
free(ini->buffer);
|
|
|
|
ini->buffer = NULL;
|
|
|
|
out_file:
|
|
|
|
fclose(ini->fp);
|
|
|
|
ini->fp = NULL;
|
2019-11-05 12:31:09 +03:00
|
|
|
return FALSE;
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static void IniFile_Load_Finish(wIniFile* ini)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
|
|
|
if (!ini)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ini->buffer)
|
|
|
|
{
|
|
|
|
free(ini->buffer);
|
|
|
|
ini->buffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static BOOL IniFile_Load_HasNextLine(wIniFile* ini)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
|
|
|
if (!ini)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return (ini->nextLine) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static char* IniFile_Load_GetNextLine(wIniFile* ini)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
|
|
|
if (!ini)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ini->line = ini->nextLine;
|
2019-10-29 13:54:55 +03:00
|
|
|
ini->lineLength = strlen(ini->line);
|
2016-02-10 18:51:27 +03:00
|
|
|
IniFile_Load_NextLine(ini, NULL);
|
2014-03-25 02:20:34 +04:00
|
|
|
return ini->line;
|
|
|
|
}
|
|
|
|
|
2020-05-15 15:49:52 +03:00
|
|
|
static void IniFile_Key_Free(wIniFileKey* key)
|
|
|
|
{
|
|
|
|
if (!key)
|
|
|
|
return;
|
|
|
|
|
|
|
|
free(key->name);
|
|
|
|
free(key->value);
|
|
|
|
free(key);
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static wIniFileKey* IniFile_Key_New(const char* name, const char* value)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
wIniFileKey* key;
|
|
|
|
|
|
|
|
if (!name || !value)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
key = malloc(sizeof(wIniFileKey));
|
2014-03-25 02:20:34 +04:00
|
|
|
|
|
|
|
if (key)
|
|
|
|
{
|
|
|
|
key->name = _strdup(name);
|
|
|
|
key->value = _strdup(value);
|
2016-02-10 18:51:27 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!key->name || !key->value)
|
|
|
|
{
|
2020-05-15 15:49:52 +03:00
|
|
|
IniFile_Key_Free(key);
|
2015-06-17 23:08:02 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static wIniFileSection* IniFile_Section_New(const char* name)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
wIniFileSection* section;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
section = malloc(sizeof(wIniFileSection));
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!section)
|
|
|
|
return NULL;
|
|
|
|
|
2014-03-25 02:20:34 +04:00
|
|
|
section->name = _strdup(name);
|
2016-02-10 18:51:27 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!section->name)
|
|
|
|
{
|
|
|
|
free(section);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
|
|
|
|
section->nKeys = 0;
|
|
|
|
section->cKeys = 64;
|
2019-11-06 17:24:51 +03:00
|
|
|
section->keys = (wIniFileKey**)calloc(section->cKeys, sizeof(wIniFileKey*));
|
2016-02-10 18:51:27 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!section->keys)
|
|
|
|
{
|
|
|
|
free(section->name);
|
|
|
|
free(section);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
|
|
|
|
return section;
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static void IniFile_Section_Free(wIniFileSection* section)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t index;
|
2014-10-03 02:45:53 +04:00
|
|
|
|
|
|
|
if (!section)
|
|
|
|
return;
|
|
|
|
|
2016-03-08 05:21:06 +03:00
|
|
|
free(section->name);
|
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
for (index = 0; index < section->nKeys; index++)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2014-10-03 02:45:53 +04:00
|
|
|
IniFile_Key_Free(section->keys[index]);
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
2014-10-03 02:45:53 +04:00
|
|
|
|
2016-03-08 05:21:06 +03:00
|
|
|
free(section->keys);
|
2014-10-03 02:45:53 +04:00
|
|
|
free(section);
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static wIniFileSection* IniFile_GetSection(wIniFile* ini, const char* name)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t index;
|
2014-10-03 02:45:53 +04:00
|
|
|
wIniFileSection* section = NULL;
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
if (!ini || !name)
|
|
|
|
return NULL;
|
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
for (index = 0; index < ini->nSections; index++)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2014-10-03 02:45:53 +04:00
|
|
|
if (_stricmp(name, ini->sections[index]->name) == 0)
|
|
|
|
{
|
|
|
|
section = ini->sections[index];
|
|
|
|
break;
|
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
return section;
|
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static wIniFileSection* IniFile_AddSection(wIniFile* ini, const char* name)
|
2014-10-03 02:45:53 +04:00
|
|
|
{
|
|
|
|
wIniFileSection* section;
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
if (!ini || !name)
|
2014-10-03 02:45:53 +04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
section = IniFile_GetSection(ini, name);
|
|
|
|
|
|
|
|
if (!section)
|
|
|
|
{
|
|
|
|
if ((ini->nSections + 1) >= (ini->cSections))
|
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t new_size;
|
2015-03-11 09:59:56 +03:00
|
|
|
wIniFileSection** new_sect;
|
|
|
|
new_size = ini->cSections * 2;
|
2019-11-06 17:24:51 +03:00
|
|
|
new_sect =
|
|
|
|
(wIniFileSection**)realloc(ini->sections, sizeof(wIniFileSection*) * new_size);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-11 09:59:56 +03:00
|
|
|
if (!new_sect)
|
|
|
|
return NULL;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-11 09:59:56 +03:00
|
|
|
ini->cSections = new_size;
|
|
|
|
ini->sections = new_sect;
|
2014-10-03 02:45:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
section = IniFile_Section_New(name);
|
|
|
|
ini->sections[ini->nSections] = section;
|
|
|
|
ini->nSections++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return section;
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static wIniFileKey* IniFile_GetKey(wIniFile* ini, wIniFileSection* section, const char* name)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t index;
|
2014-10-03 02:45:53 +04:00
|
|
|
wIniFileKey* key = NULL;
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
if (!ini || !section || !name)
|
|
|
|
return NULL;
|
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
for (index = 0; index < section->nKeys; index++)
|
|
|
|
{
|
|
|
|
if (_stricmp(name, section->keys[index]->name) == 0)
|
|
|
|
{
|
|
|
|
key = section->keys[index];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static wIniFileKey* IniFile_AddKey(wIniFile* ini, wIniFileSection* section, const char* name,
|
2019-11-06 17:24:51 +03:00
|
|
|
const char* value)
|
2014-10-03 02:45:53 +04:00
|
|
|
{
|
|
|
|
wIniFileKey* key;
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
if (!section || !name || !value)
|
2014-10-03 02:45:53 +04:00
|
|
|
return NULL;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
key = IniFile_GetKey(ini, section, name);
|
|
|
|
|
|
|
|
if (!key)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2014-10-03 02:45:53 +04:00
|
|
|
if ((section->nKeys + 1) >= (section->cKeys))
|
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t new_size;
|
2015-03-11 09:59:56 +03:00
|
|
|
wIniFileKey** new_key;
|
|
|
|
new_size = section->cKeys * 2;
|
2019-11-06 17:24:51 +03:00
|
|
|
new_key = (wIniFileKey**)realloc(section->keys, sizeof(wIniFileKey*) * new_size);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-11 09:59:56 +03:00
|
|
|
if (!new_key)
|
|
|
|
return NULL;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-03-11 09:59:56 +03:00
|
|
|
section->cKeys = new_size;
|
|
|
|
section->keys = new_key;
|
2014-10-03 02:45:53 +04:00
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
key = IniFile_Key_New(name, value);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!key)
|
|
|
|
return NULL;
|
2016-02-10 18:51:27 +03:00
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
section->keys[section->nKeys] = key;
|
|
|
|
section->nKeys++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(key->value);
|
|
|
|
key->value = _strdup(value);
|
2016-02-10 18:51:27 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!key->value)
|
|
|
|
return NULL;
|
2014-10-03 02:45:53 +04:00
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
return key;
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
static int IniFile_Load(wIniFile* ini)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
|
|
|
char* line;
|
|
|
|
char* name;
|
|
|
|
char* value;
|
|
|
|
char* separator;
|
2019-11-06 17:24:51 +03:00
|
|
|
char *beg, *end;
|
2014-03-25 02:20:34 +04:00
|
|
|
wIniFileSection* section = NULL;
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
if (!ini)
|
|
|
|
return -1;
|
|
|
|
|
2014-03-25 02:20:34 +04:00
|
|
|
while (IniFile_Load_HasNextLine(ini))
|
|
|
|
{
|
|
|
|
line = IniFile_Load_GetNextLine(ini);
|
|
|
|
|
|
|
|
if (line[0] == ';')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (line[0] == '[')
|
|
|
|
{
|
|
|
|
beg = &line[1];
|
|
|
|
end = strchr(line, ']');
|
|
|
|
|
|
|
|
if (!end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*end = '\0';
|
|
|
|
IniFile_AddSection(ini, beg);
|
|
|
|
section = ini->sections[ini->nSections - 1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
separator = strchr(line, '=');
|
2019-04-08 10:26:46 +03:00
|
|
|
|
|
|
|
if (separator == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2014-03-25 08:32:05 +04:00
|
|
|
end = separator;
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2014-03-25 08:32:05 +04:00
|
|
|
while ((&end[-1] > line) && ((end[-1] == ' ') || (end[-1] == '\t')))
|
2014-03-25 02:20:34 +04:00
|
|
|
end--;
|
|
|
|
|
|
|
|
*end = '\0';
|
|
|
|
name = line;
|
|
|
|
beg = separator + 1;
|
|
|
|
|
|
|
|
while (*beg && ((*beg == ' ') || (*beg == '\t')))
|
|
|
|
beg++;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (*beg == '"')
|
|
|
|
beg++;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
end = &line[ini->lineLength];
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
while ((end > beg) && ((end[-1] == ' ') || (end[-1] == '\t')))
|
|
|
|
end--;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (end[-1] == '"')
|
|
|
|
end[-1] = '\0';
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 02:20:34 +04:00
|
|
|
value = beg;
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!IniFile_AddKey(ini, section, name, value))
|
|
|
|
return -1;
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IniFile_Load_Finish(ini);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
int IniFile_ReadBuffer(wIniFile* ini, const char* buffer)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
BOOL status;
|
|
|
|
if (!ini || !buffer)
|
|
|
|
return -1;
|
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
ini->readOnly = TRUE;
|
|
|
|
ini->filename = NULL;
|
2014-10-03 02:45:53 +04:00
|
|
|
status = IniFile_Load_String(ini, buffer);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
if (!status)
|
|
|
|
return -1;
|
2014-03-25 06:41:30 +04:00
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
return IniFile_Load(ini);
|
2014-03-25 06:41:30 +04:00
|
|
|
}
|
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
int IniFile_ReadFile(wIniFile* ini, const char* filename)
|
2014-03-25 06:41:30 +04:00
|
|
|
{
|
|
|
|
ini->readOnly = TRUE;
|
2014-10-03 04:16:30 +04:00
|
|
|
free(ini->filename);
|
2014-03-25 06:41:30 +04:00
|
|
|
ini->filename = _strdup(filename);
|
2016-02-10 18:51:27 +03:00
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!ini->filename)
|
|
|
|
return -1;
|
2014-03-25 06:41:30 +04:00
|
|
|
|
2019-11-05 12:31:09 +03:00
|
|
|
if (!IniFile_Load_File(ini, filename))
|
|
|
|
return -1;
|
2014-03-25 06:41:30 +04:00
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
return IniFile_Load(ini);
|
2014-03-25 06:41:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char** IniFile_GetSectionNames(wIniFile* ini, int* count)
|
|
|
|
{
|
|
|
|
char* p;
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t index;
|
|
|
|
size_t length;
|
|
|
|
size_t nameLength;
|
2014-03-25 06:41:30 +04:00
|
|
|
char** sectionNames;
|
|
|
|
wIniFileSection* section = NULL;
|
2019-10-29 13:54:55 +03:00
|
|
|
|
|
|
|
if (!ini || !count)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (ini->nSections > INT_MAX)
|
|
|
|
return NULL;
|
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
length = (sizeof(char*) * ini->nSections) + sizeof(char);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
for (index = 0; index < ini->nSections; index++)
|
|
|
|
{
|
|
|
|
section = ini->sections[index];
|
2019-10-29 13:54:55 +03:00
|
|
|
nameLength = strlen(section->name);
|
2014-03-25 06:41:30 +04:00
|
|
|
length += (nameLength + 1);
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
sectionNames = (char**)malloc(length);
|
2016-02-10 18:51:27 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!sectionNames)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
p = (char*)&((BYTE*)sectionNames)[sizeof(char*) * ini->nSections];
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
for (index = 0; index < ini->nSections; index++)
|
|
|
|
{
|
|
|
|
sectionNames[index] = p;
|
|
|
|
section = ini->sections[index];
|
2019-10-29 13:54:55 +03:00
|
|
|
nameLength = strlen(section->name);
|
2014-03-25 06:41:30 +04:00
|
|
|
CopyMemory(p, section->name, nameLength + 1);
|
|
|
|
p += (nameLength + 1);
|
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
*p = '\0';
|
2019-10-29 13:54:55 +03:00
|
|
|
*count = (int)ini->nSections;
|
2014-03-25 06:41:30 +04:00
|
|
|
return sectionNames;
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
char** IniFile_GetSectionKeyNames(wIniFile* ini, const char* section, int* count)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2014-03-25 06:41:30 +04:00
|
|
|
char* p;
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t index;
|
|
|
|
size_t length;
|
|
|
|
size_t nameLength;
|
2014-03-25 06:41:30 +04:00
|
|
|
char** keyNames;
|
|
|
|
wIniFileKey* pKey = NULL;
|
|
|
|
wIniFileSection* pSection = NULL;
|
2019-10-29 13:54:55 +03:00
|
|
|
|
|
|
|
if (!ini || !section || !count)
|
|
|
|
return NULL;
|
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
pSection = IniFile_GetSection(ini, section);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (!pSection)
|
|
|
|
return NULL;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
if (pSection->nKeys > INT_MAX)
|
|
|
|
return NULL;
|
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
length = (sizeof(char*) * pSection->nKeys) + sizeof(char);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
for (index = 0; index < pSection->nKeys; index++)
|
|
|
|
{
|
|
|
|
pKey = pSection->keys[index];
|
2019-10-29 13:54:55 +03:00
|
|
|
nameLength = strlen(pKey->name);
|
2014-03-25 06:41:30 +04:00
|
|
|
length += (nameLength + 1);
|
|
|
|
}
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
keyNames = (char**)malloc(length);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!keyNames)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
p = (char*)&((BYTE*)keyNames)[sizeof(char*) * pSection->nKeys];
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
for (index = 0; index < pSection->nKeys; index++)
|
|
|
|
{
|
|
|
|
keyNames[index] = p;
|
|
|
|
pKey = pSection->keys[index];
|
2019-10-29 13:54:55 +03:00
|
|
|
nameLength = strlen(pKey->name);
|
2014-03-25 06:41:30 +04:00
|
|
|
CopyMemory(p, pKey->name, nameLength + 1);
|
|
|
|
p += (nameLength + 1);
|
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
*p = '\0';
|
2019-10-29 13:54:55 +03:00
|
|
|
*count = (int)pSection->nKeys;
|
2014-03-25 06:41:30 +04:00
|
|
|
return keyNames;
|
|
|
|
}
|
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
const char* IniFile_GetKeyValueString(wIniFile* ini, const char* section, const char* key)
|
2014-03-25 06:41:30 +04:00
|
|
|
{
|
2014-10-03 02:45:53 +04:00
|
|
|
const char* value = NULL;
|
2014-03-25 06:41:30 +04:00
|
|
|
wIniFileKey* pKey = NULL;
|
|
|
|
wIniFileSection* pSection = NULL;
|
|
|
|
pSection = IniFile_GetSection(ini, section);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (!pSection)
|
|
|
|
return NULL;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
pKey = IniFile_GetKey(ini, pSection, key);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (!pKey)
|
|
|
|
return NULL;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
value = (const char*)pKey->value;
|
2014-03-25 06:41:30 +04:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
int IniFile_GetKeyValueInt(wIniFile* ini, const char* section, const char* key)
|
2014-03-25 06:41:30 +04:00
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
int err;
|
2017-11-14 18:10:52 +03:00
|
|
|
long value = 0;
|
2014-03-25 06:41:30 +04:00
|
|
|
wIniFileKey* pKey = NULL;
|
|
|
|
wIniFileSection* pSection = NULL;
|
|
|
|
pSection = IniFile_GetSection(ini, section);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (!pSection)
|
|
|
|
return 0;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
pKey = IniFile_GetKey(ini, pSection, key);
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2014-03-25 06:41:30 +04:00
|
|
|
if (!pKey)
|
|
|
|
return 0;
|
2017-11-14 18:10:52 +03:00
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
err = errno;
|
|
|
|
errno = 0;
|
2017-11-14 18:10:52 +03:00
|
|
|
value = strtol(pKey->value, NULL, 0);
|
2019-10-29 13:54:55 +03:00
|
|
|
if ((value < INT_MIN) || (value > INT_MAX) || (errno != 0))
|
|
|
|
{
|
|
|
|
errno = err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (int)value;
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
|
2017-11-14 18:10:52 +03:00
|
|
|
int IniFile_SetKeyValueString(wIniFile* ini, const char* section, const char* key,
|
|
|
|
const char* value)
|
2014-10-03 02:45:53 +04:00
|
|
|
{
|
|
|
|
wIniFileKey* pKey = NULL;
|
|
|
|
wIniFileSection* pSection = NULL;
|
|
|
|
pSection = IniFile_GetSection(ini, section);
|
|
|
|
|
|
|
|
if (!pSection)
|
|
|
|
pSection = IniFile_AddSection(ini, section);
|
|
|
|
|
|
|
|
if (!pSection)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pKey = IniFile_AddKey(ini, pSection, key, value);
|
|
|
|
|
|
|
|
if (!pKey)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int IniFile_SetKeyValueInt(wIniFile* ini, const char* section, const char* key, int value)
|
|
|
|
{
|
|
|
|
char strVal[128];
|
|
|
|
wIniFileKey* pKey = NULL;
|
|
|
|
wIniFileSection* pSection = NULL;
|
|
|
|
sprintf_s(strVal, sizeof(strVal), "%d", value);
|
|
|
|
pSection = IniFile_GetSection(ini, section);
|
|
|
|
|
|
|
|
if (!pSection)
|
|
|
|
pSection = IniFile_AddSection(ini, section);
|
|
|
|
|
|
|
|
if (!pSection)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pKey = IniFile_AddKey(ini, pSection, key, strVal);
|
|
|
|
|
|
|
|
if (!pKey)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* IniFile_WriteBuffer(wIniFile* ini)
|
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t i, j;
|
2017-11-14 18:10:52 +03:00
|
|
|
size_t offset;
|
|
|
|
size_t size;
|
2014-10-03 04:16:30 +04:00
|
|
|
char* buffer;
|
|
|
|
wIniFileKey* key;
|
|
|
|
wIniFileSection* section;
|
|
|
|
size = 0;
|
2014-10-03 02:45:53 +04:00
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
if (!ini)
|
|
|
|
return NULL;
|
|
|
|
|
2014-10-03 04:16:30 +04:00
|
|
|
for (i = 0; i < ini->nSections; i++)
|
|
|
|
{
|
|
|
|
section = ini->sections[i];
|
2017-11-14 18:10:52 +03:00
|
|
|
size += (strlen(section->name) + 3);
|
2014-10-03 04:16:30 +04:00
|
|
|
|
|
|
|
for (j = 0; j < section->nKeys; j++)
|
|
|
|
{
|
|
|
|
key = section->keys[j];
|
2017-11-14 18:10:52 +03:00
|
|
|
size += (strlen(key->name) + strlen(key->value) + 2);
|
2014-10-03 04:16:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size += 1;
|
|
|
|
buffer = malloc(size + 1);
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ini->nSections; i++)
|
|
|
|
{
|
|
|
|
section = ini->sections[i];
|
|
|
|
sprintf_s(&buffer[offset], size - offset, "[%s]\n", section->name);
|
2017-11-14 18:10:52 +03:00
|
|
|
offset += (strlen(section->name) + 3);
|
2014-10-03 04:16:30 +04:00
|
|
|
|
|
|
|
for (j = 0; j < section->nKeys; j++)
|
|
|
|
{
|
|
|
|
key = section->keys[j];
|
|
|
|
sprintf_s(&buffer[offset], size - offset, "%s=%s\n", key->name, key->value);
|
2017-11-14 18:10:52 +03:00
|
|
|
offset += (strlen(key->name) + strlen(key->value) + 2);
|
2014-10-03 04:16:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
sprintf_s(&buffer[offset], size - offset, "\n");
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[offset] = '\0';
|
|
|
|
return buffer;
|
2014-10-03 02:45:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int IniFile_WriteFile(wIniFile* ini, const char* filename)
|
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t length;
|
2014-10-03 04:16:30 +04:00
|
|
|
char* buffer;
|
2015-06-26 16:58:01 +03:00
|
|
|
int ret = 1;
|
2014-10-03 04:16:30 +04:00
|
|
|
buffer = IniFile_WriteBuffer(ini);
|
|
|
|
|
|
|
|
if (!buffer)
|
|
|
|
return -1;
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
length = strlen(buffer);
|
2014-10-03 04:16:30 +04:00
|
|
|
ini->readOnly = FALSE;
|
|
|
|
|
|
|
|
if (!filename)
|
|
|
|
filename = ini->filename;
|
|
|
|
|
2019-11-05 12:31:09 +03:00
|
|
|
if (!IniFile_Open_File(ini, filename))
|
2014-11-16 17:51:45 +03:00
|
|
|
{
|
|
|
|
free(buffer);
|
2014-10-03 04:16:30 +04:00
|
|
|
return -1;
|
2014-11-16 17:51:45 +03:00
|
|
|
}
|
2014-10-03 04:16:30 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (fwrite((void*)buffer, length, 1, ini->fp) != 1)
|
2015-06-26 16:58:01 +03:00
|
|
|
ret = -1;
|
2014-10-03 04:16:30 +04:00
|
|
|
|
|
|
|
fclose(ini->fp);
|
|
|
|
free(buffer);
|
2015-06-26 16:58:01 +03:00
|
|
|
return ret;
|
2014-10-03 02:45:53 +04:00
|
|
|
}
|
|
|
|
|
2019-10-29 13:54:55 +03:00
|
|
|
wIniFile* IniFile_New(void)
|
2014-03-25 02:20:34 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
wIniFile* ini = (wIniFile*)calloc(1, sizeof(wIniFile));
|
2014-03-25 02:20:34 +04:00
|
|
|
|
|
|
|
if (ini)
|
|
|
|
{
|
|
|
|
ini->nSections = 0;
|
|
|
|
ini->cSections = 64;
|
2019-11-06 17:24:51 +03:00
|
|
|
ini->sections = (wIniFileSection**)calloc(ini->cSections, sizeof(wIniFileSection*));
|
2016-02-10 18:51:27 +03:00
|
|
|
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!ini->sections)
|
|
|
|
{
|
|
|
|
free(ini);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ini;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IniFile_Free(wIniFile* ini)
|
|
|
|
{
|
2019-10-29 13:54:55 +03:00
|
|
|
size_t index;
|
2014-10-03 02:45:53 +04:00
|
|
|
|
|
|
|
if (!ini)
|
|
|
|
return;
|
2014-03-25 02:20:34 +04:00
|
|
|
|
2014-10-03 02:45:53 +04:00
|
|
|
free(ini->filename);
|
|
|
|
|
|
|
|
for (index = 0; index < ini->nSections; index++)
|
|
|
|
IniFile_Section_Free(ini->sections[index]);
|
|
|
|
|
|
|
|
free(ini->sections);
|
2020-05-15 15:49:52 +03:00
|
|
|
free(ini->buffer);
|
2014-10-03 02:45:53 +04:00
|
|
|
free(ini);
|
2014-03-25 02:20:34 +04:00
|
|
|
}
|