Sign in to follow this  
Followers 0
ShadowedStranger

A better way of parsing text?

5 posts in this topic

Hey there all, this is my first post so... HEY!

Anywho, I 'wrote' a little prank script to do beeps and text to speech remotely... I saw 'wrote' because I took the TCP example scripts and added about 10 lines to them... Anyway, They work fine so I am not asking anyone to fix my script to make it work. I would just like to become better at autoit, and I am wondering if anyone has some tips for how to better parse the input of $recv. It seems like the way I am doing it is not the most efficient or robust. It would definitely start being fun to parse once multiple arguments are passed. I do want to say that I am familiar with the Switch...Case, etc as an alternative to my If...then, but I didn't feel that it was necessary with just 3 arguments. I am mainly wondering of a better way to parse text. If you see any other noob mistakes, feel free to point them out/call me stupid/whack me with a stick/etc.

Client: You can see how it's formatted

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt('MustDeclareVars', 1)

;==============================================
;==============================================
;CLIENT! Start Me after starting the SERVER!!!!!!!!!!!!!!!
;==============================================
;==============================================




Example()

Func Example()
    Local $Form1, $ttsinput, $Label1, $readtext, $Label2, $beeppitch, $Label3, $beeplength, $sendbeep, $nMsg, $szData
    #Region ### START Koda GUI section ### Form=
    $Form1 = GUICreate("Beepinator", 314, 124, 192, 124)
    $ttsinput = GUICtrlCreateInput("ttsinput", 8, 32, 217, 21)
    $Label1 = GUICtrlCreateLabel("Text To Speech", 8, 8, 25, 17)
    $readtext = GUICtrlCreateButton("Read Text", 232, 5, 73, 57, $WS_GROUP)
    $Label2 = GUICtrlCreateLabel("BEEP Frequency (37-32767)", 8, 64, 139, 17)
    $beeppitch = GUICtrlCreateInput("", 152, 64, 73, 21)
    $Label3 = GUICtrlCreateLabel("BEEP length (in milliseconds)", 8, 96, 140, 17)
    $beeplength = GUICtrlCreateInput("", 152, 96, 73, 21)
    $sendbeep = GUICtrlCreateButton("Send Beep", 232, 69, 73, 49, $WS_GROUP)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    ; Set Some reusable info
    ;--------------------------
    Local $ConnectedSocket, $szData
    ; Set $szIPADDRESS to wherever the SERVER is. We will change a PC name into an IP Address
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = "10.10.10.10" ;IP address of server.
    Local $nPORT = 33891

    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1

    ;Attempt to connect to SERVER at its IP and PORT 33891
    ;=======================================================
    $ConnectedSocket = TCPConnect($szIPADDRESS, $nPORT)

    ; If there is an error... show it
    If @error Then
        MsgBox(4112, "Error", "TCPConnect failed with WSA error: " & @error)
        ; If there is no error loop an inputbox for data
        ;   to send to the SERVER.
    Else
        ;Loop forever asking for data to send to the SERVER
        While 1
            ; InputBox for data to transmit
            ;$szData = InputBox("Data for Server", @LF & @LF & "Enter data to transmit to the SERVER:")
                While 1
                    $nMsg = GUIGetMsg()
                    Switch $nMsg
                        Case $GUI_EVENT_CLOSE
                            Exit
                        Case $readtext
                            $szData = GuiCtrlRead($ttsinput)
                            TCPSend($ConnectedSocket, $szData)
                        Case $sendbeep
                            $szData = "BEEP" & GuiCtrlRead($beeppitch) &  ":" & GuiCtrlRead($beeplength)
                            TCPSend($ConnectedSocket, $szData)
                    EndSwitch
                WEnd
            ; If they cancel the InputBox or leave it blank we exit our forever loop
            If @error Or $szData = "" Then ExitLoop

            ; We should have data in $szData... lets attempt to send it through our connected socket.
            TCPSend($ConnectedSocket, $szData)

            ; If the send failed with @error then the socket has disconnected
            ;----------------------------------------------------------------
            If @error Then ExitLoop
        WEnd
    EndIf
EndFunc   ;==>Example

Server: You can see how it's parsed.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstantsEx.au3>

Opt('MustDeclareVars', 1)
Opt("TrayIconHide", 1)
;==============================================
;==============================================
;SERVER!! Start Me First !!!!!!!!!!!!!!!
;==============================================
;==============================================

Example()

Func Example()
    ; Set Some reusable info
    ; Set your Public IP address (@IPAddress1) here.
