Link to home
Start Free TrialLog in
Avatar of Icco
Icco

asked on

CRichEditCtrl problem with backspace-char from network

Hi,
I am writing a dialogbased MFC console/telnet application. My problem is that when a backspace character (ascii 8) is arriving from some external source (network, serial...not the keyboard) the CRichEditCtrl class won´t print it out correctly. Instead it prints out a garbage character. Is there an easy way to handle this? I don´t want to check every arriving character, that would slow the application down to much.


void CRichEditCtrlTerminal::OnDataArrival(char *buffer, int size)
{
      // Insert in rich edit
      // Without checking every incoming char
      .
      .
      .
      ReplaceSel(buffer)      // Generates garbage when backspace (ascii 8) arrives
      .
      .
      .
}

This problem is driving me nuts, coz I have a gut feeling that it´s not a difficult problem.

// Icco
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

'I don´t want to check every arriving character, that would slow the application down to much'

I would be surprised that would happen.

Get your buffer
use _tcschr to search for an instance of ascii 8, if there is one (or more) then replace with what you want to display.
Now pass the buffer onto the RichEdit
Avatar of freewell
freewell

Just before you pass the result into edit control, do a little conversion. :)

CString strTemp(buffer);
while(strTemp.Replace(0x08,'*'));
ReplaceSel(strTemp);
SOLUTION
Avatar of martynjpearson
martynjpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Icco

ASKER

I'm sorry if i didn't give you enough information.
When receiving ascii character 0x08 from some external connection I would like the cursor to step one char back.

In this case, a backspace from the OS that I am trying to communicate with through a console port, is the sequence  0x08 0x32 0x08.
This means: go back one char (0x08), print space char(0x32), go back one char (0x08).  When trying to print the 0x08 char to the CRichEditCtrl, it prints out garbage where I want it to go back one char.

example:

When I send the backspacechar to the console, the shell echoes back the sequence 0x08, 0x32, 0x08.
This will be printed as: '',' ','' in the CRichEditCtrl instead of removing the last char.

The sequence 0x08 0x32 0x08 is nothing that I can change, that´s what is received from the console and I have to handle it correctly.

//Icco
Do I understand correctly that you would want the 0x08 0x32 0x08 string to be changed to 0x32 ?
If so parse the buffer you have for incidences of this string. Copy the bit before to another buffer.  Move 3 chars forward then look for next instance.  None found, copy rest of string to second buffer, found an instance copy up to that point then forward 3 chars again, continue parsing.
OK, in that case there are two approaches - either you can process the text to remove characters that fall before the 0x08 (to simulate backspace), or you can break up the data top get the bits of text and the 0x08's, sending the text, then getting the rich edit control to delete the last character, then sending the next.

For example, say you have the data

H e l l l BS o ! ! ! BS

You could process the string so that the third 'l' and the third '!' are removed (along with the backspaces of course), or you could break the text up like this :

Send "Helll" to control
Get control to backspace
Send "o!!!" to control
Get control to backspace

It is probably easier and faster to process the string to deal with backspaces before you get to the control, using code like this :

The one complication arises when the stream starts with one or more backspace characters, as here you will need to instruct the rich edit control to do the backspace for you. However, you could be a little cunning and set the selection so that your text overwrites part of the content simulating a backspace.

Hope this helps
Martyn
Avatar of Icco

ASKER

So if I understand this right, there isn't a way to insert text (that contains BS-chars) arriving from a shell without processing it or simulating a BS.

martynjpearson writes: "...you will need to instruct the rich edit control to do the backspace for you."
I tried this to simulate a backspace:

      GetSel(start_pos, end_pos);
      start_pos--;
      SetSel(start_pos,end_pos);
      ReplaceSel(CString(""));

Is there an easier way to get the control to do a backspace? The way it works now is not really what I want.
I want it to go back without processing incoming data, but maybe it just isn´t possible...


Real-life example coming up =)
When pressing escape followed by 'k' the shell returns the last command followed by the equal amount of 0x08-chars. It does this so that the cursor will be set at the first position on that line (don´t ask me why, but it does it that way).

In my app the output will be:
-> version
the cursor is set at the last position.

But the output should be:
-> version
and the cursor set at the first position (before the 'v')


// Icco
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial