Jump to content
Sign in to follow this  
Rafaelinio

Read part of line in GUI Text box.

Recommended Posts

Rafaelinio

Basically, i am trying to make my own programming language. But idk how to start. i made my own program and now i am trying to figure out how i can read part of the text box line. for example if there is the word msg, then read the text in quotes and then the comma and then the rest text in quotes.

for example

msg ("LOL!","LOL2!")

and then move to the next line.

(The first quotes is the msg's title and the second is the message)

The code i have up to now:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("RAF", 612, 438, 192, 124)
$Button1 = GUICtrlCreateButton("Save", 0, 0, 57, 17)
$Button2 = GUICtrlCreateButton("Open", 56, 0, 57, 17)
$Button3 = GUICtrlCreateButton("Run", 112, 0, 57, 17)
$Edit1 = GUICtrlCreateEdit("", 0, 20, 609, 410)
GUICtrlSetData(-1, "")
$Button4 = GUICtrlCreateButton("About", 552, 0, 57, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $Save = filesavedialog("Save File", "C:\", "Raf (*.raf)", 2, "Untitled.raf")
            $text = GUICtrlRead($Edit1)
            FileWrite($Save, $text)
        Case $Button2
            $Open = FileOpenDialog("Open File", "C:\", "Raf (*.raf)")
            $Read = FileRead($Open)
            GuiCtrlSetData($Edit1, $Read)
        Case $Button3

    EndSwitch
WEnd

Share this post


Link to post
Share on other sites
JLogan3o13

Hi, Rafaelinio. I would suggest beginning by looking at StringSplit in the Help file. You can also look at StringRegExp in the Help file; someone may wander by with an example, but see what you can work out on your own. If you continue to experience problems, post what you have here and we will attempt to help.

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Rafaelinio

yeah but, how do i split the brackets, quotations AND commas?

Edit: And how do i NOT count the commas in the quotations?

Edited by Rafaelinio

Share this post


Link to post
Share on other sites
czardas

This is more complicated than it at first appears. However, similar conventions apply to strings (in AutoIt) as apply to csv format - well some of the syntax is similar. I have only just realised this. :doh:

Using such functions seems a lot like overkill. The method fails to cater for string wrapped in single quotes. Anyway it's the best I could come up with, without spending lots of time trying to figure this out. While the code works perfectly in the example, it merely illustrates the complications involved and is not necessarily a solution I would recommend.

;

#include <Array.au3>

Local $sMsgBox = 'msg(""","",""",""","",""")'

Local $regexp = "(\A\s*msg\s*\()(?s)(.+)(\)\s*\z)"
Local $sTest = StringRegExpReplace($sMsgBox, $regexp, "$2")

Local $aRet = _CSVSplit($sTest)
_ArrayDisplay($aRet)

MsgBox(0, $aRet[0][0], $aRet[0][1])

Func _CSVSplit($string, $sDelim = ",") ; Parses csv string input and returns a one or two dimensional array
    If Not IsString($string) Or $string = "" Then Return SetError(1, 0, 0) ; Invalid string
    If Not IsString($sDelim) Or $sDelim = "" Then Return SetError(2, 0, 0) ; Invalid string

    $string = StringRegExpReplace($string, "[\r\n]+\z", "") ; [Line Added] Remove training breaks
    Local $iOverride = 63743, $asDelim[3] ; $asDelim => replacements for comma, new line and double quote
    For $i = 0 To 2
        $asDelim[$i] = __GetSubstitute($string, $iOverride) ; Choose a suitable substitution character
        If @error Then Return SetError(3, 0, 0) ; String contains too many unsuitable characters
    Next
    $iOverride = 0

    Local $aArray = StringRegExp($string, '\A[^"]+|("+[^"]+)|"+\z', 3) ; Split string using double quotes delim - largest match
    $string = ""

    Local $iBound = UBound($aArray)
    For $i = 0 To $iBound -1
        $iOverride += StringInStr($aArray[$i], '"', 0, -1) ; Increment by the number of adjacent double quotes per element
        If Mod ($iOverride +2, 2) = 0 Then ; Acts as an on/off switch
            $aArray[$i] = StringReplace($aArray[$i], $sDelim, $asDelim[0]) ; Replace comma delimeters
            $aArray[$i] = StringRegExpReplace($aArray[$i], "(\r\n)|[\r\n]", $asDelim[1]) ; Replace new line delimeters
        EndIf
        $aArray[$i] = StringReplace($aArray[$i], '""', $asDelim[2]) ; Replace double quote pairs
        $aArray[$i] = StringReplace($aArray[$i], '"', '') ; Delete enclosing double quotes - not paired
        $aArray[$i] = StringReplace($aArray[$i], $asDelim[2], '"') ; Reintroduce double quote pairs as single characters
        $string &= $aArray[$i] ; Rebuild the string, which includes two different delimiters
    Next
    $iOverride = 0

    $aArray = StringSplit($string, $asDelim[1], 2) ; Split to get rows
    $iBound = UBound($aArray)
    Local $aCSV[$iBound][2], $aTemp
    For $i = 0 To $iBound -1
        $aTemp = StringSplit($aArray[$i], $asDelim[0]) ; Split to get row items
        If Not @error Then
            If $aTemp[0] > $iOverride Then
                $iOverride = $aTemp[0]
                ReDim $aCSV[$iBound][$iOverride] ; Add columns to accomodate more items
            EndIf
        EndIf
        For $j = 1 To $aTemp[0]
            If StringLen($aTemp[$j]) Then
                If Not StringRegExp($aTemp[$j], '[^"]') Then ; Field only contains double quotes
                    $aTemp[$j] = StringTrimLeft($aTemp[$j], 1) ; Delete enclosing double quote single char
                EndIf
                $aCSV[$i][$j -1] = $aTemp[$j] ; Populate each row
            EndIf
        Next
    Next

    If $iOverride > 1 Then
        Return $aCSV ; Multiple Columns
    Else
        For $i = 0 To $iBound -1
            If StringLen($aArray[$i]) And (Not StringRegExp($aArray[$i], '[^"]')) Then ; Only contains double quotes
                $aArray[$i] = StringTrimLeft($aArray[$i], 1) ; Delete enclosing double quote single char
            EndIf
        Next
        Return $aArray ; Single column
    EndIf
EndFunc ;==> _CSVSplit

Func __GetSubstitute($string, ByRef $iCountdown)
    If $iCountdown < 57344 Then Return SetError(1, 0, "") ; Out of options
    Local $sTestChar
    For $i = $iCountdown To 57344 Step -1
        $sTestChar = ChrW($i)
        $iCountdown -= 1
        If Not StringInStr($string, $sTestChar) Then
            Return $sTestChar
        EndIf
    Next
    Return SetError(1, 0, "") ; Out of options
EndFunc ;==> __GetSubstitute

;

Function descriptions can be found at the following URL:

As I stated in an earlier topic, all the rules in a language have to be clearly defined, and without conflict. It's easier to disallow certain types of syntax than to cater for many complicated variants, such as occur in AutoIt. See the next example:

;

MsgBox(0, '",","', '"' & "," & """" & ',' & '"')

;

All the code does is illustrate different ways of writing the same thing.

BTW: Question is not exactly GUI related.

Edited by czardas

Share this post


Link to post
Share on other sites
Rafaelinio

OK Thanks! Since im not on a PC right now ill test it either later or tomorrow and tell you how its going :)

Share this post


Link to post
Share on other sites
czardas

The more I think about this, the more interesting it becomes. All function parameters in a language could be parsed using a similar technique to the one above. The code was not intended for such purposes and would need to be improved (it would need to be more flexible).

I suggest you first become familiar with the different types of syntax used for strings in AutoIt. Then decide exactly what syntax you want to use in your own project. Make sure it has no flaws and only then think about how to write the code to interpret your new syntax.

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  

×