;   Local $szServerPC = @ComputerName
;   Local $szIPADDRESS = TCPNameToIP($szServerPC)
    Local $szIPADDRESS = "172.20.227.39"
    Local $nPORT = 33891
    Local $MainSocket, $GOOEY, $edit, $ConnectedSocket, $szIP_Accepted
    Local $msg, $recv, $tts, $sendtext, $oSp, $beepfrequency, $beeptemp, $beeplength, $stringtrimrightamount
    $oSp = ObjCreate("SAPI.SpVoice")
    ; Start The TCP Services
    ;==============================================
    TCPStartup()

    ; Create a Listening "SOCKET".
    ;   Using your IP Address and Port 33891.
    ;==============================================
    $MainSocket = TCPListen($szIPADDRESS, $nPORT)

    ; If the Socket creation fails, exit.
    If $MainSocket = -1 Then Exit


    ; Create a GUI for messages
    ;==============================================
    $GOOEY = GUICreate("My Server (IP: " & $szIPADDRESS & ")", 300, 200)
    $edit = GUICtrlCreateEdit("", 10, 10, 280, 180)



    ; Initialize a variable to represent a connection
    ;==============================================
    $ConnectedSocket = -1


    ;Wait for and Accept a connection
    ;==============================================
    Do
        $ConnectedSocket = TCPAccept($MainSocket)
    Until $ConnectedSocket <> -1


    ; Get IP of client connecting
    $szIP_Accepted = SocketToIP($ConnectedSocket)

    ; GUI Message Loop
    ;==============================================
    While 1
        $msg = GUIGetMsg()

        ; GUI Closed
        ;--------------------

        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
        ; Try to receive (up to) 2048 bytes
        ;----------------------------------------------------------------
        $recv = TCPRecv($ConnectedSocket, 2048)

        ; If the receive failed with @error then the socket has disconnected
        ;----------------------------------------------------------------
        If @error Then ExitLoop

        ; Update the edit control with what we have received
        ;----------------------------------------------------------------
        If $recv <> "" Then
            If StringInStr($recv, "BEEP") > 0 Then
                ;$oSp.Speak($recv)
                $beeptemp = $recv
                $beepfrequency = StringTrimLeft($beeptemp, 4)
                $stringtrimrightamount = StringLen($beepfrequency)-StringInStr($beeptemp, ":")+1
                $beepfrequency = StringTrimRight($beepfrequency, $stringtrimrightamount)
                $beeplength = StringTrimLeft($beeptemp, StringInStr($beeptemp, ":"))
                ;MsgBox(0, "Beep Pitch", $beepfrequency)
                ;MsgBox(0, "Beep Length", $beeplength)
                Beep($beepfrequency, $beeplength)
            Else
                $oSp.Speak($recv)
            EndIf
        EndIf
    WEnd


    If $ConnectedSocket <> -1 Then TCPCloseSocket($ConnectedSocket)

    TCPShutdown()
EndFunc   ;==>Example

; Function to return IP Address from a connected socket.
;----------------------------------------------------------------------
Func SocketToIP($SHOCKET)
    Local $sockaddr, $aRet

    $sockaddr = DllStructCreate("short;ushort;uint;char[8]")

    $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $SHOCKET, _
            "ptr", DllStructGetPtr($sockaddr), "int*", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf

    $sockaddr = 0

    Return $aRet
EndFunc   ;==>SocketToIP

Share this post


Link to post
Share on other sites



Hi and welcome to the forums, please don't make prank scripts, it is a waste of your time, antivirus engineers' time, and our time since antiviruses sometimes flag our legitimate scripts as viruses. If you want to learn more please mature a little.

Share this post


Link to post
Share on other sites

Am I right in saying you want something like: "BEEP123:123"?

Ever heard of regular expressions? Well unfortunately for you thats what you want.

$recv = "BEEP123:456"
$a = StringRegExp($recv, "BEEP(\d+)\:(\d+)", 3)
MsgBox(0, "Beep test", "Frequency: " & $a[0] & @CRLF & "Length: " & $a[1])

You'd better get reading. Regular Expressions (regex) is a bit and complex subject. You must have heard the joke: A programmer had a problem. He decided to use regex to solve it. Said programmer now has two problems :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Am I right in saying you want something like: "BEEP123:123"?

Ever heard of regular expressions? Well unfortunately for you thats what you want.

$recv = "BEEP123:456"
$a = StringRegExp($recv, "BEEP(\d+)\:(\d+)", 3)
MsgBox(0, "Beep test", "Frequency: " & $a[0] & @CRLF & "Length: " & $a[1])

You'd better get reading. Regular Expressions (regex) is a bit and complex subject. You must have heard the joke: A programmer had a problem. He decided to use regex to solve it. Said programmer now has two problems :)

I can't seem to find the edit button, is it disabled after someone replies? Anyway since cartman isn't a fan of pranks because I am immature, I apologize, if mods could edit that part out. I actually wrote this as a trial or learning experiment for another script that will be pulling information from an xml input, and the way I was doing it would be a nightmare with a billion arguments to parse, hence why I am asking for a better way to parse incoming text. Either way, both of you responded with stringregexp, so I will do some research on that, thanks a bunch!

Smartee: Thanks for the stringmid, I was looking at the help file but silly me assumed that found a midpoint of a string... It's what I get for assuming something.

EDIT: MAT: Regex looks like exactly what I wanted, Now to figure out how to work this, time for me to do some homework. Thanks again guys(gals?)!

Edited by ShadowedStranger

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