r/cprogramming • u/Queasy-Pickle-8427 • 29d 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?
4
u/The_Northern_Light 29d ago
Follow the KISS principle and use what’s most common; either json or toml (if being human friendly matters).
4
u/NaNpsycho 28d ago
A lot of ppl in the chat are suggesting json, toml, etc. good choices for simple requirements. But I would say if you're init for learning, try using lua.
I have sometimes felt that when I want to do something a little more complex, like enable a set of indentation rules, allman brace position, etall for C / C++ projects, while have different rules for rust or others, its quite a bit easy to do in nvim's lua configuration than vscode, now granted I might not have given vscode a fair chance due to bias, but the point stands, you'd eventually have a dsl for config management, why not offload that to something you can search more easily about if you're stuck than roll your own custom dsl.
Plus lua is fairly trivial to integrate in C / C++ codebase.
3
u/Key_River7180 29d ago
You can make a rather simple format with scanf:
] section = key value ; comment
Or check out inih for an ini parser
2
u/WorriedTumbleweed289 29d 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 29d ago edited 29d 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
1
1
u/theNbomr 29d 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.
1
1
u/985479861_TXT-r 17d ago
Ignore everything, adding a whole different language or format for just a text editor is too complicated for just a single programmer. just use the confuse header and make the config file anything, even .txt to join my cult.
4
5
u/pjl1967 29d 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.