winpr: fix ini loader on Windows
This commit is contained in:
parent
2f568aad79
commit
0547a31832
@ -45,6 +45,7 @@ struct _wIniFile
|
||||
char* line;
|
||||
char* nextLine;
|
||||
int lineLength;
|
||||
char* tokctx;
|
||||
char* buffer;
|
||||
char* filename;
|
||||
BOOL readOnly;
|
||||
|
@ -29,6 +29,30 @@
|
||||
|
||||
#include <winpr/ini.h>
|
||||
|
||||
BOOL IniFile_Load_NextLine(wIniFile* ini, char* str)
|
||||
{
|
||||
int length = 0;
|
||||
|
||||
ini->nextLine = strtok_s(str, "\n", &ini->tokctx);
|
||||
|
||||
if (ini->nextLine)
|
||||
length = (int) strlen(ini->nextLine);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int IniFile_Load_String(wIniFile* ini, const char* iniString)
|
||||
{
|
||||
long int fileSize;
|
||||
@ -52,7 +76,7 @@ int IniFile_Load_String(wIniFile* ini, const char* iniString)
|
||||
ini->buffer[fileSize] = '\n';
|
||||
ini->buffer[fileSize + 1] = '\0';
|
||||
|
||||
ini->nextLine = strtok(ini->buffer, "\n");
|
||||
IniFile_Load_NextLine(ini, ini->buffer);
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -60,9 +84,9 @@ int IniFile_Load_String(wIniFile* ini, const char* iniString)
|
||||
int IniFile_Open_File(wIniFile* ini, const char* filename)
|
||||
{
|
||||
if (ini->readOnly)
|
||||
ini->fp = fopen(filename, "r");
|
||||
ini->fp = fopen(filename, "rb");
|
||||
else
|
||||
ini->fp = fopen(filename, "w+");
|
||||
ini->fp = fopen(filename, "w+b");
|
||||
|
||||
if (!ini->fp)
|
||||
return -1;
|
||||
@ -79,9 +103,12 @@ int IniFile_Load_File(wIniFile* ini, const char* filename)
|
||||
|
||||
if (fseek(ini->fp, 0, SEEK_END) < 0)
|
||||
goto out_file;
|
||||
|
||||
fileSize = ftell(ini->fp);
|
||||
|
||||
if (fileSize < 0)
|
||||
goto out_file;
|
||||
|
||||
if (fseek(ini->fp, 0, SEEK_SET) < 0)
|
||||
goto out_file;
|
||||
|
||||
@ -106,7 +133,7 @@ int IniFile_Load_File(wIniFile* ini, const char* filename)
|
||||
ini->buffer[fileSize] = '\n';
|
||||
ini->buffer[fileSize + 1] = '\0';
|
||||
|
||||
ini->nextLine = strtok(ini->buffer, "\n");
|
||||
IniFile_Load_NextLine(ini, ini->buffer);
|
||||
|
||||
return 1;
|
||||
|
||||
@ -145,9 +172,10 @@ char* IniFile_Load_GetNextLine(wIniFile* ini)
|
||||
return NULL;
|
||||
|
||||
ini->line = ini->nextLine;
|
||||
ini->nextLine = strtok(NULL, "\n");
|
||||
ini->lineLength = (int) strlen(ini->line);
|
||||
|
||||
IniFile_Load_NextLine(ini, NULL);
|
||||
|
||||
return ini->line;
|
||||
}
|
||||
|
||||
@ -159,6 +187,7 @@ wIniFileKey* IniFile_Key_New(const char* name, const char* value)
|
||||
{
|
||||
key->name = _strdup(name);
|
||||
key->value = _strdup(value);
|
||||
|
||||
if (!key->name || !key->value)
|
||||
{
|
||||
free(key->name);
|
||||
@ -189,6 +218,7 @@ wIniFileSection* IniFile_Section_New(const char* name)
|
||||
return NULL;
|
||||
|
||||
section->name = _strdup(name);
|
||||
|
||||
if (!section->name)
|
||||
{
|
||||
free(section);
|
||||
@ -198,6 +228,7 @@ wIniFileSection* IniFile_Section_New(const char* name)
|
||||
section->nKeys = 0;
|
||||
section->cKeys = 64;
|
||||
section->keys = (wIniFileKey**) malloc(sizeof(wIniFileKey*) * section->cKeys);
|
||||
|
||||
if (!section->keys)
|
||||
{
|
||||
free(section->name);
|
||||
@ -258,8 +289,10 @@ wIniFileSection* IniFile_AddSection(wIniFile* ini, const char* name)
|
||||
|
||||
new_size = ini->cSections * 2;
|
||||
new_sect = (wIniFileSection**) realloc(ini->sections, sizeof(wIniFileSection*) * new_size);
|
||||
|
||||
if (!new_sect)
|
||||
return NULL;
|
||||
|
||||
ini->cSections = new_size;
|
||||
ini->sections = new_sect;
|
||||
}
|
||||
@ -307,15 +340,19 @@ wIniFileKey* IniFile_AddKey(wIniFile* ini, wIniFileSection* section, const char*
|
||||
|
||||
new_size = section->cKeys * 2;
|
||||
new_key = (wIniFileKey**) realloc(section->keys, sizeof(wIniFileKey*) * new_size);
|
||||
|
||||
if (!new_key)
|
||||
return NULL;
|
||||
|
||||
section->cKeys = new_size;
|
||||
section->keys = new_key;
|
||||
}
|
||||
|
||||
key = IniFile_Key_New(name, value);
|
||||
|
||||
if (!key)
|
||||
return NULL;
|
||||
|
||||
section->keys[section->nKeys] = key;
|
||||
section->nKeys++;
|
||||
}
|
||||
@ -323,6 +360,7 @@ wIniFileKey* IniFile_AddKey(wIniFile* ini, wIniFileSection* section, const char*
|
||||
{
|
||||
free(key->value);
|
||||
key->value = _strdup(value);
|
||||
|
||||
if (!key->value)
|
||||
return NULL;
|
||||
}
|
||||
@ -432,6 +470,7 @@ int IniFile_ReadFile(wIniFile* ini, const char* filename)
|
||||
|
||||
free(ini->filename);
|
||||
ini->filename = _strdup(filename);
|
||||
|
||||
if (!ini->filename)
|
||||
return -1;
|
||||
|
||||
@ -464,6 +503,7 @@ char** IniFile_GetSectionNames(wIniFile* ini, int* count)
|
||||
}
|
||||
|
||||
sectionNames = (char**) malloc(length);
|
||||
|
||||
if (!sectionNames)
|
||||
return NULL;
|
||||
|
||||
@ -718,6 +758,7 @@ wIniFile* IniFile_New()
|
||||
ini->nSections = 0;
|
||||
ini->cSections = 64;
|
||||
ini->sections = (wIniFileSection**) calloc(ini->cSections, sizeof(wIniFileSection*));
|
||||
|
||||
if (!ini->sections)
|
||||
{
|
||||
free(ini);
|
||||
|
Loading…
Reference in New Issue
Block a user