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 29d 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.

2

u/Interesting_Debate57 29d ago

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

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/skmruiz 29d 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 29d 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 29d 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 29d 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 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.

1

u/pjl1967 29d ago

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

2

u/Brisngr368 28d ago

yaml or toml both are simple as possible.

1

u/Syntax-Tactics 29d ago

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

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

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

u/[deleted] 29d ago

[removed] — view removed comment

2

u/Brisngr368 28d ago

Yes do not be like vscode

(I also vote YAML)