thunderchero
(Fresh Scripter)
2007-09-10 05:20 AM
help need variable from ini

Help again,

I need to set a variable from an ini file. right now I want to use $rom as variable, ini file name is "stbof.ini". also the path variable I am using to ini file is "$install+" And ini file looks like this;

CDPATH=F:\
HDPATH=C:\botf\
CURRENTGAME=0
LANGUAGE=ENGLISH
VICTORY=KILLALL
INSTALL=MAXIMUM
3D=DIRECT3D
TACTICAL=DETAILED
SOUNDON=ON

I need Variable to be "F:\" "or what ever is in ini"

if any one can get me the right script it would be great.

thunderchero


Les
(KiX Master)
2007-09-10 05:23 AM
Re: help need variable from ini

You don't show the section header. What is it?

thunderchero
(Fresh Scripter)
2007-09-10 05:30 AM
Re: help need variable from ini

that is all that is in the ini

thunderchero


Les
(KiX Master)
2007-09-10 05:51 AM
Re: help need variable from ini

Then IMHO it is not a properly formatted INI. I don't think ReadProfileString() will be able to read it. Is it of your making? Can you not add a header?

thunderchero
(Fresh Scripter)
2007-09-10 05:54 AM
Re: help need variable from ini

It is not of my making it was made in 1999

thunderchero


thunderchero
(Fresh Scripter)
2007-09-10 06:02 AM
Re: help need variable from ini

Ok If I make ini look like this;

[settings]
CDPATH=F:\
HDPATH=C:\botf\
CURRENTGAME=0
LANGUAGE=ENGLISH
VICTORY=KILLALL
INSTALL=MAXIMUM
3D=DIRECT3D
TACTICAL=DETAILED
SOUNDON=ON


the next time game starts it removes it so would that work?

thunderchero


Les
(KiX Master)
2007-09-10 06:03 AM
Re: help need variable from ini

YOu could add a section header to the INI file and then use ReadProfileString().
 Code:
Break On

$path='C:\'
$file='test.ini'
$rom=ReadProfileString($path+$file,'header','CDPATH')

$rom ?


thunderchero
(Fresh Scripter)
2007-09-10 06:06 AM
Re: help need variable from ini

ReadProfileString() was what I was tring to use But never got it to work now I know why. but please still help me.

thunderchero


Les
(KiX Master)
2007-09-10 06:17 AM
Re: help need variable from ini

Alternately, you can use the ReadFile() UDF and Split().
 Code:
Break On
Function ReadFile($file)
	Dim $lf, $f, $_, $t
	$lf=chr(10)
	$f=freefilehandle
	$_=open($f,$file)
		if @error   exit @error   endif
		do $t=$t+$lf+readline($f) until @error
		$_=close($f)
	$ReadFile=split(substr($t,2),$lf)
EndFunction
$rom=''
$path='C:\'
$file='test.ini'
$romarray=Readfile($path+$file)
For $Line = 0 to UBound($romarray)
 If Split($romarray[$Line],'=')[0]='CDPATH'
  $rom = Split($romarray[$Line],'=')[1]
  $Line = UBound($romarray)
 EndIf
Next
$rom ?


Glenn BarnasAdministrator
(KiX Supporter)
2007-09-10 01:23 PM
Re: help need variable from ini

Hmm- it sounds like the "real" utility that uses this file removes the header. I guess you want to be able to change values in this before you use the real program? If so, try this:
 Code:
Move 'stbof.ini' 'stbof.bak'    ; rename original file
$ = RedirectOutput('stbof.ini') ; create new file
'[Settings]' ?                  ; add header
Display 'stbof.bak'             ; add original content to new file
$ = RedirectOutput('')          ; Close redirection
Del 'stbof.bak'                 ; clean up

If you don't need to modify the original content, but simply read it, simplify the above to:
 Code:
$ = RedirectOutput('MYstbof.ini') ; create new file
'[Settings]' ?                    ; add header
Display 'stbof.ini'               ; add original content to new file
$ = RedirectOutput('')            ; Close redirection

; read whatever you need from the new, properly formatted INI file
$Install = ReadProfileString('MYstbof.ini', 'SETTINGS', 'CDPATH')

Del 'MYstbof.ini'                 ; clean up


Glenn


Les
(KiX Master)
2007-09-10 01:49 PM
Re: help need variable from ini

Ummm... you renamed it, remember?
 Code:
Display 'stbof.bak'             ; add original content to new file


Glenn BarnasAdministrator
(KiX Supporter)
2007-09-10 02:43 PM
Re: help need variable from ini

That's right, I renamed it...

Then created a new empty file with the original name.

Then added a header line to the new file that was missing in the original.

Then took the backed-up file contents and added them to the new file after the header, which is why I'm referencing the .BAK file in the Display command. Remember - the RedirectOutput is still in effect. Thus, the content in the original file name now looks like a valid INI file.

Glenn


Les
(KiX Master)
2007-09-10 02:54 PM
Re: help need variable from ini

Ja, but in your simplified version, it no worky unless you have stbof.bak as in the first example.
[edit]Nevermind... you surely went back and edited your original post


Glenn BarnasAdministrator
(KiX Supporter)
2007-09-10 03:03 PM
Re: help need variable from ini

Huh?

The first example is used if you want to modify the orignial and convert it to an INI format, hence the rename and copy of the BAK onto the INI. The second makes a new .INI file in proper format called MYstbof.ini. Since the original wasn't renamed, Display references the original .INI file and not the BAK.

G-


Les
(KiX Master)
2007-09-10 03:12 PM
Re: help need variable from ini

I know what the currently posted code does. I should have taken a screenshot *before* you edited. \:\)

thunderchero
(Fresh Scripter)
2007-09-10 05:20 PM
Re: help need variable from ini

Les,

I got the second one (read file) to work for me. Had to create folder in my install folder and copy the ini into folder so it would read from full path.

Thanks
thunderchero