Jump to content

How to handle all of this BS?


Zachlr
 Share

Recommended Posts

As you may know, many telnet servers only work if you do not press the backspace key throughout the entire sequence. This is because the backspace key is sent as a character, rather than actually backspacing the input. It gets frustrating when you have to type a command all over again because you pressed "S" instead of "D". Well, I'm trying to develop a function that will convert the backspace character (ascii 8) into a proper string. Here is what I have so far.

$text = "hi my name is zach" & Chr(8)& Chr(8)& Chr(8)& Chr(8)
;we'll say someone meant to delete the last four characters.

MsgBox(0, "", $text)
;show the original text.

MsgBox(0, "", BS($text))
;show the fixed text

Func BS($String)
    If StringLen($String) < 2 Then Return 0 
    $String = StringSplit($String, "")
    For $i = 1 To $String[0] 
        If $String[$i] = Chr(8) Then ;check each character for the backspace character 
            $String[$i-1] = "" ; if it is the BS char, then erase the previous character.
            For $n = $i To $String[0] -1 ;- $i ; shift all of the contents left to eliminate the space
                $String[$n] = $String[$n+1] 
            Next
        EndIf
    Next
    Return $String ;return the fixed string.
EndFunc

Somehow this code seems to turn $string into "" by the end of the function. If you can help me fix it, or you know of another way to accomplish this, please post a reply.

Thanks,

Zach

Link to comment
Share on other sites

I was working on something like that before, but I solved it a bit different. Have a look: http://www.autoitscript.com/forum/index.php?showtopic=54147

Thanks for the link. I think I'll be able to implement that into my script one way or another.

EDIT: I'm going to look into the thread mentioned above, but I'm still opened to solutions. So, if you have anything, please tell me about it.

EDIT: I'm under the impression that the method mentioned above will only work in a "per character" telnet session, not "per line". I'm not sure if it will work for my particular script.

Thanks again,

Zach

Edited by Zachlr
Link to comment
Share on other sites

These two examples may help.

$text = "hi my name" & Chr(8) & " is zach" & Chr(8) & Chr(8) & Chr(8)
;we'll say someone meant to delete the last three characters.

MsgBox(0, "Original text", $text)
;show the original text.

MsgBox(0, "BS(text)", BS($text))
;show the fixed text

Func BS($String)
    Local $sRet = ""
    If StringLen($String) < 2 Then Return 0
    $String = StringSplit($String, "")
    For $i = 1 To $String[0]
        If $String[$i] <> Chr(8) Then
            $sRet &= $String[$i];check each character for the backspace character
        Else
            $sRet = StringTrimRight($sRet, 1)
        EndIf
    Next
    Return $sRet ;return the fixed string.
EndFunc ;==>BS

;or
;=======================================================================
$TextModified = StringRegExpReplace($text, "([^\x08]\x08)", "") ; Will not replace two BS next to each other.

While StringInStr($TextModified, Chr(0x08))
    $TextModified = StringRegExpReplace($TextModified, "([^\x08]\x08)", "") ; Replace anything and BS with nothing.
    Sleep(10)
WEnd

MsgBox(0, "RegExp", $TextModified)
;========================================================================

MsgBox(0, "Original text", $text)
Link to comment
Share on other sites

These two examples may help.

$text = "hi my name" & Chr(8) & " is zach" & Chr(8) & Chr(8) & Chr(8)
;we'll say someone meant to delete the last three characters.

MsgBox(0, "Original text", $text)
;show the original text.

MsgBox(0, "BS(text)", BS($text))
;show the fixed text

Func BS($String)
    Local $sRet = ""
    If StringLen($String) < 2 Then Return 0
    $String = StringSplit($String, "")
    For $i = 1 To $String[0]
        If $String[$i] <> Chr(8) Then
            $sRet &= $String[$i];check each character for the backspace character
        Else
            $sRet = StringTrimRight($sRet, 1)
        EndIf
    Next
    Return $sRet ;return the fixed string.
EndFunc ;==>BS

;or
;=======================================================================
$TextModified = StringRegExpReplace($text, "([^\x08]\x08)", "") ; Will not replace two BS next to each other.

While StringInStr($TextModified, Chr(0x08))
    $TextModified = StringRegExpReplace($TextModified, "([^\x08]\x08)", "") ; Replace anything and BS with nothing.
    Sleep(10)
WEnd

MsgBox(0, "RegExp", $TextModified)
;========================================================================

MsgBox(0, "Original text", $text)

Success! Thank you!
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...