-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessdatastream.cpp
More file actions
131 lines (107 loc) · 4.07 KB
/
processdatastream.cpp
File metadata and controls
131 lines (107 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*****************************************************************************
* This file is part of kdev-rust *
* Copyright (c) 2016 Michal Srb <michalsrb@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#include "processdatastream.h"
namespace Rust
{
ProcessDataStream::ProcessDataStream(QProcess& process)
: m_process(process)
, m_dataStream(&process)
{
m_dataStream.setByteOrder(QDataStream::LittleEndian);
}
void ProcessDataStream::waitForBytes(qint64 bytes)
{
while (m_process.bytesAvailable() < bytes) {
if (!m_process.waitForReadyRead(-1))
throw Exception("Process quited unexpectedly.");
}
}
quint8 ProcessDataStream::readUint8()
{
waitForBytes(sizeof(quint8));
quint8 value;
m_dataStream >> value;
return value;
}
quint32 ProcessDataStream::readUint32()
{
waitForBytes(sizeof(quint32));
quint32 value;
m_dataStream >> value;
return value;
}
quint64 ProcessDataStream::readUint64()
{
waitForBytes(sizeof(quint64));
quint64 value;
m_dataStream >> value;
return value;
}
QString ProcessDataStream::readString()
{
quint32 str_len = readUint32();
if (str_len == 0)
return QString();
m_scratchBuffer.resize(str_len); // TODO: Verify that this doesn't resize the buffer down if it is bigger.
waitForBytes(str_len);
if ((quint32) m_dataStream.readRawData(m_scratchBuffer.data(), str_len) != str_len)
throw Exception("Data stream didn't contain whole name.");
return QString::fromUtf8(m_scratchBuffer.data(), str_len);
}
KDevelop::IndexedString ProcessDataStream::readIndexedString()
{
quint32 name_len = readUint32();
if (name_len == 0)
throw Exception("Got zero length name while expecting non-zero.");
m_scratchBuffer.resize(name_len); // TODO: Verify that this doesn't resize the buffer down if it is bigger.
waitForBytes(name_len);
if ((quint32) m_dataStream.readRawData(m_scratchBuffer.data(), name_len) != name_len)
throw Exception("Data stream didn't contain whole name.");
return KDevelop::IndexedString(m_scratchBuffer.data(), name_len);
}
void ProcessDataStream::writeUint8(quint8 value)
{
m_dataStream << value;
}
void ProcessDataStream::writeUint32(quint32 value)
{
m_dataStream << value;
}
void ProcessDataStream::writeIndexedString(KDevelop::IndexedString indexedString)
{
int length = indexedString.length();
m_dataStream << (quint32) length;
m_dataStream.writeRawData(indexedString.c_str(), length);
}
void ProcessDataStream::writeByteArray(const QByteArray& byteArray)
{
m_dataStream << (quint32) byteArray.length();
m_dataStream.writeRawData(byteArray.data(), byteArray.length());
}
void ProcessDataStream::writeString(const QString& string)
{
writeByteArray(string.toUtf8());
}
void ProcessDataStream::writeStringList(const QStringList& stringList)
{
m_dataStream << (quint32) stringList.length();
for (const QString &string : stringList) {
writeString(string);
}
}
}