123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- /**
- * @file llsaleinfo.cpp
- * @brief
- *
- * $LicenseInfo:firstyear=2002&license=viewergpl$
- *
- * Copyright (c) 2002-2009, Linden Research, Inc.
- *
- * Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
- *
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at
- * http://secondlifegrid.net/programs/open_source/licensing/flossexception
- *
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
- *
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
- * $/LicenseInfo$
- */
- #include <iostream>
- #include "linden_common.h"
- #include "llsaleinfo.h"
- #include "llsdutil.h"
- #include "llmessage.h"
- // use this to avoid temporary object creation
- const LLSaleInfo LLSaleInfo::DEFAULT;
- ///----------------------------------------------------------------------------
- /// Local function declarations, constants, enums, and typedefs
- ///----------------------------------------------------------------------------
- const char* FOR_SALE_NAMES[] = {
- "not",
- "orig",
- "copy",
- "cntn"
- };
- ///----------------------------------------------------------------------------
- /// Class llsaleinfo
- ///----------------------------------------------------------------------------
- // Default constructor
- LLSaleInfo::LLSaleInfo()
- : mSaleType(LLSaleInfo::FS_NOT),
- mSalePrice(DEFAULT_PRICE)
- {
- }
- LLSaleInfo::LLSaleInfo(EForSale sale_type, S32 sale_price)
- : mSaleType(sale_type),
- mSalePrice(sale_price)
- {
- mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
- }
- U32 LLSaleInfo::getCRC32() const
- {
- U32 rv = (U32)mSalePrice;
- rv += (mSaleType * 0x07073096);
- return rv;
- }
- bool LLSaleInfo::exportLegacyStream(std::ostream& output_stream) const
- {
- output_stream << "\tsale_info\t0\n\t{\n";
- output_stream << "\t\tsale_type\t" << lookup(mSaleType) << "\n";
- output_stream << "\t\tsale_price\t" << mSalePrice << "\n";
- output_stream <<"\t}\n";
- return true;
- }
- LLSD LLSaleInfo::asLLSD() const
- {
- LLSD sd = LLSD();
- sd["sale_type"] = lookup(mSaleType);
- sd["sale_price"] = mSalePrice;
- return sd;
- }
- bool LLSaleInfo::fromLLSD(const LLSD& sd, bool& has_perm_mask, U32& perm_mask)
- {
- const char* w;
- if (sd["sale_type"].isString())
- {
- mSaleType = lookup(sd["sale_type"].asString().c_str());
- }
- else if (sd["sale_type"].isInteger())
- {
- S8 type = (U8)sd["sale_type"].asInteger();
- mSaleType = static_cast<LLSaleInfo::EForSale>(type);
- }
- mSalePrice = llclamp(sd["sale_price"].asInteger(), 0, S32_MAX);
- w = "perm_mask";
- if (sd.has(w))
- {
- has_perm_mask = true;
- perm_mask = ll_U32_from_sd(sd[w]);
- }
- return true;
- }
- bool LLSaleInfo::importLegacyStream(std::istream& input_stream,
- bool& has_perm_mask, U32& perm_mask)
- {
- has_perm_mask = false;
- // *NOTE: Changing the buffer size will require changing the scanf
- // calls below.
- char buffer[MAX_STRING];
- char keyword[MAX_STRING];
- char valuestr[MAX_STRING];
- bool success = true;
- keyword[0] = '\0';
- valuestr[0] = '\0';
- while (success && input_stream.good())
- {
- input_stream.getline(buffer, MAX_STRING);
- sscanf(buffer, " %254s %254s", keyword, valuestr);
- if (!keyword[0])
- {
- continue;
- }
- if (0 == strcmp("{",keyword))
- {
- continue;
- }
- if (0 == strcmp("}", keyword))
- {
- break;
- }
- else if (0 == strcmp("sale_type", keyword))
- {
- mSaleType = lookup(valuestr);
- }
- else if (0 == strcmp("sale_price", keyword))
- {
- sscanf(valuestr, "%d", &mSalePrice);
- mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
- }
- else if (!strcmp("perm_mask", keyword))
- {
- //llinfos << "found deprecated keyword perm_mask" << llendl;
- has_perm_mask = true;
- sscanf(valuestr, "%x", &perm_mask);
- }
- else
- {
- llwarns << "unknown keyword '" << keyword
- << "' in sale info import" << llendl;
- }
- }
- return success;
- }
- void LLSaleInfo::setSalePrice(S32 price)
- {
- mSalePrice = price;
- mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
- }
- LLSD LLSaleInfo::packMessage() const
- {
- LLSD result;
- U8 sale_type = static_cast<U8>(mSaleType);
- result["sale-type"] = (U8)sale_type;
- result["sale-price"] = (S32)mSalePrice;
- //result[_PREHASH_NextOwnerMask] = mNextOwnerPermMask;
- return result;
- }
- void LLSaleInfo::packMessage(LLMessageSystem* msg) const
- {
- U8 sale_type = static_cast<U8>(mSaleType);
- msg->addU8Fast(_PREHASH_SaleType, sale_type);
- msg->addS32Fast(_PREHASH_SalePrice, mSalePrice);
- //msg->addU32Fast(_PREHASH_NextOwnerMask, mNextOwnerPermMask);
- }
- void LLSaleInfo::unpackMessage(LLSD sales)
- {
- U8 sale_type = (U8)sales["sale-type"].asInteger();
- mSaleType = static_cast<EForSale>(sale_type);
- mSalePrice = (S32)sales["sale-price"].asInteger();
- mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
- //msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask);
- }
- void LLSaleInfo::unpackMessage(LLMessageSystem* msg, const char* block)
- {
- U8 sale_type;
- msg->getU8Fast(block, _PREHASH_SaleType, sale_type);
- mSaleType = static_cast<EForSale>(sale_type);
- msg->getS32Fast(block, _PREHASH_SalePrice, mSalePrice);
- mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
- //msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask);
- }
- void LLSaleInfo::unpackMultiMessage(LLMessageSystem* msg, const char* block,
- S32 block_num)
- {
- U8 sale_type;
- msg->getU8Fast(block, _PREHASH_SaleType, sale_type, block_num);
- mSaleType = static_cast<EForSale>(sale_type);
- msg->getS32Fast(block, _PREHASH_SalePrice, mSalePrice, block_num);
- mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
- //msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask, block_num);
- }
- LLSaleInfo::EForSale LLSaleInfo::lookup(const char* name)
- {
- for (S32 i = 0; i < FS_COUNT; ++i)
- {
- if (0 == strcmp(name, FOR_SALE_NAMES[i]))
- {
- // match
- return (EForSale)i;
- }
- }
- return FS_NOT;
- }
- const char* LLSaleInfo::lookup(EForSale type)
- {
- if (type >= 0 && type < FS_COUNT)
- {
- return FOR_SALE_NAMES[S32(type)];
- }
- else
- {
- return NULL;
- }
- }
- // Allow accumulation of sale info. The price of each is added,
- // conflict in sale type results in FS_NOT, and the permissions are
- // tightened.
- void LLSaleInfo::accumulate(const LLSaleInfo& sale_info)
- {
- if (mSaleType != sale_info.mSaleType)
- {
- mSaleType = FS_NOT;
- }
- mSalePrice += sale_info.mSalePrice;
- //mNextOwnerPermMask &= sale_info.mNextOwnerPermMask;
- }
- bool LLSaleInfo::operator==(const LLSaleInfo &rhs) const
- {
- return mSaleType == rhs.mSaleType && mSalePrice == rhs.mSalePrice;
- }
- bool LLSaleInfo::operator!=(const LLSaleInfo &rhs) const
- {
- return mSaleType != rhs.mSaleType || mSalePrice != rhs.mSalePrice;
- }
- ///----------------------------------------------------------------------------
- /// exported functions
- ///----------------------------------------------------------------------------
- static const std::string ST_TYPE_LABEL("sale_type");
- static const std::string ST_PRICE_LABEL("sale_price");
- LLSD ll_create_sd_from_sale_info(const LLSaleInfo& sale)
- {
- LLSD rv;
- const char* type = LLSaleInfo::lookup(sale.getSaleType());
- if (!type) type = LLSaleInfo::lookup(LLSaleInfo::FS_NOT);
- rv[ST_TYPE_LABEL] = type;
- rv[ST_PRICE_LABEL] = sale.getSalePrice();
- return rv;
- }
- LLSaleInfo ll_sale_info_from_sd(const LLSD& sd)
- {
- LLSaleInfo rv;
- rv.setSaleType(LLSaleInfo::lookup(sd[ST_TYPE_LABEL].asString().c_str()));
- rv.setSalePrice(llclamp((S32)sd[ST_PRICE_LABEL], 0, S32_MAX));
- return rv;
- }
|