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