Differences between revisions 1 and 7 (spanning 6 versions)
Revision 1 as of 2007-03-10 22:29:01
Size: 2714
Editor: KaiJaeger
Comment:
Revision 7 as of 2007-12-16 22:46:33
Size: 3961
Editor: KaiJaeger
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
They have a disadvantage as well: they are slow. They have disadvantages as well:
Line 8: Line 8:
If you are not interested in the Windows registry and command line parameters, and if you are not changing your INI files while the application is running, then the "!IniFile" class introduced in this article might attract your attention.  * They are slow
 * They return everything as a string
Line 10: Line 11:
This class allows you to use a kind of APL-Syntax in your INI files. If you are not interested in the Windows registry and command line parameters, and if nobody else is changing your INI files while your application is running, then the "!IniFile" class introduced in this article might attract your attention.

This class allows you to use a kind of APL-Syntax in your INI files. Values not enclosed in quotes will be converted to numbers, everything else gets a string.
Line 22: Line 25:
results in a two-element-vector "!FormSize" holding two Integers. results in a two-element-vector "!FormSize" holding two integers.
Line 28: Line 31:
is treated in a special way: the name between the curlies is treated as the name of an alreday defined value in the same section. It is then replaced by the value of that entry. is treated in a special way: the name between the curlies is taken as the name of an already defined value. It is then replaced by the value of that entry.
Line 32: Line 35:
{{{MyIni←⎕New #.IniClass 'C:/Appl/Example.ini'}}} {{{MyIni←⎕New #.IniClass (,⊂'C:/Appl/Example.ini')}}}
Line 40: Line 43:
LogLevels=1 2 3 * from 1 to 9 LogLevels=1 2 3 ; from 1 to 9
Line 46: Line 49:
LogFileFolder='{Home}/Logs/' LogFileFolder='{Home}Logs/'
Line 51: Line 54:
 * list everything
 * list a particular section
 * list a particular value in a particular section
 * get everything
 * get only a particular section
 * get a particular value in a particular section
Line 58: Line 61:
      MyIni.Get ⍬ ⍬
GENERAL MaxNoOfErrors 20
GENERAL FormSize 800 1200
GENERAL LogfileFlag 1
GENERAL LogLevels 1 2 3
DIR Home C:/mainfolder/
DIR AppFolder C:/mainfolder/appls/
DIR DocsFolder C:/mainfolder/docs/
DIR LogFileFolder C:/mainfolder//Logs/
      MyIni.Get ⍬ ⍬   GENERAL                                                MAXNOOFERRORS 20
          FORMSIZE 800 1200
          LOGFILEFLAG 1
          LOGLEVELS 1 2 3
 DIR                                                HOME C:/mainfolder/
          APPFOLDER C:/mainfolder/appls/
          DOCSFOLDER C:/mainfolder/docs/
          LOGFILEFOLDER C:/mainfolder/Logs/
Line 68: Line 73:
MaxNoOfErrors 20
FormSize 800 1200
LogfileFlag 1
LogLevels 1 2 3
MAXNOOFERRORS 20
FORMSIZE 800 1200
LOGFILEFLAG 1
LOGLEVELS 1 2 3
Line 74: Line 79:
      MyIni.Get'General' 'Unknown'       ¯1 MyIni.Get'General' 'Unknown' ⍝ with default
¯1
      MyIni.Get'General' 'Unknown' ⍝ without default
Value Error: "Unknown"
myDoc.Get'General' 'Unknown'
}}}
Line 76: Line 86:
      ¯1 MyIni.Get'General' 'Unknown'
¯1
      ¯1 MyIni.Get'Dir' 'LogFileFolder'
C:/mainfolder//Logs/
Since version 1.1, the class provides a default property. That means you can access values by indexing.

Examples (with the same INI file listed above):

