0

I am looking for an automatic method to parsing a configuration file which store many stuct variable member values.

For example, we have a struct variable named as following:

struct
{
    int a;
    int b;
}struct_example

Then we have a configuration file stored this struct variable values:

struct_example:
    a: 5
    b: 10

And a simple way to set struct values would be like:

struct_example example;
// supporse we have read string of "5" and "10" from configuration file and store
// it to string variable string_a, string_b;
example.a = atoi(string_a);
example.b = atoi(string_b);

However, if we have 100 struct members, we have to write 100 lines code to set value; If we have 1000 struct members, we have to write even 1000 lines code for configuration.

Do we more automatic method for such struct member configuration? Such as a function:

void struct_member_config(string member_name, string member_value, struct_example* struct_variable);
finley
  • 11
  • 4
    there are no C/C++ language. They're very different languages and structs are also very different in them. Choose only one – phuclv Jul 30 '22 at 03:22
  • Never used it, but 'Json' may fulfill your need. It's not as compact as your example, but I gather that this is its purpose... Check the Wikipedia page... – Fe2O3 Jul 30 '22 at 03:26
  • I wouldn't wish YAML on my worst enemy, but it might be an option. – Retired Ninja Jul 30 '22 at 03:32
  • So write something to parse your config files, and bobs your uncle. – Taekahn Jul 30 '22 at 03:35
  • No need for 100 or 1000 lines of code. For example, in C++ you would write an [`operator>>` overload](https://stackoverflow.com/q/4421706/4581301) and call it in a loop. In C you would replace the overload with a plain old function and do the same thing. – user4581301 Jul 30 '22 at 03:39
  • 2
    Neither C nor C++ has introspection capabilities powerful enough to do this automatically. You'd need to either write a program to parse your C++ header and generate the necessary C++ code to read in a config file and set values in the struct, or find a program that does that. Google's Protocol Buffers package comes to mind, although it's more oriented towards binary serialization that human-readable config files. It might have an ASCII text mode though, I don't know. – Jeremy Friesner Jul 30 '22 at 04:52
  • The files wouldn't be human readable/editable; though, have you considered writing and reading structures to a file in their native memory layout? For example, `write(fd,&struct_example,sizeof(struct_example))` and later `read(fd,&struct_example,sizeof(struct_example))`!? This could easily be extended for looped writes and reads. For portability, make sure you're considering platform-specific alignment differences. – ahi324 Jul 30 '22 at 07:04
  • Hi, it looks like I have no choice except work around. Well, maybe 'Json' would be the best choices. Regard struct variable layout, it would be not covinient for mannul settings. – finley Aug 01 '22 at 02:36

0 Answers0