PageRenderTime 54ms CodeModel.GetById 17ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llinventory/llsaleinfo.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 388 lines | 287 code | 44 blank | 57 comment | 47 complexity | 27936088022041fd5f6c419b9f2705d7 MD5 | raw file
  1/** 
  2 * @file llsaleinfo.cpp
  3 * @brief 
  4 *
  5 * $LicenseInfo:firstyear=2002&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 <iostream>
 28#include "linden_common.h"
 29
 30#include "llsaleinfo.h"
 31
 32#include "llerror.h"
 33#include "message.h"
 34#include "llsdutil.h"
 35
 36// use this to avoid temporary object creation
 37const LLSaleInfo LLSaleInfo::DEFAULT;
 38
 39///----------------------------------------------------------------------------
 40/// Local function declarations, constants, enums, and typedefs
 41///----------------------------------------------------------------------------
 42
 43const char* FOR_SALE_NAMES[] =
 44{ 
 45	"not",
 46	"orig",
 47	"copy",
 48	"cntn"
 49};
 50
 51///----------------------------------------------------------------------------
 52/// Class llsaleinfo
 53///----------------------------------------------------------------------------
 54
 55// Default constructor
 56LLSaleInfo::LLSaleInfo() :
 57	mSaleType(LLSaleInfo::FS_NOT),
 58	mSalePrice(DEFAULT_PRICE)
 59{
 60}
 61
 62LLSaleInfo::LLSaleInfo(EForSale sale_type, S32 sale_price) :
 63	mSaleType(sale_type),
 64	mSalePrice(sale_price)
 65{
 66	mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
 67}
 68
 69BOOL LLSaleInfo::isForSale() const
 70{
 71	return (FS_NOT != mSaleType);
 72}
 73
 74U32 LLSaleInfo::getCRC32() const
 75{
 76	U32 rv = (U32)mSalePrice;
 77	rv += (mSaleType * 0x07073096);
 78	return rv;
 79}
 80
 81
 82BOOL LLSaleInfo::exportFile(LLFILE* fp) const
 83{
 84	fprintf(fp, "\tsale_info\t0\n\t{\n");
 85	fprintf(fp, "\t\tsale_type\t%s\n", lookup(mSaleType));
 86	fprintf(fp, "\t\tsale_price\t%d\n", mSalePrice);
 87	fprintf(fp,"\t}\n");
 88	return TRUE;
 89}
 90
 91BOOL LLSaleInfo::exportLegacyStream(std::ostream& output_stream) const
 92{
 93	output_stream << "\tsale_info\t0\n\t{\n";
 94	output_stream << "\t\tsale_type\t" << lookup(mSaleType) << "\n";
 95	output_stream << "\t\tsale_price\t" << mSalePrice << "\n";
 96	output_stream <<"\t}\n";
 97	return TRUE;
 98}
 99
100LLSD LLSaleInfo::asLLSD() const
101{
102	LLSD sd = LLSD();
103	sd["sale_type"] = lookup(mSaleType);
104	sd["sale_price"] = mSalePrice;
105	return sd;
106}
107
108bool LLSaleInfo::fromLLSD(const LLSD& sd, BOOL& has_perm_mask, U32& perm_mask)
109{
110	const char *w;
111
112	if (sd["sale_type"].isString())
113	{
114		mSaleType = lookup(sd["sale_type"].asString().c_str());
115	}
116	else if(sd["sale_type"].isInteger())
117	{
118		S8 type = (U8)sd["sale_type"].asInteger();
119		mSaleType = static_cast<LLSaleInfo::EForSale>(type);
120	}
121
122	mSalePrice = llclamp(sd["sale_price"].asInteger(), 0, S32_MAX);
123	w = "perm_mask";
124	if (sd.has(w))
125	{
126		has_perm_mask = TRUE;
127		perm_mask = ll_U32_from_sd(sd[w]);
128	}
129	return true;
130}
131
132// Deleted LLSaleInfo::exportFileXML() and LLSaleInfo::importXML()
133// because I can't find any non-test code references to it. 2009-05-04 JC
134
135BOOL LLSaleInfo::importFile(LLFILE* fp, BOOL& has_perm_mask, U32& perm_mask)
136{
137	has_perm_mask = FALSE;
138
139	// *NOTE: Changing the buffer size will require changing the scanf
140	// calls below.
141	char buffer[MAX_STRING];	/* Flawfinder: ignore */
142	char keyword[MAX_STRING];	/* Flawfinder: ignore */
143	char valuestr[MAX_STRING];	/* Flawfinder: ignore */
144	BOOL success = TRUE;
145
146	keyword[0] = '\0';
147	valuestr[0] = '\0';
148	while(success && (!feof(fp)))
149	{
150		if (fgets(buffer, MAX_STRING, fp) == NULL)
151		{
152			buffer[0] = '\0';
153		}
154
155		sscanf(	/* Flawfinder: ignore */
156			buffer,
157			" %254s %254s",
158			keyword, valuestr);
159		if(!keyword[0])
160		{
161			continue;
162		}
163		if(0 == strcmp("{",keyword))
164		{
165			continue;
166		}
167		if(0 == strcmp("}", keyword))
168		{
169			break;
170		}
171		else if(0 == strcmp("sale_type", keyword))
172		{
173			mSaleType = lookup(valuestr);
174		}
175		else if(0 == strcmp("sale_price", keyword))
176		{
177			sscanf(valuestr, "%d", &mSalePrice);
178			mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
179		}
180		else if (!strcmp("perm_mask", keyword))
181		{
182			//llinfos << "found deprecated keyword perm_mask" << llendl;
183			has_perm_mask = TRUE;
184			sscanf(valuestr, "%x", &perm_mask);
185		}
186		else
187		{
188			llwarns << "unknown keyword '" << keyword
189					<< "' in sale info import" << llendl;
190		}
191	}
192	return success;
193}
194
195BOOL LLSaleInfo::importLegacyStream(std::istream& input_stream, BOOL& has_perm_mask, U32& perm_mask)
196{
197	has_perm_mask = FALSE;
198
199	// *NOTE: Changing the buffer size will require changing the scanf
200	// calls below.
201	char buffer[MAX_STRING];	/* Flawfinder: ignore */
202	char keyword[MAX_STRING];	/* Flawfinder: ignore */
203	char valuestr[MAX_STRING];	/* Flawfinder: ignore */
204	BOOL success = TRUE;
205
206	keyword[0] = '\0';
207	valuestr[0] = '\0';
208	while(success && input_stream.good())
209	{
210		input_stream.getline(buffer, MAX_STRING);
211		sscanf(	/* Flawfinder: ignore */
212			buffer,
213			" %254s %254s",
214			keyword, valuestr);
215		if(!keyword[0])
216		{
217			continue;
218		}
219		if(0 == strcmp("{",keyword))
220		{
221			continue;
222		}
223		if(0 == strcmp("}", keyword))
224		{
225			break;
226		}
227		else if(0 == strcmp("sale_type", keyword))
228		{
229			mSaleType = lookup(valuestr);
230		}
231		else if(0 == strcmp("sale_price", keyword))
232		{
233			sscanf(valuestr, "%d", &mSalePrice);
234			mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
235		}
236		else if (!strcmp("perm_mask", keyword))
237		{
238			//llinfos << "found deprecated keyword perm_mask" << llendl;
239			has_perm_mask = TRUE;
240			sscanf(valuestr, "%x", &perm_mask);
241		}
242		else
243		{
244			llwarns << "unknown keyword '" << keyword
245					<< "' in sale info import" << llendl;
246		}
247	}
248	return success;
249}
250
251void LLSaleInfo::setSalePrice(S32 price)
252{
253	mSalePrice = price;
254	mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
255}
256
257LLSD LLSaleInfo::packMessage() const
258{
259	LLSD result;
260
261	U8 sale_type = static_cast<U8>(mSaleType);
262	result["sale-type"]		= (U8)sale_type;
263	result["sale-price"]	= (S32)mSalePrice;
264	//result[_PREHASH_NextOwnerMask] = mNextOwnerPermMask;
265	return result;
266}
267
268void LLSaleInfo::packMessage(LLMessageSystem* msg) const
269{
270	U8 sale_type = static_cast<U8>(mSaleType);
271	msg->addU8Fast(_PREHASH_SaleType, sale_type);
272	msg->addS32Fast(_PREHASH_SalePrice, mSalePrice);
273	//msg->addU32Fast(_PREHASH_NextOwnerMask, mNextOwnerPermMask);
274}
275
276void LLSaleInfo::unpackMessage(LLSD sales)
277{
278	U8 sale_type = (U8)sales["sale-type"].asInteger();
279	mSaleType = static_cast<EForSale>(sale_type);
280
281	mSalePrice = (S32)sales["sale-price"].asInteger();
282	mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
283	//msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask);
284}
285
286void LLSaleInfo::unpackMessage(LLMessageSystem* msg, const char* block)
287{
288	U8 sale_type;
289	msg->getU8Fast(block, _PREHASH_SaleType, sale_type);
290	mSaleType = static_cast<EForSale>(sale_type);
291	msg->getS32Fast(block, _PREHASH_SalePrice, mSalePrice);
292	mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
293	//msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask);
294}
295
296void LLSaleInfo::unpackMultiMessage(LLMessageSystem* msg, const char* block,
297									S32 block_num)
298{
299	U8 sale_type;
300	msg->getU8Fast(block, _PREHASH_SaleType, sale_type, block_num);
301	mSaleType = static_cast<EForSale>(sale_type);
302	msg->getS32Fast(block, _PREHASH_SalePrice, mSalePrice, block_num);
303	mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
304	//msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask, block_num);
305}
306
307LLSaleInfo::EForSale LLSaleInfo::lookup(const char* name)
308{
309	for(S32 i = 0; i < FS_COUNT; i++)
310	{
311		if(0 == strcmp(name, FOR_SALE_NAMES[i]))
312		{
313			// match
314			return (EForSale)i;
315		}
316	}
317	return FS_NOT;
318}
319
320const char* LLSaleInfo::lookup(EForSale type)
321{
322	if((type >= 0) && (type < FS_COUNT))
323	{
324		return FOR_SALE_NAMES[S32(type)];
325	}
326	else
327	{
328		return NULL;
329	}
330}
331
332// Allow accumulation of sale info. The price of each is added,
333// conflict in sale type results in FS_NOT, and the permissions are
334// tightened.
335void LLSaleInfo::accumulate(const LLSaleInfo& sale_info)
336{
337	if(mSaleType != sale_info.mSaleType)
338	{
339		mSaleType = FS_NOT;
340	}
341	mSalePrice += sale_info.mSalePrice;
342	//mNextOwnerPermMask &= sale_info.mNextOwnerPermMask;
343}
344
345bool LLSaleInfo::operator==(const LLSaleInfo &rhs) const
346{
347	return (
348		(mSaleType == rhs.mSaleType) &&
349		(mSalePrice == rhs.mSalePrice) 
350		);
351}
352
353bool LLSaleInfo::operator!=(const LLSaleInfo &rhs) const
354{
355	return (
356		(mSaleType != rhs.mSaleType) ||
357		(mSalePrice != rhs.mSalePrice) 
358		);
359}
360
361
362///----------------------------------------------------------------------------
363/// Local function definitions
364///----------------------------------------------------------------------------
365
366///----------------------------------------------------------------------------
367/// exported functions
368///----------------------------------------------------------------------------
369static const std::string ST_TYPE_LABEL("sale_type");
370static const std::string ST_PRICE_LABEL("sale_price");
371
372LLSD ll_create_sd_from_sale_info(const LLSaleInfo& sale)
373{
374	LLSD rv;
375	const char* type = LLSaleInfo::lookup(sale.getSaleType());
376	if(!type) type = LLSaleInfo::lookup(LLSaleInfo::FS_NOT);
377	rv[ST_TYPE_LABEL] = type;
378	rv[ST_PRICE_LABEL] = sale.getSalePrice();
379	return rv;
380}
381
382LLSaleInfo ll_sale_info_from_sd(const LLSD& sd)
383{
384	LLSaleInfo rv;
385	rv.setSaleType(LLSaleInfo::lookup(sd[ST_TYPE_LABEL].asString().c_str()));
386	rv.setSalePrice(llclamp((S32)sd[ST_PRICE_LABEL], 0, S32_MAX));
387	return rv;
388}