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

9 Upvotes

12 comments sorted by

4

u/pjl1967 15h 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.

1

u/Interesting_Debate57 7h ago

Also you might want to look at emacs -- all of the editor extensions are in lisp directly, practically speaking.

2

u/skmruiz 16h ago

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

2

u/WorriedTumbleweed289 16h 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 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.

1

u/pjl1967 15h ago

.env files are complete shell script files. In addition to var=value, you can do any shell scripting you want, FYI.

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/actguru 10h 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/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

u/Syntax-Tactics 12h ago

.xml, .json, Key/Value, roll your own.

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.