Differences between revisions 1 and 15 (spanning 14 versions)
Revision 1 as of 2007-03-10 22:29:01
Size: 2714
Editor: KaiJaeger
Comment:
Revision 15 as of 2008-05-28 16:57:58
Size: 6223
Editor: KaiJaeger
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
[[TableOfContents]]
Line 4: Line 6:
Line 6: Line 9:
They have a disadvantage as well: they are slow.

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.

This class allows you to use a kind of APL-Syntax in your INI files.
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.
Line 14: Line 20:
=== Character Values ===
Line 18: Line 26:
results in a string holding the path, but an entry like: results in a string holding the path.

===
Numeric Values ===

A
n entry like:
Line 22: Line 34:
results in a two-element-vector "!FormSize" holding two Integers. results in a two-element-vector "!FormSize" holding two integers.

=== References ===
Line 28: Line 42:
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.

== Example ==

=== Creating an Instance ===
Line 32: Line 50:
{{{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":
{{{myIni←⎕New #.IniClass (,⊂'C:/Appl/Example.ini')}}}

=== Accessing Data with the "Get" method ===

you can get all information you are interested in by calling the method "Get". Note that names are '''not''' case sensitive.

Given this file "Example.ini":
Line 40: Line 62:
LogLevels=1 2 3 * from 1 to 9 LogLevels=1 2 3 ; from 1 to 9
Line 46: Line 68:
LogFileFolder='{Home}/Logs/' LogFileFolder='{Home}Logs/'
Line 51: Line 73:
 * list everything
 * list a particular section
 * list a particular value in a particular section

== Examples ==

{{{
      MyIni.Get ⍬ ⍬
GE
NERAL MaxNoOfErrors 20
GENERAL Form
Size 800 1200
GENERAL LogfileFlag 1
GENERAL LogLevels 1 2 3
DIR Home C:/mainfolder/
DIR AppFolder C:/mainfolder/appls/
DIR D
ocsFolder C:/mainfolder/docs/
DIR LogFileFolder C:/mainfolder//
Logs/
      MyIni.Get'General' ⍬
MaxNoOfErrors 20
Form
Size 800 1200
LogfileFlag 1
LogLevels 1 2 3
      MyIni.Get'General' 'FormSize'
 * get everything
 * get only a particular section
 * get a particular value in a particular section

==== Examples with "Get" ====

{{{
      myIni.Get ⍬ ⍬
 GENERAL
          
MAXNOOFERRORS 20
          FOR
MSIZE 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
FOR
MSIZE 800 1200
LOGFILEFLAG 1
LOGLEVELS 1 2 3
      myIni.Get'General' 'FormSize'
Line 74: Line 98:
      MyIni.Get'General' 'Unknown'

   ¯1 MyIni.Get'General' 'Unknown'
      ¯1 myIni.Get'General' 'Unknown' with default
Line 78: Line 100:
      ¯1 MyIni.Get'Dir' 'LogFileFolder'
C:/mainfolder//Logs/
}}}
Kai Jaeger
      myIni.Get'General' 'Unknown' ⍝ without default
Value Error: "Unknown"
myDoc.Get'General' 'Unknown'
}}}

=== Indexing ===

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
}}}

=== Assigning ===

            myIni[⊂'GeneRAL:FormSize']←⊂12 23

=== The "Put" method ===

            (12 23) myIni.Put 'GeneRAL:FormSize'

=== The "Save" method ===

You can also change a particular value but the changed value will persist only if you execute the "Save" method at some point:

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

==== A Warning ====

However, an INI file is by definition not a kind of database and should '''not''' be used to save data by the application itself. The "Save" method '''might''' be useful to initialise an INI file.

=== Check Keys before indexing ===

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
}}}

== Creating an INI file ==

To create a new INI file, don't specify a filename:

{{{myIni←⎕New #.IniClass }}}

=== Adding a Section ===

The only way to add a new section is to use the {{{AddSection}}}-method:

{{{
      myIni←⎕New #.IniClass
      myIni.AddSection'NewSection'
}}}

=== Adding Values ===

Adding new values can be done the same way you would cange a value: either use the "Put"-method or simply assign a value.

== Other Methods ==

=== Delete ===

Examples with supported syntax:

{{{
      myIni←⎕New #.IniClass
      myIni.Delete 'MySection:key1'
      myIni.Delete 'MySection' 'key2'
      myIni.Delete 'MySection:' ⍝ this will delete the entire section
}}}

The "Delete" method returns a shy boolean which gets 1 in case something was to be deleted.

=== Exist ===

Let's assume that "myIni" is an instance of the IniFile class, and that there is a section "Config" which contains exactly one key: "Size":

      1 <-> myIni.Exist 'Config:Size'
      0 <-> myIni.Exist 'Config' 'unknow'
      1 <-> myIni.Exist 'Config:'

=== DeleteDefault ===

Setting the "Default" property might be appropriate if you need a default value for undefined keys. However, as soon as the "Default" property is set, one can get rid of it only be calling the "DeleteDefault" method. The method returns a shy Boolean which gets 1 only if there '''was''' a default.


For a full version history: [:IniFiles/History: History]

== Version Information ==

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

Goto the [:IniFiles/DownloadPage: DownloadPage]
Line 83: Line 213:
CategoryDyalog CategoryScriptsAndClasses CategoryOpenSourceApl CategoryAplAplDyalog

Overview

TableOfContents

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

Character Values

An entry like:

HomeFolder='C:/Windows/Appl/'

results in a string holding the path.

Numeric Values

An entry like:

FormSize=300 400

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

References

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.

Example

Creating an Instance

After creating an instance from the class:

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

Accessing Data with the "Get" method

you can get all information you are interested in by calling the method "Get". Note that names are not case sensitive.

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 with "Get"

      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'

Indexing

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

Assigning

  • myIni[⊂'GeneRAL:FormSize']←⊂12 23

The "Put" method

  • (12 23) myIni.Put 'GeneRAL:FormSize'

The "Save" method

You can also change a particular value but the changed value will persist only if you execute the "Save" method at some point:

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

A Warning

However, an INI file is by definition not a kind of database and should not be used to save data by the application itself. The "Save" method might be useful to initialise an INI file.

Check Keys before indexing

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

Creating an INI file

To create a new INI file, don't specify a filename:

myIni←⎕New #.IniClass 

Adding a Section

The only way to add a new section is to use the AddSection-method:

      myIni←⎕New #.IniClass
      myIni.AddSection'NewSection'

Adding Values

Adding new values can be done the same way you would cange a value: either use the "Put"-method or simply assign a value.

Other Methods

Delete

Examples with supported syntax:

      myIni←⎕New #.IniClass
      myIni.Delete 'MySection:key1'
      myIni.Delete 'MySection' 'key2'
      myIni.Delete 'MySection:' ⍝ this will delete the entire section

The "Delete" method returns a shy boolean which gets 1 in case something was to be deleted.

Exist

Let's assume that "myIni" is an instance of the IniFile class, and that there is a section "Config" which contains exactly one key: "Size":

  • 1 <-> myIni.Exist 'Config:Size' 0 <-> myIni.Exist 'Config' 'unknow' 1 <-> myIni.Exist 'Config:'

DeleteDefault

Setting the "Default" property might be appropriate if you need a default value for undefined keys. However, as soon as the "Default" property is set, one can get rid of it only be calling the "DeleteDefault" method. The method returns a shy Boolean which gets 1 only if there was a default.

For a full version history: [:IniFiles/History: History]

Version Information

Original author:

KaiJaeger

Responsible:

KaiJaeger

Email:

kai@aplteam.com

Current state:

1.3.1

Goto the [:IniFiles/DownloadPage: DownloadPage]


CategoryOpenSourceApl CategoryAplAplDyalog

IniFiles (last edited 2018-03-03 11:42:11 by KaiJaeger)