llsaleinfo.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * @file llsaleinfo.cpp
  3. * @brief
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include <iostream>
  33. #include "linden_common.h"
  34. #include "llsaleinfo.h"
  35. #include "llsdutil.h"
  36. #include "llmessage.h"
  37. // use this to avoid temporary object creation
  38. const LLSaleInfo LLSaleInfo::DEFAULT;
  39. ///----------------------------------------------------------------------------
  40. /// Local function declarations, constants, enums, and typedefs
  41. ///----------------------------------------------------------------------------
  42. const char* FOR_SALE_NAMES[] = {
  43. "not",
  44. "orig",
  45. "copy",
  46. "cntn"
  47. };
  48. ///----------------------------------------------------------------------------
  49. /// Class llsaleinfo
  50. ///----------------------------------------------------------------------------
  51. // Default constructor
  52. LLSaleInfo::LLSaleInfo()
  53. : mSaleType(LLSaleInfo::FS_NOT),
  54. mSalePrice(DEFAULT_PRICE)
  55. {
  56. }
  57. LLSaleInfo::LLSaleInfo(EForSale sale_type, S32 sale_price)
  58. : mSaleType(sale_type),
  59. mSalePrice(sale_price)
  60. {
  61. mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
  62. }
  63. U32 LLSaleInfo::getCRC32() const
  64. {
  65. U32 rv = (U32)mSalePrice;
  66. rv += (mSaleType * 0x07073096);
  67. return rv;
  68. }
  69. bool LLSaleInfo::exportLegacyStream(std::ostream& output_stream) const
  70. {
  71. output_stream << "\tsale_info\t0\n\t{\n";
  72. output_stream << "\t\tsale_type\t" << lookup(mSaleType) << "\n";
  73. output_stream << "\t\tsale_price\t" << mSalePrice << "\n";
  74. output_stream <<"\t}\n";
  75. return true;
  76. }
  77. LLSD LLSaleInfo::asLLSD() const
  78. {
  79. LLSD sd = LLSD();
  80. sd["sale_type"] = lookup(mSaleType);
  81. sd["sale_price"] = mSalePrice;
  82. return sd;
  83. }
  84. bool LLSaleInfo::fromLLSD(const LLSD& sd, bool& has_perm_mask, U32& perm_mask)
  85. {
  86. const char* w;
  87. if (sd["sale_type"].isString())
  88. {
  89. mSaleType = lookup(sd["sale_type"].asString().c_str());
  90. }
  91. else if (sd["sale_type"].isInteger())
  92. {
  93. S8 type = (U8)sd["sale_type"].asInteger();
  94. mSaleType = static_cast<LLSaleInfo::EForSale>(type);
  95. }
  96. mSalePrice = llclamp(sd["sale_price"].asInteger(), 0, S32_MAX);
  97. w = "perm_mask";
  98. if (sd.has(w))
  99. {
  100. has_perm_mask = true;
  101. perm_mask = ll_U32_from_sd(sd[w]);
  102. }
  103. return true;
  104. }
  105. bool LLSaleInfo::importLegacyStream(std::istream& input_stream,
  106. bool& has_perm_mask, U32& perm_mask)
  107. {
  108. has_perm_mask = false;
  109. // *NOTE: Changing the buffer size will require changing the scanf
  110. // calls below.
  111. char buffer[MAX_STRING];
  112. char keyword[MAX_STRING];
  113. char valuestr[MAX_STRING];
  114. bool success = true;
  115. keyword[0] = '\0';
  116. valuestr[0] = '\0';
  117. while (success && input_stream.good())
  118. {
  119. input_stream.getline(buffer, MAX_STRING);
  120. sscanf(buffer, " %254s %254s", keyword, valuestr);
  121. if (!keyword[0])
  122. {
  123. continue;
  124. }
  125. if (0 == strcmp("{",keyword))
  126. {
  127. continue;
  128. }
  129. if (0 == strcmp("}", keyword))
  130. {
  131. break;
  132. }
  133. else if (0 == strcmp("sale_type", keyword))
  134. {
  135. mSaleType = lookup(valuestr);
  136. }
  137. else if (0 == strcmp("sale_price", keyword))
  138. {
  139. sscanf(valuestr, "%d", &mSalePrice);
  140. mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
  141. }
  142. else if (!strcmp("perm_mask", keyword))
  143. {
  144. //llinfos << "found deprecated keyword perm_mask" << llendl;
  145. has_perm_mask = true;
  146. sscanf(valuestr, "%x", &perm_mask);
  147. }
  148. else
  149. {
  150. llwarns << "unknown keyword '" << keyword
  151. << "' in sale info import" << llendl;
  152. }
  153. }
  154. return success;
  155. }
  156. void LLSaleInfo::setSalePrice(S32 price)
  157. {
  158. mSalePrice = price;
  159. mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
  160. }
  161. LLSD LLSaleInfo::packMessage() const
  162. {
  163. LLSD result;
  164. U8 sale_type = static_cast<U8>(mSaleType);
  165. result["sale-type"] = (U8)sale_type;
  166. result["sale-price"] = (S32)mSalePrice;
  167. //result[_PREHASH_NextOwnerMask] = mNextOwnerPermMask;
  168. return result;
  169. }
  170. void LLSaleInfo::packMessage(LLMessageSystem* msg) const
  171. {
  172. U8 sale_type = static_cast<U8>(mSaleType);
  173. msg->addU8Fast(_PREHASH_SaleType, sale_type);
  174. msg->addS32Fast(_PREHASH_SalePrice, mSalePrice);
  175. //msg->addU32Fast(_PREHASH_NextOwnerMask, mNextOwnerPermMask);
  176. }
  177. void LLSaleInfo::unpackMessage(LLSD sales)
  178. {
  179. U8 sale_type = (U8)sales["sale-type"].asInteger();
  180. mSaleType = static_cast<EForSale>(sale_type);
  181. mSalePrice = (S32)sales["sale-price"].asInteger();
  182. mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
  183. //msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask);
  184. }
  185. void LLSaleInfo::unpackMessage(LLMessageSystem* msg, const char* block)
  186. {
  187. U8 sale_type;
  188. msg->getU8Fast(block, _PREHASH_SaleType, sale_type);
  189. mSaleType = static_cast<EForSale>(sale_type);
  190. msg->getS32Fast(block, _PREHASH_SalePrice, mSalePrice);
  191. mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
  192. //msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask);
  193. }
  194. void LLSaleInfo::unpackMultiMessage(LLMessageSystem* msg, const char* block,
  195. S32 block_num)
  196. {
  197. U8 sale_type;
  198. msg->getU8Fast(block, _PREHASH_SaleType, sale_type, block_num);
  199. mSaleType = static_cast<EForSale>(sale_type);
  200. msg->getS32Fast(block, _PREHASH_SalePrice, mSalePrice, block_num);
  201. mSalePrice = llclamp(mSalePrice, 0, S32_MAX);
  202. //msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask, block_num);
  203. }
  204. LLSaleInfo::EForSale LLSaleInfo::lookup(const char* name)
  205. {
  206. for (S32 i = 0; i < FS_COUNT; ++i)
  207. {
  208. if (0 == strcmp(name, FOR_SALE_NAMES[i]))
  209. {
  210. // match
  211. return (EForSale)i;
  212. }
  213. }
  214. return FS_NOT;
  215. }
  216. const char* LLSaleInfo::lookup(EForSale type)
  217. {
  218. if (type >= 0 && type < FS_COUNT)
  219. {
  220. return FOR_SALE_NAMES[S32(type)];
  221. }
  222. else
  223. {
  224. return NULL;
  225. }
  226. }
  227. // Allow accumulation of sale info. The price of each is added,
  228. // conflict in sale type results in FS_NOT, and the permissions are
  229. // tightened.
  230. void LLSaleInfo::accumulate(const LLSaleInfo& sale_info)
  231. {
  232. if (mSaleType != sale_info.mSaleType)
  233. {
  234. mSaleType = FS_NOT;
  235. }
  236. mSalePrice += sale_info.mSalePrice;
  237. //mNextOwnerPermMask &= sale_info.mNextOwnerPermMask;
  238. }
  239. bool LLSaleInfo::operator==(const LLSaleInfo &rhs) const
  240. {
  241. return mSaleType == rhs.mSaleType && mSalePrice == rhs.mSalePrice;
  242. }
  243. bool LLSaleInfo::operator!=(const LLSaleInfo &rhs) const
  244. {
  245. return mSaleType != rhs.mSaleType || mSalePrice != rhs.mSalePrice;
  246. }
  247. ///----------------------------------------------------------------------------
  248. /// exported functions
  249. ///----------------------------------------------------------------------------
  250. static const std::string ST_TYPE_LABEL("sale_type");
  251. static const std::string ST_PRICE_LABEL("sale_price");
  252. LLSD ll_create_sd_from_sale_info(const LLSaleInfo& sale)
  253. {
  254. LLSD rv;
  255. const char* type = LLSaleInfo::lookup(sale.getSaleType());
  256. if (!type) type = LLSaleInfo::lookup(LLSaleInfo::FS_NOT);
  257. rv[ST_TYPE_LABEL] = type;
  258. rv[ST_PRICE_LABEL] = sale.getSalePrice();
  259. return rv;
  260. }
  261. LLSaleInfo ll_sale_info_from_sd(const LLSD& sd)
  262. {
  263. LLSaleInfo rv;
  264. rv.setSaleType(LLSaleInfo::lookup(sd[ST_TYPE_LABEL].asString().c_str()));
  265. rv.setSalePrice(llclamp((S32)sd[ST_PRICE_LABEL], 0, S32_MAX));
  266. return rv;
  267. }