Page 1 of 1 1
Topic Options
#25318 - 2002-07-18 03:04 AM Help.... Problems with a function (which is using ReadProfileString)
t_pickering Offline
Fresh Scripter

Registered: 2001-11-23
Posts: 26
Hi Guys,

The function below reads in each section and it's data from a config file into an array. The array is then rename to the section name. This works ok when each section has data, but as soon as one of the sections doesn't have any data it screws up. The script reads in the data from the next section into an incorrectly named array.

Any idea on how to fix the problem? [Confused]

code:
FUNCTION ReadProfileSectionData()

DO
IF INSTR($line, "[") = 1
$pos_start = INSTR($line, "[") + 1
$pos_finish = INSTR($line, "]") - 2
$section_name = SUBSTR($line, $pos_start, $pos_finish)
$section_name = LCASE($section_name)
GLOBAL $section_data[0]
$i = 0
DO
$line = READLINE(1)
SELECT
CASE $line = ""

CASE INSTR($line, ";") = 1

CASE INSTR($line, "[") = 1

CASE 1
REDIM PRESERVE $section_data[$i]
$section_data[$i] = $line
$i = $i + 1
ENDSELECT
UNTIL INSTR($line, "[") = 1
$result = EXECUTE("$$$section_name = $section_data")
ELSE
$line = READLINE(1)
ENDIF
UNTIL $line = "[END]"

ENDFUNCTION

code:
config.ini

[TESTLAB]
subnet=192.168.99

[EXCLUDED_HOSTS]
hostname=12345
hostname=12345
hostname=12345
hostname=12345
hostname=12345
hostname=12345
hostname=12345
hostname=12345

[SHARES]
share0_groupname=domain admins
share0_driveletter=S
share0_path=\\eee\installs$
share1_groupname=staff
share1_driveletter=W
share1_path=\\eee\temp
share2_groupname=
share2_driveletter=X
share2_path=\\eee\data
[END]

(MCA: complete subject)

[ 18 July 2002, 08:43: Message edited by: MCA ]

Top
#25319 - 2002-07-18 04:11 AM Re: Help.... Problems with a function (which is using ReadProfileString)
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Question: Why don't you use READPROFILESTRING(), this function is designed to read .INI files. It would make your code much easier.
code:
$excluded=readprofilestring('config.ini','[excluded_hosts]','')
$excluded=split($excluded,chr(10))

will put the list of excluded hosts into an array.

[ 18 July 2002, 04:13: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#25320 - 2002-07-18 04:32 AM Re: Help.... Problems with a function (which is using ReadProfileString)
t_pickering Offline
Fresh Scripter

Registered: 2001-11-23
Posts: 26
Hi,

That does't work. It only reads in the data before the equals signs.

Tim.

[ 18 July 2002, 04:42: Message edited by: t_pickering ]

Top
#25321 - 2002-07-18 05:54 AM Re: Help.... Problems with a function (which is using ReadProfileString)
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Tim,

Why doesn't READPROFILESTRING work?

Think of it this way...
Read in an INI File, by section, by key and what the key's data means.

So, if we take from the documentation the following:

READPROFILESTRING
Action
Retrieves a string from an initialization file.

Syntax
READPROFILESTRING ("file name", "section", "key")

Parameters

File name
A string that names the initialization file. If this parameter does not include a full path, Windows searches for the file in the Windows directory.

Section
A string that specifies the section containing the key name. If this parameter is empty, READPROFILESTRING returns all section names in the file.

Key
A string containing the key name whose associated string is to be retrieved. If this parameter is empty, all key names in the section specified by section are returned.

Remarks
This function is provided for compatibility with 16-bit Windows – based applications. Win32 – based applications store initialization information in the registry.

Returns
0 Function returns a string representing the value of the specified key

Example
$dev = ReadProfileString("win.ini", "Windows", "Device")

HTH,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#25322 - 2002-07-18 08:42 AM Re: Help.... Problems with a function (which is using ReadProfileString)
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
Dear,

Te concept of such files is that the keys are the same. In your example we read many
times hostname=12345
Two suggestion:
- read the unchanged file with Open/RedaLine/Close
- change hostname=12345 to
f.e.
hostname1=12345
hostname2=12345
hostname3=12345
for make it possible to use the ReadProfileString function.
greetings.
_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
#25323 - 2002-07-18 04:32 PM Re: Help.... Problems with a function (which is using ReadProfileString)
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Tim: Please read the documentation for READPROFILESTRING as Kyder suggested. READPROFILESTRING can do either of three things depending on parameters provided:
1) Read a list of sections
2) Read a list of keys in a specific section
3) Read the value of a single key in a specific section.

The example I gave you reads all keys in a spoecific section and reformats it into an array. Then you can cycle through the array and read the values.
_________________________
There are two types of vessels, submarines and targets.

Top
#25324 - 2002-07-21 11:38 AM Re: Help.... Problems with a function (which is using ReadProfileString)
MCA Offline
KiX Supporter
*****

Registered: 2000-04-28
Posts: 5152
Loc: Netherlands, EU
solved?
_________________________
email scripting@wanadoo.nl homepage scripting@wanadoo.nl | Links | Summary of Site Site KiXforms FAQ kixtart.org library collection mirror MCA | FAQ & UDF help file UDF kixtart.org library collection mirror MCA | mirror USA | mirror europe UDF scriptlogic library collection UDFs | mirror MCA

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.057 seconds in which 0.028 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org