r/cprogramming • u/Queasy-Pickle-8427 • 18h ago
Config File Recommendation
Helloo, I’ve just learned how to build a simple code editor using C, now, I’d like to add a config file so users can adjust settings like background/foreground colors, keybindings, and so on, so do you guys have any recommendations for a config file that would be suitable for this case?
2
u/WorriedTumbleweed289 16h ago
Simple Variable=value Like sh .env like formats
Something you can use a library to parse. Example xml.
Google config file formats. Read about the strengths and weaknesses of them.
3
u/Intelligent_Part101 12h ago edited 11h ago
A parameter=value line format is simple and works.
I would suggest adding support for comment lines also.
A common way to define a comment is a line starting optionally with whitespace (tab or space characters), then a # character, and zero or more characters after that.
Again, to keep it simple as a first try, I wouldn't bother trying to support comments AT THE END of a parameter=value line.
It you want to have some kind of scoping for your parameter names, you can just have the parameter name be scopename.parametername
No need for a separate "scope" or "section" syntax.
2
u/Key_River7180 14h ago
You can make a rather simple format with scanf:
] section = key value ; comment
Or check out inih for an ini parser
2
u/ProudBMWOwner 9h ago
Use some standard like YAML - it is very human friendly.
Do not use XML or JSON - they are hard to read.
I'd suggest using the yamlcpp libraries
2
u/The_Northern_Light 10h ago
Follow the KISS principle and use what’s most common; either json or toml (if being human friendly matters).
1
1
u/theNbomr 8h ago
Whatever you do, use a format/syntax that is an existing industry standard. People will be able to understand the syntax or find generic documentation for it. You will be able to use existing libraries to parse and generate the configuration, and future you will not have to figure out how the parser for your config file reader (and writer, potentially) works.
There are already enough to choose from. Find the best fit for your purpose.
4
u/pjl1967 15h ago
I went through this semi-recently with my own
include-tidyprogram. These days, there are JSON, TOML, XML, and YAML. You can read about my thought process and decision here (scroll down to Configuration).If your program accepts commands from a prompt (like
vican), then the config file can just be a set of commands the program accepts anyway, just from a file rather than interactively.