Skip to content

Commit

Permalink
libwinpr-utils: improve .ini file parser
Browse files Browse the repository at this point in the history
  • Loading branch information
awakecoding committed Mar 25, 2014
1 parent 331209f commit 43031d6
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ include/freerdp/version.h
*.cproject
*.settings

nbproject/
compile_commands.json

# .rdp files
*.rdp
*.RDP
Expand Down
13 changes: 10 additions & 3 deletions winpr/include/winpr/ini.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ typedef struct _wIniFile wIniFile;
extern "C" {
#endif

int IniFile_Parse(wIniFile* ini, const char* filename);
WINPR_API int IniFile_Parse(wIniFile* ini, const char* filename);
WINPR_API int IniFile_ParseString(wIniFile* ini, const char* iniString);

wIniFile* IniFile_New();
void IniFile_Free(wIniFile* ini);
WINPR_API char** IniFile_GetSectionNames(wIniFile* ini, int* count);
WINPR_API char** IniFile_GetSectionKeyNames(wIniFile* ini, const char* section, int* count);

WINPR_API char* IniFile_GetKeyValueString(wIniFile* ini, const char* section, const char* key);
WINPR_API UINT32 IniFile_GetKeyValueInt(wIniFile* ini, const char* section, const char* key);

WINPR_API wIniFile* IniFile_New();
WINPR_API void IniFile_Free(wIniFile* ini);

#ifdef __cplusplus
}
Expand Down
261 changes: 234 additions & 27 deletions winpr/libwinpr/utils/ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,49 @@

#include <winpr/ini.h>

void IniFile_Load_Start(wIniFile* ini)
int IniFile_Load_String(wIniFile* ini, const char* iniString)
{
long int fileSize;

ini->line = NULL;
ini->nextLine = NULL;
ini->buffer = NULL;

fileSize = strlen(iniString);

if (fileSize < 1)
return -1;

ini->buffer = (char*) malloc(fileSize + 2);
CopyMemory(ini->buffer, iniString, fileSize);

ini->buffer[fileSize] = '\n';
ini->buffer[fileSize + 1] = '\0';

ini->nextLine = strtok(ini->buffer, "\n");

return 1;
}

int IniFile_Load_File(wIniFile* ini, const char* filename)
{
long int fileSize;

if (ini->readOnly)
{
ini->fp = fopen(filename, "r");
}
else
{
ini->fp = fopen(filename, "r+");

if (!ini->fp)
ini->fp = fopen(filename, "w+");
}

if (!ini->fp)
return -1;

fseek(ini->fp, 0, SEEK_END);
fileSize = ftell(ini->fp);
fseek(ini->fp, 0, SEEK_SET);
Expand All @@ -42,21 +81,23 @@ void IniFile_Load_Start(wIniFile* ini)
ini->buffer = NULL;

if (fileSize < 1)
return;
return -1;

ini->buffer = (char*) malloc(fileSize + 2);

if (fread(ini->buffer, fileSize, 1, ini->fp) != 1)
{
free(ini->buffer);
ini->buffer = NULL;
return;
return -1;
}

ini->buffer[fileSize] = '\n';
ini->buffer[fileSize + 1] = '\0';

ini->nextLine = strtok(ini->buffer, "\n");

return 1;
}

void IniFile_Load_Finish(wIniFile* ini)
Expand Down Expand Up @@ -150,6 +191,9 @@ int IniFile_AddSection(wIniFile* ini, const char* name)

int IniFile_AddKey(wIniFile* ini, wIniFileSection* section, const char* name, const char* value)
{
if (!section)
return -1;

if ((section->nKeys + 1) >= (section->cKeys))
{
section->cKeys *= 2;
Expand All @@ -172,8 +216,6 @@ int IniFile_Load(wIniFile* ini)
wIniFileKey* key = NULL;
wIniFileSection* section = NULL;

IniFile_Load_Start(ini);

while (IniFile_Load_HasNextLine(ini))
{
line = IniFile_Load_GetNextLine(ini);
Expand All @@ -194,8 +236,6 @@ int IniFile_Load(wIniFile* ini)

IniFile_AddSection(ini, beg);
section = ini->sections[ini->nSections - 1];

printf("[%s]\n", section->name);
}
else
{
Expand All @@ -213,13 +253,22 @@ int IniFile_Load(wIniFile* ini)

while (*beg && ((*beg == ' ') || (*beg == '\t')))
beg++;


if (*beg == '"')
beg++;

end = &line[ini->lineLength];

while ((end > beg) && ((end[-1] == ' ') || (end[-1] == '\t')))
end--;

if (end[-1] == '"')
end[-1] = '\0';

value = beg;

IniFile_AddKey(ini, section, name, value);
key = section->keys[section->nKeys - 1];

printf("%s = %s\n", key->name, key->value);
}
}

Expand All @@ -228,36 +277,194 @@ int IniFile_Load(wIniFile* ini)
return 1;
}

