Results 1 to 4 of 4

Thread: QDataStream and Google Protocol Buffers

  1. #1
    Join Date
    Apr 2016
    Posts
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QDataStream and Google Protocol Buffers

    I have a QDataStream that contains both text messages and GPBs. The stream has 24 bytes that serve as a header for the next message. The header tells me if it's text or GPB, and how many bytes the message is. If it's text, I simply read that many bytes, then read in the next 24 bytes of the next header. If the next message is GPB, the header also tells me what message it is. I want to use the ParseFromIstream() method for that message, but the argument for that method is an fstream.

    So I guess my question is, is there a way to convert the QDataStream into an fstream? Better yet, does Qt have some way to parse data stored as a GPB?

    I suppose I could just toss the QDataStream and use an fstream, but I would rather not.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDataStream and Google Protocol Buffers

    You might consider extracting the char * array from the raw data and using it to construct a QByteArray which gives you a huge number of ways to iterate through and extract things from it. If it is non-binary, you could use QString which gives you similar functions as well as the ability to use QRegularExpression if you want to match patterns.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDataStream and Google Protocol Buffers

    A QDataStream is almost certainly not the tool for the job. Assuming that the I/O has to be Qt then get the data into a QByteArray directly from the underlying QIODevice (socket, file, whatever) and then use some generic C++ code to make an in-memory buffer for a std::istream sub-class. Something like this example from Stack Overflow:
    Qt Code:
    1. #include <streambuf>
    2. #include <istream>
    3.  
    4. struct membuf: std::streambuf {
    5. membuf(char const* base, size_t size) {
    6. char* p(const_cast<char*>(base));
    7. this->setg(p, p, p + size);
    8. }
    9. };
    10. struct imemstream: virtual membuf, std::istream {
    11. imemstream(char const* base, size_t size)
    12. : membuf(base, size)
    13. , std::istream(static_cast<std::streambuf*>(this)) {
    14. }
    15. };
    To copy to clipboard, switch view to plain text mode 
    In your Qt code:
    Qt Code:
    1. QByteArray buffer; // << contains the data from whatever source
    2. imemstream stream(buffer.constData(), buffer.size());
    3. // Pass &stream to the Google ParseFromIstream(istream* input) function.
    4. // Be careful of object lifetime: buffer has to stay in existence until the parse is complete
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2016
    Posts
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QDataStream and Google Protocol Buffers

    Thanks, looks like that will work for me. It's very helpful to know that QDataStream isn't the right object.

Similar Threads

  1. Linking protocol buffers library statically
    By yagabey in forum Qt Programming
    Replies: 4
    Last Post: 3rd January 2017, 22:36
  2. qTCPSocket buffers
    By nuliknol in forum Qt Programming
    Replies: 1
    Last Post: 15th August 2016, 19:52
  3. Replies: 1
    Last Post: 9th October 2015, 08:45
  4. Getting Device Interface Protocol (SCSI or ATA protocol)
    By Ryuuji in forum General Programming
    Replies: 1
    Last Post: 18th July 2013, 09:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.