{{{
      MyIni[⊂'GeneRAL:']
20 800 1200 1 1 2 3
            ⊃MyIni[⊂'GeneRAL:FormSize']
800 1200
Line 81: Line 96:
Kai Jaeger
You can also change a particular value. However, that value will persist only if you execute the "Save" method at some point:

{{{
      MyIni[⊂'GeneRAL:FormSize']←⊂'¯1 1000
      MyIni.Save
}}}

Note that using indexing, there is no default. That means that specifying an unknown value leads to an error. There are two ways to escape this problem:

{{{
      MyIni.Exist 'General:Unknown'
0
      MyIni.Default← ¯1 ¯1
      MyIni[⊂'General:Unknown']
¯1 ¯1
      MyIni[⊂'General:Unknown']←200
      MyIni[⊂'General:Unknown']
200
}}}

||Original author:||KaiJaeger||
||Responsible:||KaiJaeger||
||Email:||kai@aplteam.com||
||Current state:||1.1||

Goto the IniFileDownloadPage
Line 83: Line 125:
CategoryDyalog CategoryScriptsAndClasses CategoryOpenSourceApl CategoryAplAplDyalog

Overview

INI files are still useful to provide settings to an application. Vista is not going to change this. The Windows API methods provided to read a particular value have an advantage: they follow a clearly defined search path, and following that path they take not only the INI file into account, they also check the Windows registry and the command line parameter. Furthermore, they deliver always up-to-date values.

They have disadvantages as well:

  • They are slow
  • They return everything as a string

If you are not interested in the Windows registry and command line parameters, and if nobody else is changing your INI files while your application is running, then the "IniFile" class introduced in this article might attract your attention.

This class allows you to use a kind of APL-Syntax in your INI files. Values not enclosed in quotes will be converted to numbers, everything else gets a string.

Details

An entry like:

HomeFolder='C:/Windows/Appl/'

results in a string holding the path, but an entry like:

FormSize=300 400

results in a two-element-vector "FormSize" holding two integers.

Furthermore, an entry like:

LogFolder='{"HomeFolder}Logsfiles/'

is treated in a special way: the name between the curlies is taken as the name of an already defined value. It is then replaced by the value of that entry.

After creating an instance from the class:

MyIni←⎕New #.IniClass (,⊂'C:/Appl/Example.ini')

you can get all information you are interested in by calling the only method "Get". Given this file "Example.ini":

[GENERAL]
MaxNoOfErrors=20
FormSize=800 1200
LogfileFlag=1
LogLevels=1 2 3 ; from 1 to 9

[DIR]
Home='C:/mainfolder/'
AppFolder='{Home}appls/'
DocsFolder='{Home}docs/'
LogFileFolder='{Home}Logs/'

You can get any level of information you are interested in:

  • get everything
  • get only a particular section
  • get a particular value in a particular section

Examples

      MyIni.Get ⍬ ⍬ 
 GENERAL                                      
          MAXNOOFERRORS                    20 
          FORMSIZE                   800 1200 
          LOGFILEFLAG                       1 
          LOGLEVELS                     1 2 3 
 DIR                                          
          HOME                 C:/mainfolder/ 
          APPFOLDER      C:/mainfolder/appls/ 
          DOCSFOLDER      C:/mainfolder/docs/ 
          LOGFILEFOLDER   C:/mainfolder/Logs/ 
      MyIni.Get'General' ⍬
MAXNOOFERRORS        20 
FORMSIZE       800 1200 
LOGFILEFLAG           1 
LOGLEVELS         1 2 3 
      MyIni.Get'General' 'FormSize'
800 1200
      ¯1 MyIni.Get'General' 'Unknown' ⍝ with default
¯1
      MyIni.Get'General' 'Unknown' ⍝ without default
Value Error: "Unknown"
myDoc.Get'General' 'Unknown'

Since version 1.1, the class provides a default property. That means you can access values by indexing.

Examples (with the same INI file listed above):

      MyIni[⊂'GeneRAL:']
20  800 1200  1  1 2 3
            ⊃MyIni[⊂'GeneRAL:FormSize']
800 1200

You can also change a particular value. However, that value will persist only if you execute the "Save" method at some point:

      MyIni[⊂'GeneRAL:FormSize']←⊂'¯1 1000
      MyIni.Save

Note that using indexing, there is no default. That means that specifying an unknown value leads to an error. There are two ways to escape this problem:

      MyIni.Exist 'General:Unknown'
0
      MyIni.Default← ¯1 ¯1
      MyIni[⊂'General:Unknown']
¯1 ¯1
      MyIni[⊂'General:Unknown']←200
      MyIni[⊂'General:Unknown']
200

Original author:

KaiJaeger

Responsible:

KaiJaeger

Email:

kai@aplteam.com

Current state:

1.1

Goto the IniFileDownloadPage


CategoryOpenSourceApl CategoryAplAplDyalog

SevenZip (last edited 2018-03-03 11:38:43 by KaiJaeger)