OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / cdb / bytearrayinputstream.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef BYTEARRAYINPUTSTREAM_H
35 #define BYTEARRAYINPUTSTREAM_H
36
37 #include <QtCore/QByteArray>
38 #include <QtCore/QString>
39
40 namespace Debugger {
41 namespace Internal {
42
43 class ByteArrayInputStream
44 {
45     Q_DISABLE_COPY(ByteArrayInputStream)
46 public:
47     typedef void (ModifierFunc)(ByteArrayInputStream &s);
48
49     explicit ByteArrayInputStream(QByteArray &ba);
50
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; }
55
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; }
60
61     // Stream a modifier by invoking it
62     ByteArrayInputStream &operator<<(ModifierFunc mf) { mf(*this); return *this; }
63
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 = ' ');
70
71 private:
72     template <class IntType> void appendInt(IntType i);
73
74     QByteArray &m_target;
75     int m_integerBase;
76     bool m_hexPrefix;
77     int m_width;
78 };
79
80 template <class IntType>
81 void ByteArrayInputStream::appendInt(IntType i)
82 {
83     const bool hexPrefix = m_integerBase == 16 && m_hexPrefix;
84     if (hexPrefix)
85         m_target.append("0x");
86     const QByteArray n = QByteArray::number(i, m_integerBase);
87     if (m_width > 0) {
88         int pad = m_width - n.size();
89         if (hexPrefix)
90             pad -= 2;
91         if (pad > 0)
92             m_target.append(QByteArray(pad, '0'));
93     }
94     m_target.append(n);
95 }
96
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);
103
104 // Bytearray parse helpers
105 QByteArray trimFront(QByteArray in);
106 QByteArray trimBack(QByteArray in);
107 QByteArray simplify(const QByteArray &inIn);
108
109 } // namespace Internal
110 } // namespace Debugger
111
112 #endif // BYTEARRAYINPUTSTREAM_H