|
-
June 9th, 2007, 03:14 AM
#1
String handling in Buffer
Hi Friends i'm writing a small utility in borland c++ builder which opens a telnet client reads the messages the Telnet Host sends. Based on the messages it sends i'm call my functions.I'm using the TIdTelnet Indy Client Component which comes built in with Borland C++ Builder 6.0.
The TIdTelnet component has an event handler called OnDataAvailable which handles all the messages sent by the host
The function is like this
//----------------------------------------------------------------------
void __fastcall TForm1::IdTelnet1DataAvailable(AnsiString Buffer)
{
}
//----------------------------------------------------------------------
Here Buffer is where all the messages are recieved
The Default Message i should get when i log on to the host is as below
-------------------------------------------------------------
PCQLinux 2004 (NewDawn)
Kernel 2.4.22-1.2115.nptl on an i686
login:
-------------------------------------------------------------
So my function should be something like this
//----------------------------------------------------------------------
void __fastcall TForm1::IdTelnet1DataAvailable(AnsiString Buffer)
{
AnsiString s;
s=Buffer;
if(s=="login: ")
{
IdTelnet1->WriteLn("postgres");
}
}
//----------------------------------------------------------------------
Here i'm waiting for the "login: " or in any case the last line to appear in buffer after which the function is called.
But the condition fails because when i check the value of "s" it will be some thing like this
"PCQLinux 2004 (NewDawn)\r\nKernel 2.4.22-1.2115.nptl on an i686\r\nlogin: "
So i did something like this but That to did not help
//----------------------------------------------------------------------
void __fastcall TForm1::IdTelnet1DataAvailable(AnsiString Buffer)
{
int Start, Stop;
Start = 1;
Stop = Buffer.Pos(IntToStr('\n')); //the character count to a CR in the buffer
if(Stop == 0)
Stop = Buffer.Length() + 1;
while(Start <= Buffer.Length())
{
s=Buffer.SubString(Start,Stop-Start);
if(Buffer.SubString(Stop,1)=='\n')
{
}
Start = Stop + 1;
if(Start > Buffer.Length())
{
break;
}
if(Buffer.SubString(Start,1)=='\n')
{
Start++;
}
Stop = Start;
while((Buffer.SubString(Stop, 1)!= '\n')&&(Stop <= Buffer.Length()))
{
Stop++;
}
}
value->Text=s;
if(s=="login: ")
{
IdTelnet1->WriteLn("postgres");
}
}
//----------------------------------------------------------------------
But still the problem repeats.Any help is greatly appreciated. Thanks in advance.
Last edited by perk40; June 9th, 2007 at 03:17 AM.
-
June 9th, 2007, 04:53 AM
#2
Re: String handling in Buffer
It's very hard to read without code tags so may I instead suggest that all you search the buffer in reverse order for a '\n' and use that position+1 as starting point for the substring to test?
-
June 9th, 2007, 11:17 AM
#3
Re: String handling in Buffer
Code:
#include <iostream>
#include <cstring>
int main(){
using namespace std;
char buffer[] = "PCQLinux 2004 (NewDawn)\r\nKernel 2.4.22-1.2115.nptl on an i686\r\nlogin:";
char *ptc = strstr(buffer, "login:");
char *s = new char[strlen(ptc) + 1];
strcpy(s, ptc);
cout << s << endl;
return 0;
}
-
June 11th, 2007, 02:37 AM
#4
Re: String handling in Buffer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|