PageRenderTime 37ms CodeModel.GetById 17ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llinventory/llnotecard.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 289 lines | 193 code | 45 blank | 51 comment | 32 complexity | 923e004365b5a78215b561c53b75d83c MD5 | raw file
  1/** 
  2 * @file llnotecard.cpp
  3 * @brief LLNotecard class definition
  4 *
  5 * $LicenseInfo:firstyear=2006&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#include "llnotecard.h"
 29#include "llstreamtools.h"
 30
 31LLNotecard::LLNotecard(S32 max_text)
 32	: mMaxText(max_text),
 33	  mVersion(0),
 34	  mEmbeddedVersion(0)
 35{
 36}
 37
 38LLNotecard::~LLNotecard()
 39{
 40}
 41
 42bool LLNotecard::importEmbeddedItemsStream(std::istream& str)
 43{
 44	// Version 1 format:
 45	//		LLEmbeddedItems version 1
 46	//		{
 47	//			count <number of entries being used and not deleted>
 48	//			{
 49	//				ext char index <index>
 50	//				<InventoryItem chunk>
 51	//			}
 52	//		}
 53	
 54	S32 i;
 55	S32 count = 0;
 56
 57	str >> std::ws >> "LLEmbeddedItems version" >> mEmbeddedVersion >> "\n";
 58	if (str.fail())
 59	{
 60		llwarns << "Invalid Linden text file header" << llendl;
 61		goto import_file_failed;
 62	}
 63
 64	if( 1 != mEmbeddedVersion )
 65	{
 66		llwarns << "Invalid LLEmbeddedItems version: " << mEmbeddedVersion << llendl;
 67		goto import_file_failed;
 68	}
 69
 70	str >> std::ws >> "{\n";
 71	if(str.fail())
 72	{
 73		llwarns << "Invalid Linden text file format: missing {" << llendl;
 74		goto import_file_failed;
 75	}
 76
 77	str >> std::ws >> "count " >> count >> "\n";
 78	if(str.fail())
 79	{
 80		llwarns << "Invalid LLEmbeddedItems count" << llendl;
 81		goto import_file_failed;
 82	}
 83
 84	if((count < 0))
 85	{
 86		llwarns << "Invalid LLEmbeddedItems count value: " << count << llendl;
 87		goto import_file_failed;
 88	}
 89
 90	for(i = 0; i < count; i++)
 91	{
 92		str >> std::ws >> "{\n";
 93		if(str.fail())
 94		{
 95			llwarns << "Invalid LLEmbeddedItems file format: missing {" << llendl;
 96			goto import_file_failed;
 97		}
 98
 99		U32 index = 0;
100		str >> std::ws >> "ext char index " >> index >> "\n";
101		if(str.fail())
102		{
103			llwarns << "Invalid LLEmbeddedItems file format: missing ext char index" << llendl;
104			goto import_file_failed;
105		}
106
107		str >> std::ws >> "inv_item\t0\n";
108		if(str.fail())
109		{
110			llwarns << "Invalid LLEmbeddedItems file format: missing inv_item" << llendl;
111			goto import_file_failed;
112		}
113
114		LLPointer<LLInventoryItem> item = new LLInventoryItem;
115		if (!item->importLegacyStream(str))
116		{
117			llinfos << "notecard import failed" << llendl;
118			goto import_file_failed;
119		}		
120		mItems.push_back(item);
121
122		str >> std::ws >> "}\n";
123		if(str.fail())
124		{
125			llwarns << "Invalid LLEmbeddedItems file format: missing }" << llendl;
126			goto import_file_failed;
127		}
128	}
129
130	str >> std::ws >> "}\n";
131	if(str.fail())
132	{
133		llwarns << "Invalid LLEmbeddedItems file format: missing }" << llendl;
134		goto import_file_failed;
135	}
136
137	return true;
138
139	import_file_failed:
140	return false;
141}
142
143bool LLNotecard::importStream(std::istream& str)
144{
145	// Version 1 format:
146	//		Linden text version 1
147	//		{
148	//			<EmbeddedItemList chunk>
149	//			Text length
150	//			<ASCII text; 0x80 | index = embedded item>
151	//		}
152	
153	// Version 2 format: (NOTE: Imports identically to version 1)
154	//		Linden text version 2
155	//		{
156	//			<EmbeddedItemList chunk>
157	//			Text length
158	//			<UTF8 text; FIRST_EMBEDDED_CHAR + index = embedded item>
159	//		}
160	
161	str >> std::ws >> "Linden text version " >> mVersion >> "\n";
162	if(str.fail())
163	{
164		llwarns << "Invalid Linden text file header " << llendl;
165		return FALSE;
166	}
167
168	if( 1 != mVersion && 2 != mVersion)
169	{
170		llwarns << "Invalid Linden text file version: " << mVersion << llendl;
171		return FALSE;
172	}
173
174	str >> std::ws >> "{\n";
175	if(str.fail())
176	{
177		llwarns << "Invalid Linden text file format" << llendl;
178		return FALSE;
179	}
180
181	if(!importEmbeddedItemsStream(str))
182	{
183		return FALSE;
184	}
185
186	char line_buf[STD_STRING_BUF_SIZE];	/* Flawfinder: ignore */
187	str.getline(line_buf, STD_STRING_BUF_SIZE);
188	if(str.fail())
189	{
190		llwarns << "Invalid Linden text length field" << llendl;
191		return FALSE;
192	}
193	line_buf[STD_STRING_STR_LEN] = '\0';
194	
195	S32 text_len = 0;
196	if( 1 != sscanf(line_buf, "Text length %d", &text_len) )
197	{
198		llwarns << "Invalid Linden text length field" << llendl;
199		return FALSE;
200	}
201
202	if(text_len > mMaxText || text_len < 0)
203	{
204		llwarns << "Invalid Linden text length: " << text_len << llendl;
205		return FALSE;
206	}
207
208	BOOL success = TRUE;
209
210	char* text = new char[text_len + 1];
211	fullread(str, text, text_len);
212	if(str.fail())
213	{
214		llwarns << "Invalid Linden text: text shorter than text length: " << text_len << llendl;
215		success = FALSE;
216	}
217	text[text_len] = '\0';
218
219	if(success)
220	{
221		// Actually set the text
222		mText = std::string(text);
223	}
224
225	delete[] text;
226
227	return success;
228}
229
230////////////////////////////////////////////////////////////////////////////
231
232bool LLNotecard::exportEmbeddedItemsStream( std::ostream& out_stream )
233{
234	out_stream << "LLEmbeddedItems version 1\n";
235	out_stream << "{\n";
236
237	out_stream << llformat("count %d\n", mItems.size() );
238
239	S32 idx = 0;
240	for (std::vector<LLPointer<LLInventoryItem> >::iterator iter = mItems.begin();
241		 iter != mItems.end(); ++iter)
242	{
243		LLInventoryItem* item = *iter;
244		if (item)
245		{
246			out_stream << "{\n";
247			out_stream << llformat("ext char index %d\n", idx  );
248			if( !item->exportLegacyStream( out_stream ) )
249			{
250				return FALSE;
251			}
252			out_stream << "}\n";
253		}
254		++idx;
255	}
256
257	out_stream << "}\n";
258	
259	return TRUE;
260}
261
262bool LLNotecard::exportStream( std::ostream& out_stream )
263{
264	out_stream << "Linden text version 2\n";
265	out_stream << "{\n";
266
267	if( !exportEmbeddedItemsStream( out_stream ) )
268	{
269		return FALSE;
270	}
271
272	out_stream << llformat("Text length %d\n", mText.length() );
273	out_stream << mText;
274	out_stream << "}\n";
275
276	return TRUE;
277}
278
279////////////////////////////////////////////////////////////////////////////
280
281void LLNotecard::setItems(const std::vector<LLPointer<LLInventoryItem> >& items)
282{
283	mItems = items;
284}
285
286void LLNotecard::setText(const std::string& text)
287{
288	mText = text;
289}