Created
August 19, 2016 17:57
-
-
Save varjagg/e8a3111ffbba5f1ac8f760390c490697 to your computer and use it in GitHub Desktop.
Reading a key=value configuration file without globall section using glib own GKeyFile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <glib.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int main(void) | |
{ | |
GKeyFile *config = g_key_file_new(); | |
gchar header[] = "[general]\n\n"; | |
gchar * buffer = 0; | |
long length; | |
FILE * f = fopen ("test.cfg", "rb"); | |
if (f) | |
{ | |
fseek(f, 0, SEEK_END); | |
length = ftell(f); | |
fseek(f, 0, SEEK_SET); | |
buffer = malloc(length + strlen(header)); | |
memcpy(buffer, header, strlen(header)); | |
if (buffer) | |
{ | |
fread(buffer + strlen(header), 1, length, f); | |
} | |
fclose(f); | |
} | |
if(buffer) { | |
g_key_file_load_from_data(config, buffer, length + strlen(header) - 1, G_KEY_FILE_NONE, NULL); | |
printf("val=%s\n", g_key_file_get_string(config, "general", "foo", NULL)); | |
} | |
g_key_file_free(config); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment