-
Notifications
You must be signed in to change notification settings - Fork 2
/
MessageStream.cpp
238 lines (196 loc) · 4.34 KB
/
MessageStream.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
Copyright (C) 2011 MoSync AB
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License,
version 2, as published by the Free Software Foundation.
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
/**
* @file MessageStream.h
* @author Mikael Kindborg
*
* Class for parsing a stream of messages from a WebView.
*/
#include <ma.h> // MoSync API
#include <maheap.h> // C memory allocation
#include <mastring.h> // C string functions
#include <mavsprintf.h> // C string functions
#include <mastdlib.h> // C string conversion functions
#include <conprint.h>
#include "MessageStream.h"
namespace Wormhole
{
/**
* Constructor.
*/
MessageStream::MessageStream(
NativeUI::WebView* webView,
MAHandle dataHandle)
{
mWebView = webView;
initialize(dataHandle);
}
/**
* Destructor.
*/
MessageStream::~MessageStream()
{
if (NULL != mData)
{
free(mData);
mData = NULL;
}
}
/**
* @return true if this is a valid message stream.
*/
bool MessageStream::isValid()
{
return NULL != mData;
}
/**
* Get the WebView widget associated with this message.
* @return Pointer to WebView object.
*/
NativeUI::WebView* MessageStream::getWebView()
{
return mWebView;
}
/**
* Get a pointer to the next string in the message stream.
* @param length Length of the string is returned in this
* parameter. Can be set to NULL (default value) in which
* case it is not used.
* @return Pointer to the string, NULL if there are no more
* strings in the message.
*/
const char* MessageStream::getNext(int* length)
{
char* p;
// If first call then start at beginning of data,
// else move forward one character.
if (NULL == mEnd)
{
// Move past the protocol specifier.
p = mData + 3;
}
else
{
p = mEnd + 1;
}
// lprintfln("value of p: %i", (int)p);
// Check boundary.
if (p - mData >= mDataSize)
{
// lprintfln("1: p - mData >= mDataSize");
return NULL;
}
// Get string length and updated pointer.
int len = xtoi(p, &p);
if (len == 0)
{
// lprintfln("len == 0");
return NULL;
}
// Check boundary.
if (p - mData >= mDataSize)
{
// lprintfln("2: p - mData >= mDataSize");
return NULL;
}
// Set length parameter.
if (NULL != length)
{
*length = len;
}
// lprintfln("String length: %i", len);
// Point p to start of string data.
mStart = p + 1;
// Point to end of string.
mEnd = mStart + len;
// Check boundary.
if (mEnd - mData >= mDataSize)
{
// lprintfln("3: mEnd - mData >= mDataSize");
return NULL;
}
// Zero terminate string.
*mEnd = 0;
// lprintfln("@@@ mStart: %s", mStart);
// Return string start.
return mStart;
}
/**
* TODO: Add error handling.
*/
int MessageStream::xtoi(char* s, char** newPos)
{
int firstChar = 33;
int lastChar = 126;
int base = lastChar - firstChar;
int n = 0;
int i;
for (i = 0; s[i] != ' '; ++i)
{
// Sanity check.
if (i > 4)
{
return 0;
}
n += raise(base, i) * (s[i] - firstChar);
}
*newPos = s + i;
return n;
}
/**
* TODO: Add error handling.
*/
int MessageStream::raise(int base, int e)
{
int pow = 1;
// Sanity check.
if (e > 4)
{
return 0;
}
while (e--)
{
pow *= base;
}
return pow;
}
/**
* Read data and initialise the stream.
*/
void MessageStream::initialize(MAHandle dataHandle)
{
mData = NULL;
// We must have data.
if (NULL == dataHandle)
{
return;
}
// Get length of the data, it is not zero terminated.
int dataSize = maGetDataSize(dataHandle);
// Allocate buffer for string data.
char* data = (char*) malloc(dataSize + 1);
// Get the data.
maReadData(dataHandle, data, 0, dataSize);
data[dataSize] = 0;
// Check that we have the "ms:" prefix.
if (data[0] != 'm') { return; }
if (data[1] != 's') { return; }
if (data[2] != ':') { return; }
mData = data;
mDataSize = dataSize;
mStart = NULL;
mEnd = NULL;
}
} // namespace