Reputation: 3333
I have the following Config.cfg
[Power]
Power8=8
Temp=5=1001
Hum=7=1002
Link=8=1003
Vol=9=1004
[Power]
Power10=10
Temp=5=1012
Hum=7=1013
Link=8=1014
Vol=9=1015
and with the usage of glib I want to read the values of each Power. I want something like 'if Power8=8 then return temp, hum, Link, Vol'else the same for Power10=10
I wrote this function
int read_config()
{
GKeyFile *keyfile;
GKeyFileFlags flags;
GError *error = NULL;
gsize length;
gchar *temperatura, *humedad, *link, *voltage;
// Create a new GKeyFile object and a bitwise list of flags.
keyfile = g_key_file_new ();
flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
// Load the GKeyFile from keyfile.conf or return.
if (!g_key_file_load_from_file (keyfile, "/home/pi/Desktop/Config.cfg", flags, &error))
{
g_error (error->message);
return -1;
}
printf("[Power]\n");
if(g_key_file_get_integer(keyfile,"Power","Power8",NULL) == 8)
{
temperatura=g_key_file_get_string(keyfile,"Power","Temp",NULL);
humedad=g_key_file_get_string(keyfile,"Power","Hum",NULL);
link=g_key_file_get_string(keyfile,"Power","Link",NULL);
voltage=g_key_file_get_string(keyfile,"Power","Volt",NULL);
printf("Power8:%d\n",g_key_file_get_integer(keyfile,"Power","Power8",NULL));
printf("Temp:%s\n",temperatura);
printf("Hum:%s\n",humedad);
printf("Link:%s\n",link);
printf("Vol:%s\n",voltage);
}
else
{
temperatura=g_key_file_get_string(keyfile,"Power","Temp",NULL);
humedad=g_key_file_get_string(keyfile,"Power","Hum",NULL);
link=g_key_file_get_string(keyfile,"Power","Link",NULL);
voltage=g_key_file_get_string(keyfile,"Power","Volt",NULL);
printf("Power10:%d\n",g_key_file_get_integer(keyfile,"Power","Power10",NULL));
printf("Temp:%s\n",temperatura);
printf("Hum:%s\n",humedad);
printf("Link:%s\n",link);
printf("Vol:%s\n",voltage);
}
}
But it returns me the values of Power10 and also Vol=(Null)
[Power]
Power8=8
Temp=5=1012
Hum=7=1013
Link=8=1014
Vol=(Null)
What is the problem here?
Upvotes: 0
Views: 4436
Reputation: 400029
The documentation clearly states:
Note that in contrast to the Desktop Entry Specification, groups in key files may contain the same key multiple times; the last entry wins. Key files may also contain multiple groups with the same name; they are merged together.
Thus, your file is not a valid glib keyfile since it tries to define the same key multiple times, expecting each group to be an "object". That's not how it works, unfortunately.
The Vol
issue seems to simply be the wrong key, you're calling with "Volt"
but the file has Vol
.
Upvotes: 5