Differences between revisions 8 and 57 (spanning 49 versions)
Revision 8 as of 2008-04-16 19:27:47
Size: 4206
Editor: KaiJaeger
Comment:
Revision 57 as of 2011-08-31 20:48:00
Size: 7482
Editor: KaiJaeger
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was copied from IniFiles
= IniFiles =
{{{IniFiles}}} is part of the CategoryAplTree project.

<<TableOfContents>>
Line 2: Line 8:

INI files are still useful to provide settings to an application. Vista is not going to change this. 
INI files are still useful to provide settings to an application. Neither Vista nor Windows 7 are going to change this.
Line 6: Line 12:
They have disadvantages as well:  They have disadvantages as well:
Line 11: Line 17:
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. 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.
Line 16: Line 22:
=== Character Values ===
Line 21: Line 27:
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 27: Line 36:
=== References ===
Line 33: Line 43:
Note that of course "!HomeFolder" must be specified upfront. Prior to version 1.5, this must be specified within the same section. As a result the same variable needed to be specified more than once if the same path needed to be available in more than one section.

Since version 1.5 this restriction was lifted by the introduction of "local" variables, see there.

=== Local Variables ===
Local values are those specified above the first section. They have only one purpose: to be used as references in several sections.

There are some restrictions:

 * They can only be used during the instanciation
 * They must not be nested
 * Although it is possible to specify a numeric value this does not make any sense since numeric values cannot be used as references

=== Import another INI files ===

Above the first section definition one can also import another INI file. This can be used to make an INI file path/drive independent, for example in order to support an application on a notebook as well as a server.

This is how an INI file that is going to be imported elsewhere could look like:

{{{
; INI file to be imported
[DRIVES] ; This is a comment
ArchiveDrive='D:\'
DocDrive='M:\'
}}}

Note that the section is ignored in the file that will import this INI file. Assuming that the name of this INI file is "local.ini":

{{{
!Import local.ini
[PATHS]
ArchivePath='{ArchiveDrive}this\that\'
DocPath='{DocDrive}there\'
}}}

Using this technique all values that depend on the current environment can be specified in `local.ini` while all the other entries can be specified in the second INI file. However, this is suitable for small "local" INI files. With version 2.0.0 the constructor accepted more than one filename: use this to effectively merge INI files together.


== Example ==
=== Creating an Instance ===
Line 35: Line 85:
{{{MyIni←⎕New #.IniClass (,⊂'C:/Appl/Example.ini')}}} {{{myIni←⎕New #.IniClass (,⊂'C:/Appl/Example.ini')}}}

=== Accessing Data with the "Get" method ===
Line 40: Line 91:
Line 53: Line 105:

You can get any level of information you are interested in: 
You can get any level of information you are interested in:
Line 57: Line 108:
 * 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'
 * get all keys and values of a particular section
 * get a particular value from 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/
      m
yIni.Get'General' ⍬
MAXNOOFERRORS 20
FORMSIZE 800 1200
LOGFILEFLAG 1
LOGLEVELS 1 2 3
      m
yIni.Get'General' 'FormSize'
Line 81: Line 131:
      ¯1 MyIni.Get'General' 'Unknown' ⍝ with default       ¯1 myIni.Get'General' 'Unknown' ⍝ with default
Line 83: Line 133:
      MyIni.Get'General' 'Unknown' ⍝ without default       myIni.Get'General' 'Unknown' ⍝ without default
Line 87: Line 137:
=== Indexing ===
Line 93: Line 143:
      MyIni[⊂'GeneRAL:']       myIni[⊂'GeneRAL:']
Line 95: Line 145:
            ⊃MyIni[⊂'GeneRAL:FormSize']             ⊃myIni[⊂'GeneRAL:FormSize']
Line 98: Line 148:
=== Assigning ===
 .`myIni[⊂'GeneRAL:FormSize']←⊂12 23`

=== The "Put" method ===
 .` (12 23) myIni.Put 'GeneRAL:FormSize'`

=== Nested Entries ===
Since version 1.4 nested values are supported. Imagine an INI file that sets an "AcceptIP" value to a number of IP addresses to be accepted when a client tries to connect to your application. That's how that might look like:

{{{
AcceptID='192.168.68.1,192.168.68.100,195.64.2.2,127.0.0.1,85.86.87.88,156.147.123.1'
}}}
and maybe even much longer. Horrible, and prone to error when that needs to be changed. By initializing the value as an empty vector and then using the ",=" syntax one can overcome the problem:

{{{
AcceptID=''
AcceptID,='192.168.68.1'
AcceptID,='192.168.68.100'
AcceptID,='195.64.2.2'
AcceptID,='127.0.0.1'
AcceptID,='85.86.87.88'
AcceptID,='156.147.123.1'
}}}
This results in a nexted vector of length 6 were each item holds a single IP addres. This works with numbers as well:

{{{
vector=''
vector,=1 2 3
vector,=200 300
}}}
leads to:

{{{
(1 2 3) (200 300)
}}}
=== The "Save" method ===
Line 102: Line 187:
      MyIni[⊂'GeneRAL:FormSize']←⊂'¯1 1000
      MyIni.Save
}}}

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.

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
      myIni[⊂'GeneRAL:FormSize']←⊂'¯1 1000
      myIni.Save
}}}
==== A Warning ====
An INI file is by definition not a kind of database and should '''not''' be used as such.

