[Interest] "QDataStream::version()" -- safe to assume 8-bit?

Alex Malyushytskyy alexmalvtk at gmail.com
Wed Oct 30 23:02:13 CET 2013


Typically it is a not magic number, it is a magic string, which solves most
problems.
I would recommend to keep it this way.
If you want to use number there, write it to string first. then write the
string.

Alex


On Sat, Oct 26, 2013 at 6:49 PM, Constantin Makshin <cmakshin at gmail.com>wrote:

> Since QDataStream versions form a contiguous sequence of integer numbers
> and new ones are added in [some] minor Qt updates, I'd say you have a LOT
> of time before these numbers stop fitting into 7-8 bits.
>
> As for the magic number issue, you may store it *after* the stream version
> number -- if the version is wrong (i.e. larger than your application
> understands), there's no need to worry about the magic number because the
> stream should be considered unreadable anyway.
> On Oct 27, 2013 3:44 AM, "Charley Bay" <charleyb123 at gmail.com> wrote:
>
>> The QDataStream docs give examples on serializing for compatible
>> formats across versions defined by QDataStream:
>>
>> <http://doc-snapshot.qt-project.org/qdoc/qdatastream.html#setVersion>
>>
>> Other examples on the web are similar, like:
>>
>> <http://doc.qt.digia.com/qq/qq05-achtung.html>
>>
>> The recommendation is something like:
>>
>>   //---------------
>>   //...
>>   QDataStream out(&file);
>>
>>   // Write a header with a "magic number" and a version
>>   out << (quint32)0xA0B0C0D0;
>>   out << (qint32)123;
>>   //---------------
>>
>> Then, reading in is:
>>
>>   //---------------
>>   //...-
>>   QDataStream in(&file);
>>
>>   // Read and check the header
>>   quint32 magic;
>>   in >> magic;
>>   if (magic != 0xA0B0C0D0)
>>     return XXX_BAD_FILE_FORMAT;
>>
>>   // Read the version
>>   qint32 version;
>>   in >> version;
>>   //---------------
>>
>> QUESTION #1:  Is it safe to write the 32-bit magic-number before
>> reading-and-setting the "QDataStream::version()" ?  It seems like
>> these examples are not cross-platform, as the 32-bit value may have
>> varying formats on different platforms.
>>
>> Other reading suggests that it is "safe" to assume
>> "QDataStream::version()" wholly fits into an 8-bit value, so this
>> should not be a problem with that value.
>>
>> QUESTION #2:  Is it forever safe to assume an 8-bit value is
>> sufficient to store "QDataStream::version()"?
>>
>> Because of my concern regarding "#1", it seems like four
>> "magic-quint8" values should be serialized instead of one
>> "magic-quint32" value.
>>
>> Or, am I just paranoid?
>>
>> --charley
>>
>> P.S.:   I'll close with the quote from the Digia page above because I
>> found it funny:
>>
>> "We have two choices, either to attack I/O now and get it over with,
>> or to postpone I/O until near the end.
>>
>> Neither prospect is very attractive."
>>   -- Donald E. Knuth
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
>>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131030/b41abcf5/attachment.html>


More information about the Interest mailing list