diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llinventory/llsaleinfo.cpp | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llinventory/llsaleinfo.cpp')
-rw-r--r-- | linden/indra/llinventory/llsaleinfo.cpp | 375 |
1 files changed, 375 insertions, 0 deletions
diff --git a/linden/indra/llinventory/llsaleinfo.cpp b/linden/indra/llinventory/llsaleinfo.cpp new file mode 100644 index 0000000..b82bd18 --- /dev/null +++ b/linden/indra/llinventory/llsaleinfo.cpp | |||
@@ -0,0 +1,375 @@ | |||
1 | /** | ||
2 | * @file llsaleinfo.cpp | ||
3 | * @brief | ||
4 | * | ||
5 | * Copyright (c) 2002-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #include <iostream> | ||
29 | #include "linden_common.h" | ||
30 | |||
31 | #include "llsaleinfo.h" | ||
32 | |||
33 | #include "llerror.h" | ||
34 | #include "message.h" | ||
35 | #include "llsdutil.h" | ||
36 | |||
37 | // use this to avoid temporary object creation | ||
38 | const LLSaleInfo LLSaleInfo::DEFAULT; | ||
39 | |||
40 | ///---------------------------------------------------------------------------- | ||
41 | /// Local function declarations, constants, enums, and typedefs | ||
42 | ///---------------------------------------------------------------------------- | ||
43 | |||
44 | const char* FOR_SALE_NAMES[] = | ||
45 | { | ||
46 | "not", | ||
47 | "orig", | ||
48 | "copy", | ||
49 | "cntn" | ||
50 | }; | ||
51 | |||
52 | ///---------------------------------------------------------------------------- | ||
53 | /// Class llsaleinfo | ||
54 | ///---------------------------------------------------------------------------- | ||
55 | |||
56 | // Default constructor | ||
57 | LLSaleInfo::LLSaleInfo() : | ||
58 | mSaleType(LLSaleInfo::FS_NOT), | ||
59 | mSalePrice(DEFAULT_PRICE) | ||
60 | { | ||
61 | } | ||
62 | |||
63 | LLSaleInfo::LLSaleInfo(EForSale sale_type, S32 sale_price) : | ||
64 | mSaleType(sale_type), | ||
65 | mSalePrice(sale_price) | ||
66 | { | ||
67 | mSalePrice = llclamp(mSalePrice, 0, S32_MAX); | ||
68 | } | ||
69 | |||
70 | BOOL LLSaleInfo::isForSale() const | ||
71 | { | ||
72 | return (FS_NOT != mSaleType); | ||
73 | } | ||
74 | |||
75 | U32 LLSaleInfo::getCRC32() const | ||
76 | { | ||
77 | U32 rv = (U32)mSalePrice; | ||
78 | rv += (mSaleType * 0x07073096); | ||
79 | return rv; | ||
80 | } | ||
81 | |||
82 | |||
83 | BOOL LLSaleInfo::exportFile(FILE* fp) const | ||
84 | { | ||
85 | fprintf(fp, "\tsale_info\t0\n\t{\n"); | ||
86 | fprintf(fp, "\t\tsale_type\t%s\n", lookup(mSaleType)); | ||
87 | fprintf(fp, "\t\tsale_price\t%d\n", mSalePrice); | ||
88 | fprintf(fp,"\t}\n"); | ||
89 | return TRUE; | ||
90 | } | ||
91 | |||
92 | BOOL LLSaleInfo::exportLegacyStream(std::ostream& output_stream) const | ||
93 | { | ||
94 | output_stream << "\tsale_info\t0\n\t{\n"; | ||
95 | output_stream << "\t\tsale_type\t" << lookup(mSaleType) << "\n"; | ||
96 | output_stream << "\t\tsale_price\t" << mSalePrice << "\n"; | ||
97 | output_stream <<"\t}\n"; | ||
98 | return TRUE; | ||
99 | } | ||
100 | |||
101 | LLSD LLSaleInfo::asLLSD() const | ||
102 | { | ||
103 | LLSD sd = LLSD(); | ||
104 | sd["sale_type"] = lookup(mSaleType); | ||
105 | sd["sale_price"] = mSalePrice; | ||
106 | return sd; | ||
107 | } | ||
108 | |||
109 | bool LLSaleInfo::fromLLSD(LLSD& sd, BOOL& has_perm_mask, U32& perm_mask) | ||
110 | { | ||
111 | const char *w; | ||
112 | |||
113 | mSaleType = lookup(sd["sale_type"].asString().c_str()); | ||
114 | mSalePrice = llclamp(sd["sale_price"].asInteger(), 0, S32_MAX); | ||
115 | w = "perm_mask"; | ||
116 | if (sd.has(w)) | ||
117 | { | ||
118 | has_perm_mask = TRUE; | ||
119 | perm_mask = ll_U32_from_sd(sd[w]); | ||
120 | } | ||
121 | return true; | ||
122 | } | ||
123 | |||
124 | LLXMLNode *LLSaleInfo::exportFileXML() const | ||
125 | { | ||
126 | LLXMLNode *ret = new LLXMLNode("sale_info", FALSE); | ||
127 | LLString type_str = lookup(mSaleType); | ||
128 | ret->createChild("type", TRUE)->setStringValue(1, &type_str); | ||
129 | ret->createChild("price", TRUE)->setIntValue(1, &mSalePrice); | ||
130 | return ret; | ||
131 | } | ||
132 | |||
133 | BOOL LLSaleInfo::importXML(LLXMLNode* node) | ||
134 | { | ||
135 | BOOL success = FALSE; | ||
136 | if (node) | ||
137 | { | ||
138 | success = TRUE; | ||
139 | LLXMLNodePtr sub_node; | ||
140 | if (node->getChild("type", sub_node)) | ||
141 | { | ||
142 | mSaleType = lookup(sub_node->getValue().c_str()); | ||
143 | } | ||
144 | if (node->getChild("price", sub_node)) | ||
145 | { | ||
146 | success &= (1 == sub_node->getIntValue(1, &mSalePrice)); | ||
147 | } | ||
148 | if (!success) | ||
149 | { | ||
150 | lldebugs << "LLSaleInfo::importXML() failed for node named '" | ||
151 | << node->getName() << "'" << llendl; | ||
152 | } | ||
153 | } | ||
154 | return success; | ||
155 | } | ||
156 | |||
157 | BOOL LLSaleInfo::importFile(FILE* fp, BOOL& has_perm_mask, U32& perm_mask) | ||
158 | { | ||
159 | has_perm_mask = FALSE; | ||
160 | |||
161 | char buffer[MAX_STRING]; | ||
162 | char keyword[MAX_STRING]; | ||
163 | char valuestr[MAX_STRING]; | ||
164 | BOOL success = TRUE; | ||
165 | |||
166 | keyword[0] = '\0'; | ||
167 | valuestr[0] = '\0'; | ||
168 | while(success && (!feof(fp))) | ||
169 | { | ||
170 | fgets(buffer, MAX_STRING, fp); | ||
171 | sscanf(buffer, " %s %s", keyword, valuestr); | ||
172 | if(!keyword) | ||
173 | { | ||
174 | continue; | ||
175 | } | ||
176 | if(0 == strcmp("{",keyword)) | ||
177 | { | ||
178 | continue; | ||
179 | } | ||
180 | if(0 == strcmp("}", keyword)) | ||
181 | { | ||
182 | break; | ||
183 | } | ||
184 | else if(0 == strcmp("sale_type", keyword)) | ||
185 | { | ||
186 | mSaleType = lookup(valuestr); | ||
187 | } | ||
188 | else if(0 == strcmp("sale_price", keyword)) | ||
189 | { | ||
190 | sscanf(valuestr, "%d", &mSalePrice); | ||
191 | mSalePrice = llclamp(mSalePrice, 0, S32_MAX); | ||
192 | } | ||
193 | else if (!strcmp("perm_mask", keyword)) | ||
194 | { | ||
195 | //llinfos << "found deprecated keyword perm_mask" << llendl; | ||
196 | has_perm_mask = TRUE; | ||
197 | sscanf(valuestr, "%x", &perm_mask); | ||
198 | } | ||
199 | else | ||
200 | { | ||
201 | llwarns << "unknown keyword '" << keyword | ||
202 | << "' in sale info import" << llendl; | ||
203 | } | ||
204 | } | ||
205 | return success; | ||
206 | } | ||
207 | |||
208 | BOOL LLSaleInfo::importLegacyStream(std::istream& input_stream, BOOL& has_perm_mask, U32& perm_mask) | ||
209 | { | ||
210 | has_perm_mask = FALSE; | ||
211 | |||
212 | char buffer[MAX_STRING]; | ||
213 | char keyword[MAX_STRING]; | ||
214 | char valuestr[MAX_STRING]; | ||
215 | BOOL success = TRUE; | ||
216 | |||
217 | keyword[0] = '\0'; | ||
218 | valuestr[0] = '\0'; | ||
219 | while(success && input_stream.good()) | ||
220 | { | ||
221 | input_stream.getline(buffer, MAX_STRING); | ||
222 | sscanf(buffer, " %s %s", keyword, valuestr); | ||
223 | if(!keyword) | ||
224 | { | ||
225 | continue; | ||
226 | } | ||
227 | if(0 == strcmp("{",keyword)) | ||
228 | { | ||
229 | continue; | ||
230 | } | ||
231 | if(0 == strcmp("}", keyword)) | ||
232 | { | ||
233 | break; | ||
234 | } | ||
235 | else if(0 == strcmp("sale_type", keyword)) | ||
236 | { | ||
237 | mSaleType = lookup(valuestr); | ||
238 | } | ||
239 | else if(0 == strcmp("sale_price", keyword)) | ||
240 | { | ||
241 | sscanf(valuestr, "%d", &mSalePrice); | ||
242 | mSalePrice = llclamp(mSalePrice, 0, S32_MAX); | ||
243 | } | ||
244 | else if (!strcmp("perm_mask", keyword)) | ||
245 | { | ||
246 | //llinfos << "found deprecated keyword perm_mask" << llendl; | ||
247 | has_perm_mask = TRUE; | ||
248 | sscanf(valuestr, "%x", &perm_mask); | ||
249 | } | ||
250 | else | ||
251 | { | ||
252 | llwarns << "unknown keyword '" << keyword | ||
253 | << "' in sale info import" << llendl; | ||
254 | } | ||
255 | } | ||
256 | return success; | ||
257 | } | ||
258 | |||
259 | void LLSaleInfo::setSalePrice(S32 price) | ||
260 | { | ||
261 | mSalePrice = price; | ||
262 | mSalePrice = llclamp(mSalePrice, 0, S32_MAX); | ||
263 | } | ||
264 | |||
265 | void LLSaleInfo::packMessage(LLMessageSystem* msg) const | ||
266 | { | ||
267 | U8 sale_type = static_cast<U8>(mSaleType); | ||
268 | msg->addU8Fast(_PREHASH_SaleType, sale_type); | ||
269 | msg->addS32Fast(_PREHASH_SalePrice, mSalePrice); | ||
270 | //msg->addU32Fast(_PREHASH_NextOwnerMask, mNextOwnerPermMask); | ||
271 | } | ||
272 | |||
273 | void LLSaleInfo::unpackMessage(LLMessageSystem* msg, const char* block) | ||
274 | { | ||
275 | U8 sale_type; | ||
276 | msg->getU8Fast(block, _PREHASH_SaleType, sale_type); | ||
277 | mSaleType = static_cast<EForSale>(sale_type); | ||
278 | msg->getS32Fast(block, _PREHASH_SalePrice, mSalePrice); | ||
279 | mSalePrice = llclamp(mSalePrice, 0, S32_MAX); | ||
280 | //msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask); | ||
281 | } | ||
282 | |||
283 | void LLSaleInfo::unpackMultiMessage(LLMessageSystem* msg, const char* block, | ||
284 | S32 block_num) | ||
285 | { | ||
286 | U8 sale_type; | ||
287 | msg->getU8Fast(block, _PREHASH_SaleType, sale_type, block_num); | ||
288 | mSaleType = static_cast<EForSale>(sale_type); | ||
289 | msg->getS32Fast(block, _PREHASH_SalePrice, mSalePrice, block_num); | ||
290 | mSalePrice = llclamp(mSalePrice, 0, S32_MAX); | ||
291 | //msg->getU32Fast(block, _PREHASH_NextOwnerMask, mNextOwnerPermMask, block_num); | ||
292 | } | ||
293 | |||
294 | LLSaleInfo::EForSale LLSaleInfo::lookup(const char* name) | ||
295 | { | ||
296 | for(S32 i = 0; i < FS_COUNT; i++) | ||
297 | { | ||
298 | if(0 == strcmp(name, FOR_SALE_NAMES[i])) | ||
299 | { | ||
300 | // match | ||
301 | return (EForSale)i; | ||
302 | } | ||
303 | } | ||
304 | return FS_NOT; | ||
305 | } | ||
306 | |||
307 | const char* LLSaleInfo::lookup(EForSale type) | ||
308 | { | ||
309 | if((type >= 0) && (type < FS_COUNT)) | ||
310 | { | ||
311 | return FOR_SALE_NAMES[S32(type)]; | ||
312 | } | ||
313 | else | ||
314 | { | ||
315 | return NULL; | ||
316 | } | ||
317 | } | ||
318 | |||
319 | // Allow accumulation of sale info. The price of each is added, | ||
320 | // conflict in sale type results in FS_NOT, and the permissions are | ||
321 | // tightened. | ||
322 | void LLSaleInfo::accumulate(const LLSaleInfo& sale_info) | ||
323 | { | ||
324 | if(mSaleType != sale_info.mSaleType) | ||
325 | { | ||
326 | mSaleType = FS_NOT; | ||
327 | } | ||
328 | mSalePrice += sale_info.mSalePrice; | ||
329 | //mNextOwnerPermMask &= sale_info.mNextOwnerPermMask; | ||
330 | } | ||
331 | |||
332 | bool LLSaleInfo::operator==(const LLSaleInfo &rhs) const | ||
333 | { | ||
334 | return ( | ||
335 | (mSaleType == rhs.mSaleType) && | ||
336 | (mSalePrice == rhs.mSalePrice) | ||
337 | ); | ||
338 | } | ||
339 | |||
340 | bool LLSaleInfo::operator!=(const LLSaleInfo &rhs) const | ||
341 | { | ||
342 | return ( | ||
343 | (mSaleType != rhs.mSaleType) || | ||
344 | (mSalePrice != rhs.mSalePrice) | ||
345 | ); | ||
346 | } | ||
347 | |||
348 | |||
349 | ///---------------------------------------------------------------------------- | ||
350 | /// Local function definitions | ||
351 | ///---------------------------------------------------------------------------- | ||
352 | |||
353 | ///---------------------------------------------------------------------------- | ||
354 | /// exported functions | ||
355 | ///---------------------------------------------------------------------------- | ||
356 | static const std::string ST_TYPE_LABEL("sale_type"); | ||
357 | static const std::string ST_PRICE_LABEL("sale_price"); | ||
358 | |||
359 | LLSD ll_create_sd_from_sale_info(const LLSaleInfo& sale) | ||
360 | { | ||
361 | LLSD rv; | ||
362 | const char* type = LLSaleInfo::lookup(sale.getSaleType()); | ||
363 | if(!type) type = LLSaleInfo::lookup(LLSaleInfo::FS_NOT); | ||
364 | rv[ST_TYPE_LABEL] = type; | ||
365 | rv[ST_PRICE_LABEL] = sale.getSalePrice(); | ||
366 | return rv; | ||
367 | } | ||
368 | |||
369 | LLSaleInfo ll_sale_info_from_sd(const LLSD& sd) | ||
370 | { | ||
371 | LLSaleInfo rv; | ||
372 | rv.setSaleType(LLSaleInfo::lookup(sd[ST_TYPE_LABEL].asString().c_str())); | ||
373 | rv.setSalePrice(llclamp((S32)sd[ST_PRICE_LABEL], 0, S32_MAX)); | ||
374 | return rv; | ||
375 | } | ||