=== Multiple INI files ===
Note that since version 2.0.0 one can specify more than one filename in the `⎕NEW` statement. This effectively merges the INI files together. Note that....

 1. in case of name clashes the last file wins.
 1. you cannot execute the `Save` method when more than one INI file was specified.
 1. the `IniFilename` property always returns a simple string. In case of more than one INI file the filenames are separated by ";".

== Project Page ==

For bug reports, future enhancements and a full version history see IniFiles/ProjectPage

== Version Information ==
||Original author: ||KaiJaeger ||
||Responsible: ||KaiJaeger ||
||Email: || kai@aplteam.com ||

<<Include(APLTreeDownloads)>>
Line 129: Line 212:
CategoryOpenSourceApl CategoryAplAplDyalog CategoryAplTree

IniFiles

IniFiles is part of the CategoryAplTree project.

Overview

INI files are still useful to provide settings to an application. Neither Vista nor Windows 7 are 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.

Note that of course "HomeFolder" must be specified upfront. Prior to version 1.5, this must be specified within the same section. As a result the same variable needed to be specified more than once if the same path needed to be available in more than one section.

Since version 1.5 this restriction was lifted by the introduction of "local" variables, see there.

Local Variables

Local values are those specified above the first section. They have only one purpose: to be used as references in several sections.

There are some restrictions:

  • They can only be used during the instanciation
  • They must not be nested
  • Although it is possible to specify a numeric value this does not make any sense since numeric values cannot be used as references

Import another INI files

Above the first section definition one can also import another INI file. This can be used to make an INI file path/drive independent, for example in order to support an application on a notebook as well as a server.

This is how an INI file that is going to be imported elsewhere could look like:

; INI file to be imported
[DRIVES]  ; This is a comment
ArchiveDrive='D:\'
DocDrive='M:\'

Note that the section is ignored in the file that will import this INI file. Assuming that the name of this INI file is "local.ini":

!Import local.ini
[PATHS]
ArchivePath='{ArchiveDrive}this\that\'
DocPath='{DocDrive}there\'

Using this technique all values that depend on the current environment can be specified in local.ini while all the other entries can be specified in the second INI file. However, this is suitable for small "local" INI files. With version 2.0.0 the constructor accepted more than one filename: use this to effectively merge INI files together.

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 all keys and values of a particular section
  • get a particular value from 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'

Nested Entries

Since version 1.4 nested values are supported. Imagine an INI file that sets an "AcceptIP" value to a number of IP addresses to be accepted when a client tries to connect to your application. That's how that might look like:

AcceptID='192.168.68.1,192.168.68.100,195.64.2.2,127.0.0.1,85.86.87.88,156.147.123.1'

and maybe even much longer. Horrible, and prone to error when that needs to be changed. By initializing the value as an empty vector and then using the ",=" syntax one can overcome the problem:

AcceptID=''
AcceptID,='192.168.68.1'
AcceptID,='192.168.68.100'
AcceptID,='195.64.2.2'
AcceptID,='127.0.0.1'
AcceptID,='85.86.87.88'
AcceptID,='156.147.123.1'

This results in a nexted vector of length 6 were each item holds a single IP addres. This works with numbers as well:

vector=''
vector,=1 2 3
vector,=200 300

leads to:

(1 2 3) (200 300)

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

An INI file is by definition not a kind of database and should not be used as such.

Multiple INI files

Note that since version 2.0.0 one can specify more than one filename in the ⎕NEW statement. This effectively merges the INI files together. Note that....

  1. in case of name clashes the last file wins.
  2. you cannot execute the Save method when more than one INI file was specified.

  3. the IniFilename property always returns a simple string. In case of more than one INI file the filenames are separated by ";".

Project Page

For bug reports, future enhancements and a full version history see IniFiles/ProjectPage

Version Information

Original author:

KaiJaeger

Responsible:

KaiJaeger

Email:

kai@aplteam.com


CategoryAplTree

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