View previous topic :: View next topic |
Author |
Message |
guyuming Apprentice
Joined: 19 Nov 2020 Posts: 242
|
Posted: Mon Sep 02, 2024 8:04 am Post subject: how to read .conf file keyvalue pair without groupname in c? |
|
|
i am writing application with gtk in c language.
I need to parse key value pair text files
according to https://docs.gtk.org/glib/method.KeyFile.get_keys.html , i have to provide a group name(or so-called section name).
but i want to deal with simple configuration files which may not have group name.
if you run locate *.conf , you can find many .conf files without group name, although many do contains group name.
so, what's the best practice to handle key value pair text file in c which may or may not have group name? |
|
Back to top |
|
|
lars_the_bear Guru
Joined: 05 Jun 2024 Posts: 517
|
Posted: Mon Sep 02, 2024 8:34 am Post subject: |
|
|
Load the entire configuration file into memory, adding an extra six bytes for a dummy " [foo]\n " at the beginning. Then use KeyFile::load_from_data() to load this entire block of memory into a KeyFile, starting from the initial "[foo]". Then use as documented, with "foo" as your group name.
Or just copy the entire file to a temporary location, adding the [foo] header as you copy. Then use load_from_file() as normal, again with 'foo' as the group name.
So far as I know, GLib 'ini files' have to have a group header, even if it's just a dummy one. Of course, you aren't constrained to use 'foo'
Do be aware that the GLib KeyFile functions are designed to handle data in a particular format. That is, there are particular ways to include commments, escape non-printing characters, etc. You can't necessarily assume that these functions will handle an arbitrary configuration file generated by non-GLib software (although usually they work fine).
I wrote my own functions for handling key-value files. It's not hugely difficult, and now I can use it wherever I want.
BR, Lars.
PS. There must surely be open-source libraries around for handling this kind of file? They're pretty common, after all. |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 22618
|
Posted: Mon Sep 02, 2024 1:36 pm Post subject: |
|
|
By itself, .conf is only a hint that some program considers this some type of configuration file. You cannot generally assume anything beyond that. The program may or may not have section headers like INI files do. It may or may not require quoting of values. It may or may not support comments. Comments are traditionally introduced by #, but some unusual programs may use some other character for comments. Comments may or may not require that the first non-whitespace character on the line be the comment character. Comments may or may not require that the comment character be the very first character on the line, disallowing an leading whitespace.
In my opinion, you need to narrow the problem space. What type of files are you prepared to support? What files will you consider ill-formed, and refuse to load? |
|
Back to top |
|
|
lars_the_bear Guru
Joined: 05 Jun 2024 Posts: 517
|
Posted: Mon Sep 02, 2024 1:49 pm Post subject: |
|
|
@Hu
The OP says
Quote: | I need to parse key value pair text files |
The GLib KeyFile class handles a specific kind of key/value pair text file, so I presume the OP has that kind of thing in mind, since he mentions KeyFile. I presume that the advice to `locate *.conf` was just to find examples.
But, to be fair, I'm just guessing.
In fact, the KeyFile class is pretty particular about the format it handles. Still, I've found that, if you're developing for GLib anyway, it's often easier to massage a different kind of key/value file into a format that KeyFile understands, than to write a parser from scratch.
BR, Lars. |
|
Back to top |
|
|
Genone Retired Dev
Joined: 14 Mar 2003 Posts: 9608 Location: beyond the rim
|
Posted: Mon Sep 02, 2024 4:04 pm Post subject: |
|
|
The main issue is that there is no real standard for ini-style files (unlike xml or json). The closest thing to a standard is probably the way Microsoft has implemented it within regedit for importing/exporting registry keys and values, and that specific implementation does require sections for semantic reasons. So most implementations will at least support sections, though not necessarily enforce it.
Not sure why glib enforces sections as it is pretty trivial to handle both cases.
The easiest option to work around is to just add a section to your config file and be done with it, unless this would cause other issues. Or if you really just need basic key=value assignments with no fancy stuff (arrays, multi-line values, variables, ...) you can also write your own parser, it isn't exactly rocket science. I wouldn't recommend this normally, but with ini-style files in particular the implementation details can be really annoying to deal with when they clash with your understanding. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|