1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtBluetooth module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** 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 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QPRIVATELINEARBUFFER_P_H |
41 | #define QPRIVATELINEARBUFFER_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | // This is QIODevice's read buffer, optimised for read(), isEmpty() and getChar() |
55 | class QPrivateLinearBuffer |
56 | { |
57 | public: |
58 | QPrivateLinearBuffer() : len(0), first(0), buf(0), capacity(0) { |
59 | } |
60 | ~QPrivateLinearBuffer() { |
61 | delete [] buf; |
62 | } |
63 | void clear() { |
64 | first = buf; |
65 | len = 0; |
66 | } |
67 | int size() const { |
68 | return len; |
69 | } |
70 | bool isEmpty() const { |
71 | return len == 0; |
72 | } |
73 | void skip(int n) { |
74 | if (n >= len) { |
75 | clear(); |
76 | } else { |
77 | len -= n; |
78 | first += n; |
79 | } |
80 | } |
81 | int getChar() { |
82 | if (len == 0) |
83 | return -1; |
84 | int ch = uchar(*first); |
85 | len--; |
86 | first++; |
87 | return ch; |
88 | } |
89 | int read(char* target, int size) { |
90 | int r = qMin(a: size, b: len); |
91 | memcpy(dest: target, src: first, n: r); |
92 | len -= r; |
93 | first += r; |
94 | return r; |
95 | } |
96 | char* reserve(int size) { |
97 | makeSpace(required: size + len, where: freeSpaceAtEnd); |
98 | char* writePtr = first + len; |
99 | len += size; |
100 | return writePtr; |
101 | } |
102 | void chop(int size) { |
103 | if (size >= len) { |
104 | clear(); |
105 | } else { |
106 | len -= size; |
107 | } |
108 | } |
109 | QByteArray readAll() { |
110 | char* f = first; |
111 | int l = len; |
112 | clear(); |
113 | return QByteArray(f, l); |
114 | } |
115 | int readLine(char* target, int size) { |
116 | int r = qMin(a: size, b: len); |
117 | char* eol = static_cast<char*>(memchr(s: first, c: '\n', n: r)); |
118 | if (eol) |
119 | r = 1+(eol-first); |
120 | memcpy(dest: target, src: first, n: r); |
121 | len -= r; |
122 | first += r; |
123 | return int(r); |
124 | } |
125 | bool canReadLine() const { |
126 | return memchr(s: first, c: '\n', n: len); |
127 | } |
128 | void ungetChar(char c) { |
129 | if (first == buf) { |
130 | // underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer |
131 | makeSpace(required: len+1, where: freeSpaceAtStart); |
132 | } |
133 | first--; |
134 | len++; |
135 | *first = c; |
136 | } |
137 | void ungetBlock(const char* block, int size) { |
138 | if ((first - buf) < size) { |
139 | // underflow, the existing valid data needs to move to the end of the (potentially bigger) buffer |
140 | makeSpace(required: len + size, where: freeSpaceAtStart); |
141 | } |
142 | first -= size; |
143 | len += size; |
144 | memcpy(dest: first, src: block, n: size); |
145 | } |
146 | |
147 | private: |
148 | enum FreeSpacePos {freeSpaceAtStart, freeSpaceAtEnd}; |
149 | void makeSpace(size_t required, FreeSpacePos where) { |
150 | size_t newCapacity = qMax(a: capacity, b: size_t(QPRIVATELINEARBUFFER_BUFFERSIZE)); |
151 | while (newCapacity < required) |
152 | newCapacity *= 2; |
153 | const int moveOffset = (where == freeSpaceAtEnd) ? 0 : int(newCapacity) - len; |
154 | if (newCapacity > capacity) { |
155 | // allocate more space |
156 | char* newBuf = new char[newCapacity]; |
157 | memmove(dest: newBuf + moveOffset, src: first, n: len); |
158 | delete [] buf; |
159 | buf = newBuf; |
160 | capacity = newCapacity; |
161 | } else { |
162 | // shift any existing data to make space |
163 | memmove(dest: buf + moveOffset, src: first, n: len); |
164 | } |
165 | first = buf + moveOffset; |
166 | } |
167 | |
168 | private: |
169 | // length of the unread data |
170 | int len; |
171 | // start of the unread data |
172 | char* first; |
173 | // the allocated buffer |
174 | char* buf; |
175 | // allocated buffer size |
176 | size_t capacity; |
177 | }; |
178 | |
179 | #endif // QPRIVATELINEARBUFFER_P_H |
180 | |