Hi George,

On 30/01/2020 10:07 am, George Georgalis wrote:
Hi Mark,

On Wed, Jan 29, 2020 at 2:07 PM MJ <mafsys1...@gmail.com> wrote:
On 30/01/2020 5:19 am, George Georgalis wrote:
Hi, I would like to introduce configuration files to a software
project I am supporting with build infrastructure. The C programs
depend on quite a few libraries from different sources. As a result of
development evolution, we have hardcoded paths in the source which
would be better served as parameters read from configuration files, so
the location of dependencies can be adjusted without modifying the
code. We need this functionality for runtime as well as compile time.
Can you not just change all the hardcoded paths to defines and then pass those 
defines to the programs via make?
...
Am I over-simplifying the issue?
That sounds like a great solution, compile time defines solves the
immediate problem of getting it to work on another platform, accept
that I'm not proficient enough in C to just code it out. How do you
pass make env to programs at compile time? Would you provide an
example?

How are you at C? Can you use pointers, manipulate "strings"? I'm not averse to 
helping you, but I can't teach you C :-)

To your issue and using MACROS/defines , for example, suppose this code snippet:

some_function(void) {

config_path="/usr/local/etc/prog.txt"

...

...

}


Change it to:

some_function(void) {

#if defined (MY_CONFIG_PATH)

strcpy(config_path,MY_CONFIG_PATH);

#else

#error "compile with -DMY_CONFIG_PATH="/usr/local/etc/prog.txt"

#endif
...

...

}


Then compile (using make), with cc -DMY_CONFIG_PATH="/usr/local/etc/prog.txt"


Of course, if you've got a large suite of programs each with a different 
CONFIG_PATH, then the command line might get overrun with data! That's a 
decision you will have to confront based on your knowledge of the configuration 
path lengths hardcoded into the programs.


What about runtime paths and parameters? I guess a config file still
needs parsing from C, for that? I know a lot of compiled programs do
If they are runtime, then this is different altogether.

Suppose your configuration file is made up of some passed arguments like so:

int main(int argc, char **argv)

{
/* config_path depends on argument 1 for some of the path */
sprintf(config_path,"/usr/local/etc/%s/prog.txt", argv[1]);

}


Then this cannot be circumvented with -D, obviously. Then again, how are you 
doing it now, if they're runtime? Are they runtime but still static?

That is, even though the information is added at runtime, it never varies each 
time the program runs?


this, but in the source I've looked at, I'm not seeing exactly
how/where. :-\ ...this would be the first thing I would make templates
for, but I'm not seeing any templates either....

If I was building a simple configuration file and parsing it, I would use 
something like this:

key=value

In the tradition of unix, I would skip all lines that are blank and begin with #

I'm "old skool" with this format. It's simple, it's effective.

You can then use something like strtok() to parse it, or roll your own. In the 
latter case, I would just use basic fopen(), fgets(), parse the line by pulling 
it apart using pointers, then placing the result into a KEY and DATA. You would 
then compare the KEY(s) to what you're looking for and when a match is 
obtained, use that DATA value.

Do you want some more specific code rather than this vague notion?

cheers

Mark

Reply via email to