My project is developed in C under linux platform. I am also using libxml2 . The executable consists of multiple modules distributed in multiple c files.
I have a configuration file ( Xml ) with 20 configurable parameters.
Let us say I am reading the configuration file parameters in Module M1. But I need them in other big modules M2 and M3.
Is there any efficient way to make the configurable parameters available to all the modules without using global variables to read parameters in module M1.
Specifically is there any other method / design to avoid using globally declared variables to read configuration parameters.
First of all, if the values are read once and never changed after that, then storing them as global variables is not a big crime.
The best (general purpose) way to handle this, is to read the XML, convert the values to their native types (numbers as xml strings into integers), and stored collectively in a struct. There will be only one instanciation of the struct but don't stress over making it a "singleton". That one struct can be globally accessible, or a pointer to it can be passed to the different modules.
In this case, I would indeed use global variables, your variables do indeed seem to be used globally by all modules. However to mitigate the global name-space pollution you will want to place these variables into a struct.
Create a struct called ConfigGlobals or some other descriptive name and simply add members to it for each configuration option. This adds clarity as you know which variables are config at a glance and means you are only throwing one name into the global scope.
EDIT: Note I see this answer is similar to James Curran's answer, I am sorry, the duplication is completely coincidental and I did not intentionally copy you, I had began writing this before you posted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With