RegEx and streams Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
utopico
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Aug 03, 2006 11:30 am

RegEx and streams

Post by utopico »

Is it possible to use wxRegEx with streams and if not do you have any sugestions to what library to use?

I had a look at the wxCTB serial communication lib, but it is all based on wxIOBase? If I could get a confirmation on how good the regexp in the wxCTB lib, I could probably take the time to rewrite it so that it support streams instead.

Best regards

Jardar
Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor »

What do you want to do exactly ?

If you want to use regex to filter stream dataflow, you can derive from wxFilter[Input/Output]Stream and override OnSysRead or OnSysWrite to do what you want.

This is a simple copy output stream (from wx internals):

Code: Select all

class wxStoredOutputStream : public wxFilterOutputStream
{
public:
    wxStoredOutputStream(wxOutputStream& stream) :
        wxFilterOutputStream(stream), m_pos(0) { }

    bool Close() {
        m_pos = 0;
        m_lasterror = wxSTREAM_NO_ERROR;
        return true;
    }

protected:
    virtual size_t OnSysWrite(const void *buffer, size_t size);
    virtual wxFileOffset OnSysTell() const { return m_pos; }

private:
    wxFileOffset m_pos;
    DECLARE_NO_COPY_CLASS(wxStoredOutputStream)
};

size_t wxStoredOutputStream::OnSysWrite(const void *buffer, size_t size)
{
    if (!IsOk() || !size)
        return 0;
    size_t count = m_parent_o_stream->Write(buffer, size).LastWrite();
    if (count != size)
        m_lasterror = wxSTREAM_WRITE_ERROR;
    m_pos += count;
    return count;
}
What is little and green, witch go up and down ??
Yoda playing with the force.
utopico
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Aug 03, 2006 11:30 am

Post by utopico »

Thanks for the answer, but did not give me my answer.

My question is if the wxRegEx can work on general data streams. Thus be able to continue somehow from wher it left last time. It could very well be that the match I am looking for is between two reads, or over several reads. If the regex library is not design for this I will have to repeat the regex query for the whole chunk of incomming data every time, thus wasting a lot of resources.

Best regards

Jardar
Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg »

For my wxLexer I was also looking into wxRegExp the problem is that the regular expression mechanism works on a wxString or buffer. You can never predict how many chars the regular expression will need, so your only solution is reading it all in, or accepting a max block size for the regular expression to work on. If your parser works on lines only, simply read one line on a wxTextInputStream and feed that to the wxRegExp mechanism.

Ideally the regexp would also accept streams it is not that hard to implement. All they have to do is add a function that requests a no. of bytes when needed, or tyhey can make it transparent by the operator[] when an index in the buffer is requested that is not there, the underlying stream reads more data if needed.

However it's not possible right now..

Hope this helps,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
utopico
Earned a small fee
Earned a small fee
Posts: 12
Joined: Thu Aug 03, 2006 11:30 am

Post by utopico »

Thank you!

Are there any other libraries that will do this for me?
geralds
I live to help wx-kind
I live to help wx-kind
Posts: 186
Joined: Tue Nov 01, 2005 9:22 am
Contact:

Post by geralds »

You could try the boost regex library. This example supports stream-based matching.
http://boost.cppll.jp/HEAD/libs/regex/d ... tches.html

Alternatively, pcre flags partial matches, so it should be possible to implement the functionality you need on top of pcre.
Post Reply