Board index » cppbuilder » Anyone familiar with the std:: search and std::replace

Anyone familiar with the std:: search and std::replace

I have a search function that i pieced together, it seems to work pretty
good, but i want to know how to do a replace also, and i cannot seem to
figure it out.  Can someone offer some wisdom, this is what i have so far.

    TMemoryStream* MemoryStream = new TMemoryStream;
    MemoryStream->LoadFromFile( FileName );
    BYTE* MemoryStreamBuffer = static_cast <BYTE*>( MemoryStream->Memory );
    int StreamLength = MemoryStream->Size;

    char* Sequence;
    int ByteCount = EditBytes->Text.Length();
    Sequence = new char[ ByteCount ];
    strncpy( Sequence, EditBytes->Text.c_str(), ByteCount );
//    BYTE Sequence[] = {0x10,0x00,0xFF,0x74,0x24,0x04};

    BYTE* PastTheEnd = MemoryStreamBuffer + StreamLength;

    BYTE* Pos = std::search( MemoryStreamBuffer, PastTheEnd, Sequence,
                             Sequence + sizeof(Sequence) );

// Add a replace here

    delete MemoryStream;

    if( Pos != PastTheEnd )
    {
        TListItem *Item;
        Item = ListViewFiles->Items->Add();
        Item->Caption = FileName;
        Item->SubItems->Add( "[ has the search bytes. ]" );
        return; // No need for more searching
    }

Thanks in advance.

 

Re:Anyone familiar with the std:: search and std::replace


I don't mean to shortcut your exercise, but you may wish to read this
message about parsing AnsiStrings, since your data are already in one:
http://www.mers.com/MERLIST/BORLAND/PUBLIC/CPPBUILDER/LANGUAGE/73394.H
TML

--
Timothy H. Buchman
========================================
City Center Theater
New York NY
tbuch...@citycenter.org
Please treat this signature information as confidential.
========================================
Search .borland newsgroup archives at:
http://www.mers.com/searchsite.html

Quote
Jason Savala <j_sav...@yahoo.com> wrote in message

news:8l59o4$2go6@bornews.borland.com...
Quote
> I have a search function that i pieced together, it seems to work
pretty
> good, but i want to know how to do a replace also, and i cannot seem
to
> figure it out.  Can someone offer some wisdom, this is what i have
so far.

>     TMemoryStream* MemoryStream = new TMemoryStream;
>     MemoryStream->LoadFromFile( FileName );
>     BYTE* MemoryStreamBuffer = static_cast

BYTE*>( MemoryStream->Memory );
Quote
>     int StreamLength = MemoryStream->Size;

>     char* Sequence;
>     int ByteCount = EditBytes->Text.Length();

Re:Anyone familiar with the std:: search and std::replace


Dead link
Quote
> http://www.mers.com/MERLIST/BORLAND/PUBLIC/CPPBUILDER/LANGUAGE/73394.H

Re:Anyone familiar with the std:: search and std::replace


Your newsreader wrapped the line.  It ends in .HTML
THB

Quote
Jason Savala <j_sav...@yahoo.com> wrote in message

news:8l5q28$79m10@bornews.borland.com...
Quote
> Dead link

http://www.mers.com/MERLIST/BORLAND/PUBLIC/CPPBUILDER/LANGUAGE/73394.H
Quote

Re:Anyone familiar with the std:: search and std::replace


Timothy i dont' understand how a string replacement routine is going to
replace bytes in a file, from what i read this will replace the occurance of
one string to another.  But when you search a file for a byte sequence that
is a lot different, i have tried routines before to search for bytes as a
string and it never finds anything.

Maybe i am over looking something, but if you could give me an example that
might help me out.

Thanks.

Re:Anyone familiar with the std:: search and std::replace


Quote
>     char* Sequence;
>     int ByteCount = EditBytes->Text.Length();
>     Sequence = new char[ ByteCount ];
>     strncpy( Sequence, EditBytes->Text.c_str(), ByteCount );

>     BYTE* Pos = std::search( MemoryStreamBuffer, PastTheEnd, Sequence,
>                              Sequence + sizeof(Sequence) );

I think you have a bug here. You better inspect the value of sizeof(Sequence).
Sequence is a pointer. sizeof(Sequence) is going to be 4 bytes, regardless of how
many characters are in the string.

My personal opinion is that Sequence should include the null-terminator. That way,
you could use strlen instead of sizeof. You don't want to include the null terminator
in your search, but that does not mean that you can't have a null terminator on your
string.

Quote
> // Add a replace here

The generic replace algorithm replaces individual elements in the container, not a
range of elements. In your case, an individual element is a character. If you wanted
to replace all 'b' characters with 'Z' characters, you could user std::replace.
However, I don't think this is what your are looking for. You probably want to
replace the string "foo" with "FooBar". std::replace can't help you there.

You need to erase the original matching characters, and then insert the new
characters somehow. Beware that your memory stream goofs this up because there is a
good chance that you will need to grow the memory stream during the replace. Since
the STL algorithms have no knowledge that your memory is being managed by the memory
stream, there is a good chance that an AV will occur. A vector or list would make
this easier.

Your question ended up being more about the STL than the VCL. STL questions typically
go in the language group.

Harold Howe [TeamB]
http://www.bcbdev.com

Re:Anyone familiar with the std:: search and std::replace


Thanks harold, i will repost the question in there.

Other Threads