BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

memory stream parsing

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
xa
Guest





PostPosted: Sun May 15, 2005 5:44 pm    Post subject: memory stream parsing Reply with quote



I have some html code stored in a memory stream and i need to strip out the
"rn" prior to assigning the dat afor display... can anyone show me an
effective way of doing this please..



Back to top
Liz Albin
Guest





PostPosted: Sun May 15, 2005 6:12 pm    Post subject: Re: memory stream parsing Reply with quote



On Sun, 15 May 2005 18:44:48 +0100, xa wrote:

Quote:
I have some html code stored in a memory stream and i need to strip out the
"rn" prior to assigning the dat afor display... can anyone show me an
effective way of doing this please..

If you can put your data in an AnsiString you can use StringReplace()
--
Good luck,

liz

Back to top
Vladimir Stefanovic
Guest





PostPosted: Sun May 15, 2005 7:19 pm    Post subject: Re: memory stream parsing Reply with quote



You can easily access single bytes inside TMemoryStream, and
further make some logic by yourself.

The easiest way is to use temporary memory stream to get rid of
unnecessary "rn" but you still can use only (the original) one, but
with more logic involved.

This code (UNTESTED) should get rid of all "r" or "n", not only
sequences of them.

I's not so hard to extend the code to search for sequences only.

// UNTESTED !!! To check the code place a file "file.txt" in the
//same root where your EXE is placed, and after Button1Click, the
//second file should be created "file2.txt".

void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream *MS_OLD = NULL;
TMemoryStream *MS_NEW = NULL;
try
{
try
{
MS_OLD = new TMemoryStream;
MS_NEW = new TMemoryStream;

MS_OLD->LoadFromFile( ExtractFilePath( ParamStr(0) ) +
"file.txt" );

for ( int i=0; i<MS_OLD->Size ; i++ )
{
if (
(((char*)(MS_OLD->Memory))[i] != 'r') &&
(((char*)(MS_OLD->Memory))[i] != 'n')
)
{
MS_NEW->WriteBuffer( &((char*)(MS_OLD->Memory))[i] , 1 );
}
}
MS_NEW->SaveToFile( ExtractFilePath( ParamStr(0) ) + "file2.txt" );
}
catch ( const Exception &e )
{
ShowMessage( "Error" );
}
}
__finally
{
if ( MS_OLD )
delete MS_OLD;
if ( MS_NEW )
delete MS_NEW;
}
}


--
Best regards,
Vladimir Stefanovic
"xa" <pageme (AT) ntlworld (DOT) com> wrote

Quote:
I have some html code stored in a memory stream and i need to strip out the
"rn" prior to assigning the dat afor display... can anyone show me an
effective way of doing this please..






Back to top
Vladimir Stefanovic
Guest





PostPosted: Sun May 15, 2005 7:31 pm    Post subject: Re: memory stream parsing Reply with quote

To extend the code to search for sequences "rn", you can do
something like this (untested again):

Quote:
for ( int i=0; i<MS_OLD->Size ; i++ )

for ( int i=0; i<MS_OLD->Size-1; i++ )

Quote:
if (
(((char*)(MS_OLD->Memory))[i] != 'r') &&
(((char*)(MS_OLD->Memory))[i] != 'n')
)

if (
(((char*)(MS_OLD->Memory))[i] != 'r') &&
(((char*)(MS_OLD->Memory))[i+1] != 'n')
)

Quote:
MS_NEW->WriteBuffer( &((char*)(MS_OLD->Memory))[i] , 1 );

MS_NEW->WriteBuffer( &((char*)(MS_OLD->Memory))[i] , 1 );
++i; //<--- THIS IS ADDED



--
Best regards,
Vladimir Stefanovic



Back to top
xa
Guest





PostPosted: Mon May 16, 2005 3:39 am    Post subject: Re: memory stream parsing Reply with quote

Thanks Vladimir.

"Vladimir Stefanovic" <antivari (AT) po (DOT) sbb.co.yu> wrote

Quote:
To extend the code to search for sequences "rn", you can do
something like this (untested again):

for ( int i=0; i<MS_OLD->Size ; i++ )

for ( int i=0; i<MS_OLD->Size-1; i++ )

if (
(((char*)(MS_OLD->Memory))[i] != 'r') &&
(((char*)(MS_OLD->Memory))[i] != 'n')
)

if (
(((char*)(MS_OLD->Memory))[i] != 'r') &&
(((char*)(MS_OLD->Memory))[i+1] != 'n')
)

MS_NEW->WriteBuffer( &((char*)(MS_OLD->Memory))[i] , 1 );

MS_NEW->WriteBuffer( &((char*)(MS_OLD->Memory))[i] , 1 );
++i; //<--- THIS IS ADDED



