-1

What I need to do is to "fine tune" some constant values that should be compiled along with the rest of the program, but I want to verify the results at every change without having to modify a value and recompile the whole program each time. So I was thinking at a sort of plain text configuration file to reload every time I change a number in it, and re-initialize part of the program to take action on the new values. It's something that I do often, but this time what I want to do is to have this configuration file under the form of a valid inclusion file with the following syntax:

const MyStructure[] = 
{
  { 1, 0.5f, 0.2f, 0.77f, [other values...] },
  { 3, 0.4f, 0.1f, 0.15f, [other values...] },
  [other rows...]
};

If I were using an interpreted language such as Perl, I'd have used the eval() function, which if course is not possible with C++. And while I have read other questions about the possiblity to have an eval() function in C++, what I want is not to evaluate and run this code, just to parse it and put the values in the variables they belong to.

I would probably use a Regular Expression to parse the C syntax above, but again, RegExp still is not something worth using in C++, so can you suggest an alternative method?

It's probably worth saying that I need to parse this file only during the development phase. I will #include it when the program is ready for the release.

Mark Miles
  • 706
  • 8
  • 20
  • Just load the data from a regular file into non-`const` variables. You don't gain anything from "This variable is `const` but sometimes it changes". – nwp Sep 15 '17 at 13:36
  • Why not to just have a data file? – KonstantinL Sep 15 '17 at 13:37
  • I know that english is not my mother toungue but I don't think my post wasn't clear enough when I said that I only need to change this file during the development phase. Once the program is done, that data must stay constant and never change any more. There's no point in having a data file for data that has not to be changed any more. My main goal was to speed up the development process avoiding to recompile the whole program every time I need to change something in that data structure. – Mark Miles Sep 15 '17 at 14:21
  • Although regexes may well not be the ideal tool for this problem, I think your assertion about C++ support is highly questionable. – rici Sep 15 '17 at 14:24
  • Then include that data file into resource. – KonstantinL Sep 15 '17 at 14:24
  • Have you considered standard patch tools? see https://stackoverflow.com/q/1451694/2785528 – 2785528 Sep 17 '17 at 02:34
  • By the time being, I've solved by writing my own parser using regular expressions, since I need to do this only on MSVC. I can read the float numbers from the tables and put them into the variables, ignoring the rest of the syntax which, however, is valid as a static c array. That, along with a couple of compiler switches, solved my problem. – Mark Miles Sep 17 '17 at 11:35

1 Answers1

0

Writing your own parser is probably more work than is appropriate for this use case.

A simpler solution would be to just compile the file containing the variables separately, as a shared object or DLL, which can be loaded dynamically at run time. (Precise details depend on your OS.) You could, if desired, invoke the compiler during program initialisation as well.

If you don't want to deal with the complication of finding the symbols and copying them into static variables, you could also compile the bulk of your program as a shared object, with only a small shim as the main executable. That shim would:

  • If necessary, invoke the compiler to create the data shared object

  • Dynamically load the data shared object

  • Dynamically load the program shared object, and

  • Invoke the main program using it's main entry point (possibly using a different name).

To produce the production version, it is only necessary to compile program and data together, and use it directly without the shim.

Variations on this theme are possible, depending on precise needs.

rici
  • 234,347
  • 28
  • 237
  • 341