llnotecard.cpp

Go to the documentation of this file.
00001 
00032 #include "linden_common.h"
00033 #include "llnotecard.h"
00034 #include "llstreamtools.h"
00035 
00036 LLNotecard::LLNotecard(S32 max_text)
00037 : mMaxText(max_text)
00038 {
00039 }
00040 
00041 LLNotecard::~LLNotecard()
00042 {
00043 }
00044 
00045 bool LLNotecard::importEmbeddedItemsStream(std::istream& str)
00046 {
00047         // Version 1 format:
00048         //              LLEmbeddedItems version 1
00049         //              {
00050         //                      count <number of entries being used and not deleted>
00051         //                      {
00052         //                              ext char index <index>
00053         //                              <InventoryItem chunk>
00054         //                      }
00055         //              }
00056         
00057         S32 i;
00058         S32 count = 0;
00059 
00060         str >> std::ws >> "LLEmbeddedItems version" >> mEmbeddedVersion >> "\n";
00061         if (str.fail())
00062         {
00063                 llwarns << "Invalid Linden text file header" << llendl;
00064                 goto import_file_failed;
00065         }
00066 
00067         if( 1 != mEmbeddedVersion )
00068         {
00069                 llwarns << "Invalid LLEmbeddedItems version: " << mEmbeddedVersion << llendl;
00070                 goto import_file_failed;
00071         }
00072 
00073         str >> std::ws >> "{\n";
00074         if(str.fail())
00075         {
00076                 llwarns << "Invalid Linden text file format: missing {" << llendl;
00077                 goto import_file_failed;
00078         }
00079 
00080         str >> std::ws >> "count " >> count >> "\n";
00081         if(str.fail())
00082         {
00083                 llwarns << "Invalid LLEmbeddedItems count" << llendl;
00084                 goto import_file_failed;
00085         }
00086 
00087         if((count < 0))
00088         {
00089                 llwarns << "Invalid LLEmbeddedItems count value: " << count << llendl;
00090                 goto import_file_failed;
00091         }
00092 
00093         for(i = 0; i < count; i++)
00094         {
00095                 str >> std::ws >> "{\n";
00096                 if(str.fail())
00097                 {
00098                         llwarns << "Invalid LLEmbeddedItems file format: missing {" << llendl;
00099                         goto import_file_failed;
00100                 }
00101 
00102                 U32 index = 0;
00103                 str >> std::ws >> "ext char index " >> index >> "\n";
00104                 if(str.fail())
00105                 {
00106                         llwarns << "Invalid LLEmbeddedItems file format: missing ext char index" << llendl;
00107                         goto import_file_failed;
00108                 }
00109 
00110                 str >> std::ws >> "inv_item\t0\n";
00111                 if(str.fail())
00112                 {
00113                         llwarns << "Invalid LLEmbeddedItems file format: missing inv_item" << llendl;
00114                         goto import_file_failed;
00115                 }
00116 
00117                 LLPointer<LLInventoryItem> item = new LLInventoryItem;
00118                 if (!item->importLegacyStream(str))
00119                 {
00120                         llinfos << "notecard import failed" << llendl;
00121                         goto import_file_failed;
00122                 }               
00123                 mItems.push_back(item);
00124 
00125                 str >> std::ws >> "}\n";
00126                 if(str.fail())
00127                 {
00128                         llwarns << "Invalid LLEmbeddedItems file format: missing }" << llendl;
00129                         goto import_file_failed;
00130                 }
00131         }
00132 
00133         str >> std::ws >> "}\n";
00134         if(str.fail())
00135         {
00136                 llwarns << "Invalid LLEmbeddedItems file format: missing }" << llendl;
00137                 goto import_file_failed;
00138         }
00139 
00140         return true;
00141 
00142         import_file_failed:
00143         return false;
00144 }
00145 
00146 bool LLNotecard::importStream(std::istream& str)
00147 {
00148         // Version 1 format:
00149         //              Linden text version 1
00150         //              {
00151         //                      <EmbeddedItemList chunk>
00152         //                      Text length
00153         //                      <ASCII text; 0x80 | index = embedded item>
00154         //              }
00155         
00156         // Version 2 format: (NOTE: Imports identically to version 1)
00157         //              Linden text version 2
00158         //              {
00159         //                      <EmbeddedItemList chunk>
00160         //                      Text length
00161         //                      <UTF8 text; FIRST_EMBEDDED_CHAR + index = embedded item>
00162         //              }
00163         
00164         str >> std::ws >> "Linden text version " >> mVersion >> "\n";
00165         if(str.fail())
00166         {
00167                 llwarns << "Invalid Linden text file header " << llendl;
00168                 return FALSE;
00169         }
00170 
00171         if( 1 != mVersion && 2 != mVersion)
00172         {
00173                 llwarns << "Invalid Linden text file version: " << mVersion << llendl;
00174                 return FALSE;
00175         }
00176 
00177         str >> std::ws >> "{\n";
00178         if(str.fail())
00179         {
00180                 llwarns << "Invalid Linden text file format" << llendl;
00181                 return FALSE;
00182         }
00183 
00184         if(!importEmbeddedItemsStream(str))
00185         {
00186                 return FALSE;
00187         }
00188 
00189         char line_buf[STD_STRING_BUF_SIZE];     /* Flawfinder: ignore */
00190         str.getline(line_buf, STD_STRING_BUF_SIZE);
00191         if(str.fail())
00192         {
00193                 llwarns << "Invalid Linden text length field" << llendl;
00194                 return FALSE;
00195         }
00196         line_buf[STD_STRING_STR_LEN] = '\0';
00197         
00198         S32 text_len = 0;
00199         if( 1 != sscanf(line_buf, "Text length %d", &text_len) )
00200         {
00201                 llwarns << "Invalid Linden text length field" << llendl;
00202                 return FALSE;
00203         }
00204 
00205         if(text_len > mMaxText)
00206         {
00207                 llwarns << "Invalid Linden text length: " << text_len << llendl;
00208                 return FALSE;
00209         }
00210 
00211         BOOL success = TRUE;
00212 
00213         char* text = new char[text_len + 1];
00214         fullread(str, text, text_len);
00215         if(str.fail())
00216         {
00217                 llwarns << "Invalid Linden text: text shorter than text length: " << text_len << llendl;
00218                 success = FALSE;
00219         }
00220         text[text_len] = '\0';
00221 
00222         if(success)
00223         {
00224                 // Actually set the text
00225                 mText = text;
00226         }
00227 
00228         delete[] text;
00229 
00230         return success;
00231 }
00232 
00234 
00235 bool LLNotecard::exportEmbeddedItemsStream( std::ostream& out_stream )
00236 {
00237         out_stream << "LLEmbeddedItems version 1\n";
00238         out_stream << "{\n";
00239 
00240         out_stream << llformat("count %d\n", mItems.size() );
00241 
00242         S32 idx = 0;
00243         for (std::vector<LLPointer<LLInventoryItem> >::iterator iter = mItems.begin();
00244                  iter != mItems.end(); ++iter)
00245         {
00246                 LLInventoryItem* item = *iter;
00247                 if (item)
00248                 {
00249                         out_stream << "{\n";
00250                         out_stream << llformat("ext char index %d\n", idx  );
00251                         if( !item->exportLegacyStream( out_stream ) )
00252                         {
00253                                 return FALSE;
00254                         }
00255                         out_stream << "}\n";
00256                 }
00257                 ++idx;
00258         }
00259 
00260         out_stream << "}\n";
00261         
00262         return TRUE;
00263 }
00264 
00265 bool LLNotecard::exportStream( std::ostream& out_stream )
00266 {
00267         out_stream << "Linden text version 2\n";
00268         out_stream << "{\n";
00269 
00270         if( !exportEmbeddedItemsStream( out_stream ) )
00271         {
00272                 return FALSE;
00273         }
00274 
00275         out_stream << llformat("Text length %d\n", mText.length() );
00276         out_stream << mText;
00277         out_stream << "}\n";
00278 
00279         return TRUE;
00280 }
00281 
00283 
00284 const std::vector<LLPointer<LLInventoryItem> >& LLNotecard::getItems() const
00285 {
00286         return mItems;
00287 }
00288 
00289 LLString& LLNotecard::getText()
00290 {
00291         return mText;
00292 }
00293 
00294 void LLNotecard::setItems(const std::vector<LLPointer<LLInventoryItem> >& items)
00295 {
00296         mItems = items;
00297 }
00298 
00299 void LLNotecard::setText(const LLString& text)
00300 {
00301         mText = text;
00302 }

Generated on Thu Jul 1 06:08:55 2010 for Second Life Viewer by  doxygen 1.4.7