Hi Atul,
I mean not to bother with creating a decorator just because of a single ASCII parsing function. We should keep the idea in mind and realize it when/if we need to do more parsing of ASCII data.
I pretty much mean what you have already done in the last patch + modify BinaryReader::readCString() and use it instead of indexOf(), which you agreed would be good refactoring ;-) :
QByteArray BinaryReader::readString (char terminator)
{
QByteArray res;
if (d.readOrigin == ReadFromStart) {
while (d.dataStart < d.dataEnd && *d.dataStart != terminator)
res.append (*d.dataStart++);
// Eigher a zero, or the end was reached, step past it:
d.dataStart++;
} else {
d.dataEnd--; // Move on the last byte, don't stay past it.
while (d.dataEnd >= d.dataStart && *d.dataEnd != terminator)
res.prepend (*d.dataEnd–);
}
return res;
}
The terminator character is not included in the returned QByteArray.
Regards,
Peter
atul wrote:Hey Peter
In bold…
Peter Dimov wrote:
Hi Atul,
What about that: instead of adding this new readInt() to BinaryReader, add a new parseInt() or extractInt() or readAsciiInt() as a private helper to BDecoder? This way BDecoder would perfectly build upon the functionality of BinaryReader and extend it.
I think I understand what you mean… There should be a wrapper on BinaryReader ( this is similar to Java i/o streams ) that reads text streams and get the delimited data….
If we add readInt() to BinaryReader then we should also later add readSingle() and readDouble() for example. Which semantics would these new functions have: binary or ASCII parsing? Wouldn’t it be better to keep the binary only semantics?
Yes mate I agree with you… what is needed is another class .. it is really a decorator
Another solution would be to add a read mode (binary, ASCII) to BinaryReader and use the State pattern for the implementation and… maybe change the name of BinaryReader ;-). I’d suggest though that we use the readString (char terminator = ‘’) solution until need for more ASCII parsing arises.
Did not get this completely - the last part of ASCII parsing ;-) Maybe you could elaborate a little ;-)
-cheers atul
