txturbo

Configuration file at startup

Recommended Posts

I have an application that is used on more than one system. Each system has slightly different hardware but the user interface is the same. 

How can I use a configuration file to select the proper hardware depending on the system at startup? 

 

 

Share this post


Link to post
Share on other sites

The way I do it is using a class that contains all the settings, then I instantiate that class once into a global variable, called, say "gSys".  Then I can access the settings using gSys.mySetting.  The class has a load and save method that saves all the local variables to a json formatted text file.

class CSystem
   local debug = 
   local string rootDirectory = "c:\daqfactory"
   local mySetting = 5
   local AnotherSetting = 6
   local string AStringSetting = "Some string"
   
   function saveSettings()
      try
         private string out = toJson()
         private handle = file.Open(rootDirectory + "\Settings.json",,1,,1)
         file.Write(handle, out)
         file.Close(handle)
      catch()
         ? strLastError
      endcatch      
   endfunction
   
   function loadSettings()
      try
         private string in
         private handle = file.Open(rootDirectory + "\Settings.json",1,,,1)
         in = file.Read(handle)
         file.Close(handle)
         fromJson(in)
      catch("F0002")
         ? "Settings not found, creating new file"
         saveSettings()
      endcatch
      catch()
         ? strLastError
      endcatch      
   endfunction      
endclass

I then instantiate the class in my start up sequence, and call loadSettings on it:

global gSys = new(CSystem)
gSys.loadSettings()

And whenever I change a setting from within the program, I call gSys.saveSettings(). 

The nice part is that if you add a new setting to your application and then run the application on a system that has an older settings file, the system will still load those settings, and then fill in any new settings with the defaults.

Note that I store the root folder as part of the settings.  This is a little backwards because until the file is loaded, it doesn't know the root folder, but instead will use the default.

Share this post


Link to post
Share on other sites

I have been trying to get this to work but I get an error. I changed the attributes on file open to replace the blanks from the example above with "0" but that did not seem to help. 

I tried to set up global variables to match the locals from the class as well. 

I created a test file. 

I set up a sequence named createClasses with this:

class CSystem
   local debug = 
   local string rootDirectory = "c:\daqfactory"
   local mySetting = 5
   local AnotherSetting = 6
   local string AStringSetting = "Some string"
   
   function saveSettings()
      try
         private string out = toJson()
         private handle = file.Open(rootDirectory + "\Settings.json",,1,,1)
         file.Write(handle, out)
         file.Close(handle)
      catch()
         ? strLastError
      endcatch      
   endfunction
   
   function loadSettings()
      try
         private string in
         private handle = file.Open(rootDirectory + "\Settings.json",1,,,1)
         in = file.Read(handle)
         file.Close(handle)
         fromJson(in)
      catch("F0002")
         ? "Settings not found, creating new file"
         saveSettings()
      endcatch
      catch()
         ? strLastError
      endcatch      
   endfunction      
endclass
 

Then I run a start up sequence with this:

createClasses()
global gSys = new(CSystem)
gSys.loadSettings()

 

I get this error:
C1000 Channel or function not found: startup Line 3 - Uncaught error in sequence startup

 

 

sysloadtest.ctl

Share this post


Link to post
Share on other sites

The problem is the second line in your class declaration:

local debug =

That is invalid.  You either need to give it an initial value or don't include the = sign.

Share this post


Link to post
Share on other sites

Thanks,  it's working now. 

1. I got rid of the local debug = 

2. I changed the file open from this 

private handle = file.Open(rootDirectory + "\Settings.json",1,,,1)

private handle = file.Open(rootDirectory + "\Settings.json",,,1,,1)

to this

private handle = file.Open(rootDirectory + "\Settings.json",1,0,0,1)

private handle = file.Open(rootDirectory + "\Settings.json",0,1,0,1)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now