int IniFile_Open(wIniFile* ini)
int IniFile_ParseString(wIniFile* ini, const char* iniString)
{
if (ini->readOnly)
int status;

ini->readOnly = TRUE;
ini->filename = NULL;

status = IniFile_Load_String(ini, iniString);

if (status < 0)
return status;

status = IniFile_Load(ini);

return status;
}

int IniFile_Parse(wIniFile* ini, const char* filename)
{
int status;

ini->readOnly = TRUE;
ini->filename = _strdup(filename);

status = IniFile_Load_File(ini, ini->filename);

if (status < 0)
return status;

status = IniFile_Load(ini);

return status;
}

wIniFileSection* IniFile_GetSection(wIniFile* ini, const char* name)
{
int index;
wIniFileSection* section = NULL;

for (index = 0; index < ini->nSections; index++)
{
ini->fp = fopen(ini->filename, "r");
if (_stricmp(name, ini->sections[index]->name) == 0)
{
section = ini->sections[index];
break;
}
}
else
{
ini->fp = fopen(ini->filename, "r+");

return section;
}

if (!ini->fp)
ini->fp = fopen(ini->filename, "w+");
wIniFileKey* IniFile_GetKey(wIniFile* ini, wIniFileSection* section, const char* name)
{
int index;
wIniFileKey* key = NULL;

for (index = 0; index < section->nKeys; index++)
{
if (_stricmp(name, section->keys[index]->name) == 0)
{
key = section->keys[index];
break;
}
}

return key;
}

if (!ini->fp)
return -1;
char** IniFile_GetSectionNames(wIniFile* ini, int* count)
{
char* p;
int index;
int length;
int nameLength;
char** sectionNames;
wIniFileSection* section = NULL;

length = (sizeof(char*) * ini->nSections) + sizeof(char);

for (index = 0; index < ini->nSections; index++)
{
section = ini->sections[index];
nameLength = strlen(section->name);
length += (nameLength + 1);
}

sectionNames = (char**) malloc(length);
p = (char*) &((BYTE*) sectionNames)[sizeof(char*) * ini->nSections];

for (index = 0; index < ini->nSections; index++)
{
sectionNames[index] = p;
section = ini->sections[index];
nameLength = strlen(section->name);
CopyMemory(p, section->name, nameLength + 1);
p += (nameLength + 1);
}

IniFile_Load(ini);
*p = '\0';

return 1;
*count = ini->nSections;

return sectionNames;
}

int IniFile_Parse(wIniFile* ini, const char* filename)
char** IniFile_GetSectionKeyNames(wIniFile* ini, const char* section, int* count)
{
ini->readOnly = TRUE;
ini->filename = _strdup(filename);
char* p;
int index;
int length;
int nameLength;
char** keyNames;
wIniFileKey* pKey = NULL;
wIniFileSection* pSection = NULL;

pSection = IniFile_GetSection(ini, section);

if (!pSection)
return NULL;

length = (sizeof(char*) * pSection->nKeys) + sizeof(char);

for (index = 0; index < pSection->nKeys; index++)
{
pKey = pSection->keys[index];
nameLength = strlen(pKey->name);
length += (nameLength + 1);
}

keyNames = (char**) malloc(length);
p = (char*) &((BYTE*) keyNames)[sizeof(char*) * pSection->nKeys];

for (index = 0; index < pSection->nKeys; index++)
{
keyNames[index] = p;
pKey = pSection->keys[index];
nameLength = strlen(pKey->name);
CopyMemory(p, pKey->name, nameLength + 1);
p += (nameLength + 1);
}

IniFile_Open(ini);
*p = '\0';

return 1;
*count = pSection->nKeys;

return keyNames;
}

char* IniFile_GetKeyValueString(wIniFile* ini, const char* section, const char* key)
{
char* value = NULL;
wIniFileKey* pKey = NULL;
wIniFileSection* pSection = NULL;

pSection = IniFile_GetSection(ini, section);

if (!pSection)
return NULL;

pKey = IniFile_GetKey(ini, pSection, key);

if (!pKey)
return NULL;

value = pKey->value;

return value;
}

UINT32 IniFile_GetKeyValueInt(wIniFile* ini, const char* section, const char* key)
{
UINT32 value = 0;
wIniFileKey* pKey = NULL;
wIniFileSection* pSection = NULL;

pSection = IniFile_GetSection(ini, section);

if (!pSection)
return 0;

pKey = IniFile_GetKey(ini, pSection, key);

if (!pKey)
return 0;

value = atoi(pKey->value);

return value;
}

wIniFile* IniFile_New()
Expand Down
Loading

0 comments on commit 43031d6

Please sign in to comment.