--
Best regards,
Vladimir Stefanovic





Back to top
Vladimir Stefanovic
Guest





PostPosted: Mon May 16, 2005 4:35 pm    Post subject: Re: memory stream parsing Reply with quote

I made some late changes, and now I think that this
must work:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream *MS_OLD = NULL;
TMemoryStream *MS_NEW = NULL;
try
{
try
{
MS_OLD = new TMemoryStream;
MS_NEW = new TMemoryStream;

MS_OLD->LoadFromFile( ExtractFilePath( ParamStr(0) ) +
"file.txt" );
for ( int i=0; i<MS_OLD->Size-1 ; i++ )
{
if (
(((char*)(MS_OLD->Memory))[i] == 'r') &&
(((char*)(MS_OLD->Memory))[i+1] == 'n')
)
{
++i;
}
else
{
MS_NEW->WriteBuffer( &((char*)(MS_OLD->Memory))[i] , 1 );
}
}
MS_NEW->SaveToFile( ExtractFilePath( ParamStr(0) ) + "file2.txt" );
}
catch ( const Exception &e )
{
ShowMessage( "Error" );
}
}
__finally
{
if ( MS_OLD )
delete MS_OLD;
if ( MS_NEW )
delete MS_NEW;
}
}


--
Best regards,
Vladimir Stefanovic


Back to top
Jonathan Benedicto
Guest





PostPosted: Mon May 16, 2005 4:56 pm    Post subject: Re: memory stream parsing Reply with quote

"Vladimir Stefanovic" <antivari (AT) po (DOT) sbb.co.yu> wrote

Quote:
I made some late changes, and now I think that this
must work:

Not to discount your code, but isn't this a little simpler:

AnsiString Data = AnsiString(Stream->Memory, Stream->Size);

Data = StringReplace(Data, "rn", "", TReplaceFlags() <<
rfReplaceAll);

Stream->Clear();
Stream->Write(Data.c_str(), Data.Length());

Jonathan



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon May 16, 2005 5:12 pm    Post subject: Re: memory stream parsing Reply with quote


"Vladimir Stefanovic" <antivari (AT) po (DOT) sbb.co.yu> wrote


Quote:
// UNTESTED !!!

You don't need a second stream, you can modify the source stream's data
directly, ie:

{
TMemoryStream *MS = new TMemoryStream;
// fill stream as needed...

LPBYTE Original = static_cast<LPBYTE>(MS->Memory);
LPBYTE Ptr = Original;
LPBYTE End = (Original + MS->Size);

while( Ptr < End )
{
if( IS_CR_OR_LF(*Ptr) )
{
LPBYTE Temp = (Ptr + 1);
while( (Temp < End) && IS_CR_OR_LF(*Temp) )
++Temp;

::MoveMemory(Ptr, Temp, End - Temp);
End -= (Temp - Ptr);
}
else
++Ptr;
}

MS->Size = (End - Original);
// use new stream data as needed...

delete MS;
}


Gambit



Back to top
Vladimir Stefanovic
Guest





PostPosted: Mon May 16, 2005 6:25 pm    Post subject: Re: memory stream parsing Reply with quote

Quote:
Not to discount your code, but isn't this a little simpler:

Yes, I know for StringReplace(), that's what I mainly use
in simple cases.

But I never knew about AnsiString capacities so I tried with
Streams directly.



--
Best regards,
Vladimir Stefanovic



Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon May 16, 2005 6:27 pm    Post subject: Re: memory stream parsing Reply with quote


"Jonathan Benedicto" <incorrect (AT) no (DOT) server> wrote


Quote:
Not to discount your code, but isn't this a little simpler:

Simplier perhaps, but much less efficient since you are now copying the data
around 4 times.


Gambit



Back to top
Jonathan Benedicto
Guest





PostPosted: Mon May 16, 2005 6:27 pm    Post subject: Re: memory stream parsing Reply with quote

"Vladimir Stefanovic" <antivari (AT) po (DOT) sbb.co.yu> wrote

Quote:

Yes, I know for StringReplace(), that's what I mainly use
in simple cases.

But I never knew about AnsiString capacities so I tried with
Streams directly.

I thought that AnsiStrings worked off the heap, and therefore were
well able to take huge strings. So, I always used them for things like
this.

Jonathan



Back to top
Jonathan Benedicto
Guest





PostPosted: Mon May 16, 2005 6:30 pm    Post subject: Re: memory stream parsing Reply with quote

"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote

Quote:

Simplier perhaps, but much less efficient since you are now copying
the data
around 4 times.

I didn't think of that. Thank you for enlightening me.

Jonathan



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.