Sign in to follow this  
Followers 0
neo van matix

Parsing a file - Problem with delimiter

7 posts in this topic

Hello,

i'm writing a little script, that parses an configuration file.

This is the *.ini-File (from prism):

[Parameters]

id=worldclient@xxx

name=WorldClient

uri=http://xxx:3000/worldclient.dll?View=Main&User=EMAILADRESSE&Password=PASSWORT

icon=favicon

status=yes

location=no

sidebar=no

navigation=no

trayicon=yes

Okay, now.. i wrote a script, that splits this File by the ending @CRLF into an array; each $array[x] is one line of the ini-File.

Then, i split each line, if the script finds an "=".

That works perfectly - a little bit to perfect. Line 4 - the uri-parameters...

The Code of the ReadWebappIni:

Func ReadWebappIni()
    $file = FileOpen("c:\prism\_packages\worldclient.webapp-source\WebApp.ini", 0)

    If $file = -1 Then
        MsgBox(0, "Fehler", "Konnte c:\prism\_packages\worldclient.webapp-source\WebApp.ini nicht öffnen!")
    Else
        $configfile = FileRead($file)
        $configfile = StringSplit($configfile, @CRLF, 1)

        For $i = 2 to $configfile[0]
            $configfile_var = _StringExplode($configfile[$i], "=", 0)
            _ArrayDisplay($configfile_var)
            Switch $configfile_var[0]
                Case "id"
                    GUICtrlSetData($input_id, $configfile_var[1])
                Case "name"
                    GUICtrlSetData($input_name, $configfile_var[1])
                Case "uri"
                    GUICtrlSetData($input_uri, $configfile_var[1])
                Case "icon"
                    GUICtrlSetData($input_icon, $configfile_var[1])
                Case "status"
                    IF $configfile_var[1] = "yes" Then GUICtrlSetState($checkbox_statusbar, $GUI_CHECKED)
                Case "location"
                    IF $configfile_var[1] = "yes" Then GUICtrlSetState($checkbox_location, $GUI_CHECKED)
                Case "navigation"
                    IF $configfile_var[1] = "yes" Then GUICtrlSetState($checkbox_navigation, $GUI_CHECKED)
                Case "trayicon"
                    IF $configfile_var[1] = "yes" Then GUICtrlSetState($checkbox_trayicon, $GUI_CHECKED)
            EndSwitch
        Next
    EndIf
EndFunc

What could i do? Is there any function, that works like... "Split by the first occurance of = the string into two parts; first part all chars left by =; second part all chars right by =" ? Maybe with a regex?

thanks for help!


Attention! English noob ^^

Share this post


Link to post
Share on other sites



Try using the Iniread functions instead, it's a lot easier than what you're doing. You also might want to use the IniReadSection function which will put all the keys/values into an array. One line will do it all.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I would use StringInStr() to get the location of "?", then StringTrimLeft() to get the string to the right side the "?".

$string="aaaa?a=b&b=c"

$string=StringTrimLeft($string,StringInStr($string,"?"))

MsgBox(0,"",$string)

Then you can StringSplit() what remains using "=" as a delimiter.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Here's how to do it the short way:

#include <Array.au3>
$File = "c:\prism\_packages\worldclient.webapp-source\WebApp.ini"
$Array = IniReadSection($File, "Parameters")
_ArrayDisplay($Array)
Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Here's how to do it the short way:

#include <Array.au3>
$File = "c:\prism\_packages\worldclient.webapp-source\WebApp.ini"
$Array = IniReadSection($File, "Parameters")
_ArrayDisplay($Array)

omfg -.- It's really nothing more... and i tried it the other way for many hours.. -.-

Thanks!


Attention! English noob ^^

Share this post


Link to post
Share on other sites

It's fortunate that the file you were trying to read from was in the .ini format which makes it soooo much easier to parse it. Of course if the file wasn't in the .ini format, you could always convert it to an .ini file by reading in the first file, and then using IniWriteSection to write the text to a temporary .ini file, and reading it in again using the scriptlet I gave you, and then deleting the temp ini file.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Many scripts that I run uncompiled I will wrap the ini settings in quote tags and use the script itself as the settings file.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0