PageRenderTime 29ms CodeModel.GetById 8ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llmessage/llsdmessagereader.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 344 lines | 236 code | 42 blank | 66 comment | 13 complexity | ff46b6628e58e17738ad110b2c43a149 MD5 | raw file
  1/** 
  2 * @file llsdmessagereader.cpp
  3 * @brief LLSDMessageReader class implementation.
  4 *
  5 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "linden_common.h"
 28
 29#include "llsdmessagereader.h"
 30
 31#include "llmessagebuilder.h"
 32#include "llsdmessagebuilder.h"
 33#include "llsdutil.h"
 34
 35#include "llsdutil_math.h"
 36#include "v3math.h"
 37#include "v4math.h"
 38#include "v3dmath.h"
 39#include "v2math.h"
 40#include "llquaternion.h"
 41#include "v4color.h"
 42
 43LLSDMessageReader::LLSDMessageReader() :
 44	mMessageName(NULL)
 45{
 46}
 47
 48//virtual 
 49LLSDMessageReader::~LLSDMessageReader()
 50{
 51}
 52
 53
 54LLSD getLLSD(const LLSD& input, const char* block, const char* var, S32 blocknum)
 55{
 56	// babbage: log error to llerrs if variable not found to mimic
 57	// LLTemplateMessageReader::getData behaviour
 58	if(NULL == block)
 59	{
 60		llerrs << "NULL block name" << llendl;
 61		return LLSD();
 62	}
 63	if(NULL == var)
 64	{
 65		llerrs << "NULL var name" << llendl;
 66		return LLSD();
 67	}
 68	if(! input[block].isArray())
 69	{
 70		// NOTE: babbage: need to return default for missing blocks to allow
 71		// backwards/forwards compatibility - handlers must cope with default
 72		// values.
 73		llwarns << "block " << block << " not found" << llendl;
 74		return LLSD();
 75	}
 76
 77	LLSD result = input[block][blocknum][var]; 
 78	if(result.isUndefined())
 79	{
 80		// NOTE: babbage: need to return default for missing vars to allow
 81		// backwards/forwards compatibility - handlers must cope with default
 82		// values.
 83		llwarns << "var " << var << " not found" << llendl;
 84	}
 85	return result;
 86}
 87
 88//virtual 
 89void LLSDMessageReader::getBinaryData(const char *block, const char *var, 
 90									  void *datap, S32 size, S32 blocknum, 
 91									  S32 max_size)
 92{
 93	std::vector<U8> data = getLLSD(mMessage, block, var, blocknum);
 94	S32 data_size = (S32)data.size();
 95
 96	if (size && data_size != size)
 97	{
 98		return;
 99	}
100	
101	if (max_size < data_size)
102	{
103		data_size = max_size;
104	}
105	
106	// Calls to memcpy will fail if data_size is not positive.
107	// Phoenix 2009-02-27
108	if(data_size <= 0)
109	{
110		return;
111	}
112	memcpy(datap, &(data[0]), data_size);
113}
114
115//virtual 
116void LLSDMessageReader::getBOOL(const char *block, const char *var, 
117								BOOL &data, 
118								S32 blocknum)
119{
120	data = getLLSD(mMessage, block, var, blocknum);
121}
122
123//virtual 
124void LLSDMessageReader::getS8(const char *block, const char *var, S8 &data, 
125					   S32 blocknum)
126{
127	data = getLLSD(mMessage, block, var, blocknum).asInteger();
128}
129
130//virtual 
131void LLSDMessageReader::getU8(const char *block, const char *var, U8 &data, 
132					   S32 blocknum)
133{
134	data = getLLSD(mMessage, block, var, blocknum).asInteger();
135}
136
137//virtual 
138void LLSDMessageReader::getS16(const char *block, const char *var, S16 &data, 
139						S32 blocknum)
140{
141	data = getLLSD(mMessage, block, var, blocknum).asInteger();
142}
143
144//virtual 
145void LLSDMessageReader::getU16(const char *block, const char *var, U16 &data, 
146						S32 blocknum)
147{
148	data = getLLSD(mMessage, block, var, blocknum).asInteger();
149}
150
151//virtual 
152void LLSDMessageReader::getS32(const char *block, const char *var, S32 &data, 
153						S32 blocknum)
154{
155	data = getLLSD(mMessage, block, var, blocknum);
156}
157
158//virtual 
159void LLSDMessageReader::getF32(const char *block, const char *var, F32 &data, 
160						S32 blocknum)
161{
162	data = (F32)getLLSD(mMessage, block, var, blocknum).asReal();
163}
164
165//virtual 
166void LLSDMessageReader::getU32(const char *block, const char *var, U32 &data, 
167						S32 blocknum)
168{
169	data = ll_U32_from_sd(getLLSD(mMessage, block, var, blocknum));
170}
171
172//virtual 
173void LLSDMessageReader::getU64(const char *block, const char *var,
174								U64 &data, S32 blocknum)
175{
176	data = ll_U64_from_sd(getLLSD(mMessage, block, var, blocknum));
177}
178
179//virtual 
180void LLSDMessageReader::getF64(const char *block, const char *var,
181								F64 &data, S32 blocknum)
182{
183	data = getLLSD(mMessage, block, var, blocknum);
184}
185
186//virtual 
187void LLSDMessageReader::getVector3(const char *block, const char *var, 
188							LLVector3 &vec, S32 blocknum)
189{
190	vec = ll_vector3_from_sd(getLLSD(mMessage, block, var, blocknum));
191}
192
193//virtual 
194void LLSDMessageReader::getVector4(const char *block, const char *var, 
195							LLVector4 &vec, S32 blocknum)
196{
197	vec = ll_vector4_from_sd(getLLSD(mMessage, block, var, blocknum));
198}
199
200//virtual 
201void LLSDMessageReader::getVector3d(const char *block, const char *var, 
202							 LLVector3d &vec, S32 blocknum)
203{
204	vec = ll_vector3d_from_sd(getLLSD(mMessage, block, var, blocknum));
205}
206
207//virtual 
208void LLSDMessageReader::getQuat(const char *block, const char *var,
209								 LLQuaternion &q, S32 blocknum)
210{
211	q = ll_quaternion_from_sd(getLLSD(mMessage, block, var, blocknum));
212}
213
214//virtual 
215void LLSDMessageReader::getUUID(const char *block, const char *var,
216								 LLUUID &uuid, S32 blocknum)
217{
218	uuid = getLLSD(mMessage, block, var, blocknum);
219}
220
221//virtual 
222void LLSDMessageReader::getIPAddr(const char *block, const char *var,
223								   U32 &ip, S32 blocknum)
224{
225	ip = ll_ipaddr_from_sd(getLLSD(mMessage, block, var, blocknum));
226}
227
228//virtual 
229void LLSDMessageReader::getIPPort(const char *block, const char *var,
230								   U16 &port, S32 blocknum)
231{
232	port = getLLSD(mMessage, block, var, blocknum).asInteger();
233}
234
235//virtual 
236void LLSDMessageReader::getString(const char *block, const char *var, 
237						   S32 buffer_size, char *buffer, S32 blocknum)
238{
239	if(buffer_size <= 0)
240	{
241		llwarns << "buffer_size <= 0" << llendl;
242		return;
243	}
244	std::string data = getLLSD(mMessage, block, var, blocknum);
245	S32 data_size = data.size();
246	if (data_size >= buffer_size)
247	{
248		data_size = buffer_size - 1;
249	}
250	memcpy(buffer, data.data(), data_size);
251	buffer[data_size] = '\0';
252}
253
254//virtual 
255void LLSDMessageReader::getString(const char *block, const char *var, 
256						   std::string& outstr, S32 blocknum)
257{
258	outstr = getLLSD(mMessage, block, var, blocknum).asString();
259}
260
261//virtual 
262S32	LLSDMessageReader::getNumberOfBlocks(const char *blockname)
263{
264	return mMessage[blockname].size();
265}
266
267S32 getElementSize(const LLSD& llsd)
268{
269	LLSD::Type type = llsd.type();
270	switch(type)
271	{
272	case LLSD::TypeBoolean:
273		return sizeof(bool);
274	case LLSD::TypeInteger:
275		return sizeof(S32);
276	case LLSD::TypeReal:
277		return sizeof(F64);
278	case LLSD::TypeString:
279		return llsd.asString().size();
280	case LLSD::TypeUUID:
281		return sizeof(LLUUID);
282	case LLSD::TypeDate:
283		return sizeof(LLDate);
284	case LLSD::TypeURI:
285		return sizeof(LLURI);
286	case LLSD::TypeBinary:
287	{
288		std::vector<U8> data = llsd;
289		return data.size() * sizeof(U8);
290	}
291	case LLSD::TypeMap:
292	case LLSD::TypeArray:
293	case LLSD::TypeUndefined:
294	default:                        // TypeLLSDTypeEnd, TypeLLSDNumTypes, etc.
295		return 0;
296	}
297	//return 0;
298}
299
300//virtual 
301//Mainly used to find size of binary block of data
302S32	LLSDMessageReader::getSize(const char *blockname, const char *varname)
303{
304	return getElementSize(mMessage[blockname][0][varname]);
305}
306
307
308//virtual 
309S32	LLSDMessageReader::getSize(const char *blockname, S32 blocknum, 
310							   const char *varname)
311{
312	return getElementSize(mMessage[blockname][blocknum][varname]);
313}
314
315//virtual 
316void LLSDMessageReader::clearMessage()
317{
318	mMessage = LLSD();
319}
320
321//virtual 
322const char* LLSDMessageReader::getMessageName() const
323{
324	return mMessageName;
325}
326
327// virtual 
328S32 LLSDMessageReader::getMessageSize() const
329{
330	return 0;
331}
332
333//virtual 
334void LLSDMessageReader::copyToBuilder(LLMessageBuilder& builder) const
335{
336	builder.copyFromLLSD(mMessage);
337}
338
339void LLSDMessageReader::setMessage(const char* name, const LLSD& message)
340{
341	mMessageName = name;
342	// TODO: Validate
343	mMessage = message;
344}