Configuration file at startup


txturbo

Recommended Posts

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.

Link to comment
Share on other sites

  • 1 year later...

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

Link to comment
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)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.