r/cprogramming • • 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?

12 Upvotes

17 comments sorted by

5

u/pjl1967 28d ago

I went through this semi-recently with my own include-tidy program. 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 vi can), then the config file can just be a set of commands the program accepts anyway, just from a file rather than interactively.

→ More replies (1)

4

u/The_Northern_Light 28d 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/skmruiz 28d ago

Any actually, maybe you can take at how editorconfig files look like. Some editors use JSON, others XML, others toml...

3

u/Key_River7180 28d ago

You can make a rather simple format with scanf:

] section = key value ; comment

Or check out inih for an ini parser

3

u/actguru 28d ago

You might try a standard format like JSON, Apple Plist XML etc. This way if you later allow the user to pick colors and keystrokes in the program, you will be able to have the program update the configuration file.

2

u/WorriedTumbleweed289 28d ago
  1. Simple Variable=value Like sh .env like formats
  2. Something you can use a library to parse. Example xml.
  3. Google config file formats. Read about the strengths and weaknesses of them.

3

u/Intelligent_Part101 28d ago edited 28d 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.

→ More replies (1)

4

u/[deleted] 28d ago

[removed] — view removed comment

2

u/Brisngr368 27d ago

Yes do not be like vscode

(I also vote YAML)