BinaryReader.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2006-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef UTILS__ENCODINGS__BINARY_READER_H
00024 #define UTILS__ENCODINGS__BINARY_READER_H
00025 
00026 namespace Utils {
00027 namespace Encodings {
00028 
00030 
00047 class BinaryReader
00048 {
00049     REFERENCE_OBJECT (BinaryReader)
00050 
00051 public:
00052     enum ByteOrder
00053     {
00054         BigEndian       = 0,
00055         LittleEndian    = 1
00056     };
00057 
00058     enum ReadOrigin
00059     {
00060         ReadFromStart   = 0,
00061         ReadFromEnd     = 1
00062     };
00063 
00064                     BinaryReader (const QByteArray &rawData, ByteOrder);
00065 
00066     uchar           lookAhead (const int count);
00067     bool            canRead (const int count);
00068 
00069     uchar           readByte();
00070     quint16         readUInt16();
00071     quint32         readUInt32();
00072     quint64         readUInt64();
00073     qint16          readInt16();
00074     qint32          readInt32();
00075     qint64          readInt64();
00076 
00077     QByteArray      readAll();
00078     QByteArray      readBytes (size_t count);
00079     QByteArray      readString (char terminator = '\0');
00080 
00081     QByteArray      getSharedBytes (size_t count);
00082 
00083     ByteOrder       byteOrder() const;
00084     ReadOrigin      readOrigin() const;
00085 
00086     void            setByteOrder (ByteOrder);
00087     void            setReadOrigin (ReadOrigin);
00088 
00089     bool            hasReadPastEnd() const;
00090     bool            hasReadAll() const;
00091 
00092 private:
00093     void            doRead (char *destination, size_t count);
00094     void            bumpPtrs(size_t count);
00095     void            copyBytes(const char *beg, const char *end, char *dest);
00096     const char *    getRangePtrs(size_t count);
00097 
00098 
00099     class Data
00100     {
00101     private:
00102         REFERENCE_OBJECT (Data)
00103 
00104     public:
00105         QByteArray  rawData;
00106         ByteOrder   byteOrder;
00107         ReadOrigin  readOrigin;
00108         bool        swapBytes;
00109         char        *dataStart;
00110         char        *dataEnd;
00111 
00112         Data() :    rawData(), byteOrder (BigEndian),
00113                     readOrigin (ReadFromStart), swapBytes (false),
00114                     dataStart (0), dataEnd (0)
00115         {}
00116     };
00117 
00118     Data    d;
00119 };
00120 
00122 
00125 inline BinaryReader::ByteOrder BinaryReader::byteOrder() const
00126 { return d.byteOrder; }
00127 
00129 
00132 inline BinaryReader::ReadOrigin BinaryReader::readOrigin() const
00133 { return d.readOrigin; }
00134 
00136 inline qint16 BinaryReader::readInt16()
00137 { return static_cast <qint16> (readUInt16()); }
00138 
00140 inline qint32 BinaryReader::readInt32()
00141 { return static_cast <qint32> (readUInt32()); }
00142 
00144 inline qint64 BinaryReader::readInt64()
00145 { return static_cast <qint64> (readUInt64()); }
00146 
00147 } // namespace Encodings
00148 } // namespace Utils
00149 
00150 #endif // UTILS__ENCODINGS__BINARY_READER_H