How to read multiline values in inifile
Asked Answered
A

1

9

I am trying to use a multi line value as specified as list in ini file, I was able to read sections but not able read to multi line values from a given key.

I have tried to using list manner it completely failed.

# Ini file
[UNITS]
COMPARE_UNITS = [list
                    [11871000 118700]
                    [1198100 1198100]
                ]
[VARS]
OLD_REL = 4.3
NEW_REL = 4.5

I have tried to using string based format also i failed but i could able to read sections and first line of a given key value.

# Ini file
[UNITS]
COMPARE_UNITS = "
                    11871000 118700
                    1198100 1198100
                "
[VARS]
OLD_REL = 4.3
NEW_REL = 4.5

When i tried to get key values it returns only first line

% set fileOrg [ini::open "sample.ini" r]
ini11
% foreach sec [ini::sections $fileOrg] {puts [::ini::get $fileOrg $sec]}
NEW_REL 4.5 OLD_REL 4.3
COMPARE_UNITS {1198100 1198100}
%

I have two question

  1. How to read a multi value form a given key using package inifile
  2. Can i specify a list values in a key?

-Malli

Armington answered 22/3, 2015 at 7:58 Comment(0)
C
8

The INI file format does not support multiline values. The specification is lines with either a section name in square brackets to start a new section or lines with a keyname followed by an equals sign followed by a value terminated in a line end. Or a comment line.

The tcllib parser splits the file into lines and if the line is not a comment, not a section start and does not contain an equals sign it is discarded.

If you want to include multiple values in an INI file value then you should use some application-specific field separator or multiple keys eg:

[Test.Field]
multi-field = first|second|third
[Test.MultiKey]
multi.1 = first
multi.2 = second
multi.3 = third

The first version could be used as simply as:

set ini [ini::open test.ini r]
set fields [split [ini::value $ini Test.Field multi-field] "|"]
Caitiff answered 22/3, 2015 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.