1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #ifndef BYTEARRAYINPUTSTREAM_H
35 #define BYTEARRAYINPUTSTREAM_H
37 #include <QtCore/QByteArray>
38 #include <QtCore/QString>
43 class ByteArrayInputStream
45 Q_DISABLE_COPY(ByteArrayInputStream)
47 typedef void (ModifierFunc)(ByteArrayInputStream &s);
49 explicit ByteArrayInputStream(QByteArray &ba);
51 ByteArrayInputStream &operator<<(char a) { m_target.append(a); return *this; }
52 ByteArrayInputStream &operator<<(const QByteArray &a) { m_target.append(a); return *this; }
53 ByteArrayInputStream &operator<<(const char *a) { m_target.append(a); return *this; }
54 ByteArrayInputStream &operator<<(const QString &a) { m_target.append(a.toLatin1()); return *this; }
56 ByteArrayInputStream &operator<<(int i) { appendInt(i); return *this; }
57 ByteArrayInputStream &operator<<(unsigned i) { appendInt(i); return *this; }
58 ByteArrayInputStream &operator<<(quint64 i) { appendInt(i); return *this; }
59 ByteArrayInputStream &operator<<(qint64 i) { appendInt(i); return *this; }
61 // Stream a modifier by invoking it
62 ByteArrayInputStream &operator<<(ModifierFunc mf) { mf(*this); return *this; }
64 void setHexPrefix(bool hp) { m_hexPrefix = hp; }
65 bool hexPrefix() const { return m_hexPrefix; }
66 void setIntegerBase(int b) { m_integerBase = b; }
67 int integerBase() const { return m_integerBase; }
68 // Append a separator if required (target does not end with it)
69 void appendSeparator(char c = ' ');
72 template <class IntType> void appendInt(IntType i);
80 template <class IntType>
81 void ByteArrayInputStream::appendInt(IntType i)
83 const bool hexPrefix = m_integerBase == 16 && m_hexPrefix;
85 m_target.append("0x");
86 const QByteArray n = QByteArray::number(i, m_integerBase);
88 int pad = m_width - n.size();
92 m_target.append(QByteArray(pad, '0'));
97 // Streamable modifiers for ByteArrayInputStream
98 void hexPrefixOn(ByteArrayInputStream &bs);
99 void hexPrefixOff(ByteArrayInputStream &bs);
100 void hex(ByteArrayInputStream &bs);
101 void dec(ByteArrayInputStream &bs);
102 void blankSeparator(ByteArrayInputStream &bs);
104 // Bytearray parse helpers
105 QByteArray trimFront(QByteArray in);
106 QByteArray trimBack(QByteArray in);
107 QByteArray simplify(const QByteArray &inIn);
109 } // namespace Internal
110 } // namespace Debugger
112 #endif // BYTEARRAYINPUTSTREAM_H