00001
00034 #ifndef LL_LLSDSERIALIZE_H
00035 #define LL_LLSDSERIALIZE_H
00036
00037 #include <iosfwd>
00038 #include "llsd.h"
00039 #include "llmemory.h"
00040
00045 class LLSDParser : public LLRefCount
00046 {
00047 protected:
00051 virtual ~LLSDParser();
00052
00053 public:
00057 enum
00058 {
00059 PARSE_FAILURE = -1
00060 };
00061
00065 LLSDParser();
00066
00084 S32 parse(std::istream& istr, LLSD& data, S32 max_bytes);
00085
00086 protected:
00101 virtual S32 doParse(std::istream& istr, LLSD& data) const = 0;
00102
00103
00104
00105
00106
00107
00108
00109
00111
00117 int get(std::istream& istr) const;
00118
00128 std::istream& get(
00129 std::istream& istr,
00130 char* s,
00131 std::streamsize n,
00132 char delim) const;
00133
00142 std::istream& get(
00143 std::istream& istr,
00144 std::streambuf& sb,
00145 char delim) const;
00146
00153 std::istream& ignore(std::istream& istr) const;
00154
00162 std::istream& putback(std::istream& istr, char c) const;
00163
00172 std::istream& read(std::istream& istr, char* s, std::streamsize n) const;
00174
00175 protected:
00182 void account(S32 bytes) const;
00183
00184 protected:
00188 bool mCheckLimits;
00189
00193 mutable S32 mMaxBytesLeft;
00194 };
00195
00200 class LLSDNotationParser : public LLSDParser
00201 {
00202 protected:
00206 virtual ~LLSDNotationParser();
00207
00208 public:
00212 LLSDNotationParser();
00213
00214 protected:
00229 virtual S32 doParse(std::istream& istr, LLSD& data) const;
00230
00231 private:
00239 S32 parseMap(std::istream& istr, LLSD& map) const;
00240
00248 S32 parseArray(std::istream& istr, LLSD& array) const;
00249
00257 bool parseString(std::istream& istr, LLSD& data) const;
00258
00266 bool parseBinary(std::istream& istr, LLSD& data) const;
00267 };
00268
00273 class LLSDXMLParser : public LLSDParser
00274 {
00275 protected:
00279 virtual ~LLSDXMLParser();
00280
00281 public:
00285 LLSDXMLParser();
00286
00287 protected:
00302 virtual S32 doParse(std::istream& istr, LLSD& data) const;
00303
00304 private:
00305 class Impl;
00306 Impl& impl;
00307
00308 void parsePart(const char* buf, int len);
00309 friend class LLSDSerialize;
00310 };
00311
00316 class LLSDBinaryParser : public LLSDParser
00317 {
00318 protected:
00322 virtual ~LLSDBinaryParser();
00323
00324 public:
00328 LLSDBinaryParser();
00329
00330 protected:
00345 virtual S32 doParse(std::istream& istr, LLSD& data) const;
00346
00347 private:
00355 S32 parseMap(std::istream& istr, LLSD& map) const;
00356
00364 S32 parseArray(std::istream& istr, LLSD& array) const;
00365
00373 bool parseString(std::istream& istr, std::string& value) const;
00374 };
00375
00376
00381 class LLSDFormatter : public LLRefCount
00382 {
00383 protected:
00387 virtual ~LLSDFormatter();
00388
00389 public:
00393 typedef enum e_formatter_options_type
00394 {
00395 OPTIONS_NONE = 0,
00396 OPTIONS_PRETTY = 1
00397 } EFormatterOptions;
00398
00402 LLSDFormatter();
00403
00409 void boolalpha(bool alpha);
00410
00423 void realFormat(const std::string& format);
00424
00432 virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const = 0;
00433
00434 protected:
00441 void formatReal(LLSD::Real real, std::ostream& ostr) const;
00442
00443 protected:
00444 bool mBoolAlpha;
00445 std::string mRealFormat;
00446 };
00447
00448
00453 class LLSDNotationFormatter : public LLSDFormatter
00454 {
00455 protected:
00459 virtual ~LLSDNotationFormatter();
00460
00461 public:
00465 LLSDNotationFormatter();
00466
00477 static std::string escapeString(const std::string& in);
00478
00486 virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const;
00487 };
00488
00489
00494 class LLSDXMLFormatter : public LLSDFormatter
00495 {
00496 protected:
00500 virtual ~LLSDXMLFormatter();
00501
00502 public:
00506 LLSDXMLFormatter();
00507
00514 static std::string escapeString(const std::string& in);
00515
00523 virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const;
00524
00525 protected:
00526
00534 S32 format_impl(const LLSD& data, std::ostream& ostr, U32 options, U32 level) const;
00535 };
00536
00537
00562 class LLSDBinaryFormatter : public LLSDFormatter
00563 {
00564 protected:
00568 virtual ~LLSDBinaryFormatter();
00569
00570 public:
00574 LLSDBinaryFormatter();
00575
00583 virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const;
00584
00585 protected:
00594 void formatString(const std::string& string, std::ostream& ostr) const;
00595 };
00596
00597
00613 template <class Formatter>
00614 class LLSDOStreamer : public Formatter
00615 {
00616 public:
00620 LLSDOStreamer(const LLSD& data, U32 options = LLSDFormatter::OPTIONS_NONE) :
00621 mSD(data), mOptions(options) {}
00622
00631 friend std::ostream& operator<<(
00632 std::ostream& str,
00633 const LLSDOStreamer<Formatter>& formatter)
00634 {
00635 formatter.format(formatter.mSD, str, formatter.mOptions);
00636 return str;
00637 }
00638
00639 protected:
00640 LLSD mSD;
00641 U32 mOptions;
00642 };
00643
00644 typedef LLSDOStreamer<LLSDNotationFormatter> LLSDNotationStreamer;
00645 typedef LLSDOStreamer<LLSDXMLFormatter> LLSDXMLStreamer;
00646
00651 class LLSDSerialize
00652 {
00653 public:
00654 enum ELLSD_Serialize
00655 {
00656 LLSD_BINARY, LLSD_XML
00657 };
00658
00662 enum
00663 {
00664
00665
00666
00667 SIZE_UNLIMITED = -1,
00668 };
00669
00670
00671
00672
00673 static void serialize(const LLSD& sd, std::ostream& str, ELLSD_Serialize,
00674 U32 options = LLSDFormatter::OPTIONS_NONE);
00675
00684 static bool deserialize(LLSD& sd, std::istream& str, S32 max_bytes);
00685
00686
00687
00688
00689 static S32 toNotation(const LLSD& sd, std::ostream& str)
00690 {
00691 LLPointer<LLSDNotationFormatter> f = new LLSDNotationFormatter;
00692 return f->format(sd, str, LLSDFormatter::OPTIONS_NONE);
00693 }
00694 static S32 fromNotation(LLSD& sd, std::istream& str, S32 max_bytes)
00695 {
00696 LLPointer<LLSDNotationParser> p = new LLSDNotationParser;
00697 return p->parse(str, sd, max_bytes);
00698 }
00699 static LLSD fromNotation(std::istream& str, S32 max_bytes)
00700 {
00701 LLPointer<LLSDNotationParser> p = new LLSDNotationParser;
00702 LLSD sd;
00703 (void)p->parse(str, sd, max_bytes);
00704 return sd;
00705 }
00706
00707
00708
00709
00710 static S32 toXML(const LLSD& sd, std::ostream& str)
00711 {
00712 LLPointer<LLSDXMLFormatter> f = new LLSDXMLFormatter;
00713 return f->format(sd, str, LLSDFormatter::OPTIONS_NONE);
00714 }
00715 static S32 toPrettyXML(const LLSD& sd, std::ostream& str)
00716 {
00717 LLPointer<LLSDXMLFormatter> f = new LLSDXMLFormatter;
00718 return f->format(sd, str, LLSDFormatter::OPTIONS_PRETTY);
00719 }
00720
00721 static S32 fromXML(LLSD& sd, std::istream& str)
00722 {
00723
00724
00725 LLPointer<LLSDXMLParser> p = new LLSDXMLParser;
00726 return p->parse(str, sd, LLSDSerialize::SIZE_UNLIMITED);
00727 }
00728
00729
00730
00731
00732 static S32 toBinary(const LLSD& sd, std::ostream& str)
00733 {
00734 LLPointer<LLSDBinaryFormatter> f = new LLSDBinaryFormatter;
00735 return f->format(sd, str, LLSDFormatter::OPTIONS_NONE);
00736 }
00737 static S32 fromBinary(LLSD& sd, std::istream& str, S32 max_bytes)
00738 {
00739 LLPointer<LLSDBinaryParser> p = new LLSDBinaryParser;
00740 return p->parse(str, sd, max_bytes);
00741 }
00742 static LLSD fromBinary(std::istream& str, S32 max_bytes)
00743 {
00744 LLPointer<LLSDBinaryParser> p = new LLSDBinaryParser;
00745 LLSD sd;
00746 (void)p->parse(str, sd, max_bytes);
00747 return sd;
00748 }
00749 };
00750
00751 #endif // LL_LLSDSERIALIZE_H