lldatapacker.h

Go to the documentation of this file.
00001 
00032 #ifndef LL_LLDATAPACKER_H 
00033 #define LL_LLDATAPACKER_H
00034 
00035 class LLColor4;
00036 class LLColor4U;
00037 class LLVector2;
00038 class LLVector3;
00039 class LLVector4;
00040 class LLUUID;
00041 
00042 class LLDataPacker
00043 {
00044 public:
00045         virtual ~LLDataPacker() {}
00046         
00047         virtual void            reset()         { llerrs << "Using unimplemented datapacker reset!" << llendl; };
00048         virtual void dumpBufferToLog()  { llerrs << "dumpBufferToLog not implemented for this type!" << llendl; }
00049 
00050         virtual BOOL            hasNext() const = 0;
00051 
00052         virtual BOOL            packString(const char *value, const char *name) = 0;
00053         virtual BOOL            unpackString(std::string& value, const char *name) = 0;
00054 
00055         virtual BOOL            packBinaryData(const U8 *value, S32 size, const char *name) = 0;
00056         virtual BOOL            unpackBinaryData(U8 *value, S32 &size, const char *name) = 0;
00057 
00058         // Constant size binary data packing
00059         virtual BOOL            packBinaryDataFixed(const U8 *value, S32 size, const char *name) = 0;
00060         virtual BOOL            unpackBinaryDataFixed(U8 *value, S32 size, const char *name) = 0;
00061 
00062         virtual BOOL            packU8(const U8 value, const char *name) = 0;
00063         virtual BOOL            unpackU8(U8 &value, const char *name) = 0;
00064 
00065         virtual BOOL            packU16(const U16 value, const char *name) = 0;
00066         virtual BOOL            unpackU16(U16 &value, const char *name) = 0;
00067 
00068         virtual BOOL            packU32(const U32 value, const char *name) = 0;
00069         virtual BOOL            unpackU32(U32 &value, const char *name) = 0;
00070 
00071         virtual BOOL            packS32(const S32 value, const char *name) = 0;
00072         virtual BOOL            unpackS32(S32 &value, const char *name) = 0;
00073 
00074         virtual BOOL            packF32(const F32 value, const char *name) = 0;
00075         virtual BOOL            unpackF32(F32 &value, const char *name) = 0;
00076 
00077         // Packs a float into an integer, using the given size
00078         // and picks the right U* data type to pack into.
00079         BOOL                            packFixed(const F32 value, const char *name,
00080                                                                 const BOOL is_signed, const U32 int_bits, const U32 frac_bits);
00081         BOOL                            unpackFixed(F32 &value, const char *name,
00082                                                                 const BOOL is_signed, const U32 int_bits, const U32 frac_bits);
00083 
00084         virtual BOOL            packColor4(const LLColor4 &value, const char *name) = 0;
00085         virtual BOOL            unpackColor4(LLColor4 &value, const char *name) = 0;
00086 
00087         virtual BOOL            packColor4U(const LLColor4U &value, const char *name) = 0;
00088         virtual BOOL            unpackColor4U(LLColor4U &value, const char *name) = 0;
00089 
00090         virtual BOOL            packVector2(const LLVector2 &value, const char *name) = 0;
00091         virtual BOOL            unpackVector2(LLVector2 &value, const char *name) = 0;
00092 
00093         virtual BOOL            packVector3(const LLVector3 &value, const char *name) = 0;
00094         virtual BOOL            unpackVector3(LLVector3 &value, const char *name) = 0;
00095 
00096         virtual BOOL            packVector4(const LLVector4 &value, const char *name) = 0;
00097         virtual BOOL            unpackVector4(LLVector4 &value, const char *name) = 0;
00098 
00099         virtual BOOL            packUUID(const LLUUID &value, const char *name) = 0;
00100         virtual BOOL            unpackUUID(LLUUID &value, const char *name) = 0;
00101                         U32                     getPassFlags() const    { return mPassFlags; }
00102                         void            setPassFlags(U32 flags) { mPassFlags = flags; }
00103 protected:
00104         LLDataPacker();
00105 protected:
00106         U32     mPassFlags;
00107         BOOL mWriteEnabled; // disable this to do things like determine filesize without actually copying data
00108 };
00109 
00110 class LLDataPackerBinaryBuffer : public LLDataPacker
00111 {
00112 public:
00113         LLDataPackerBinaryBuffer(U8 *bufferp, S32 size)
00114         :       LLDataPacker(),
00115                 mBufferp(bufferp),
00116                 mCurBufferp(bufferp),
00117                 mBufferSize(size)
00118         {
00119                 mWriteEnabled = TRUE;
00120         }
00121 
00122         LLDataPackerBinaryBuffer()
00123         :       LLDataPacker(),
00124                 mBufferp(NULL),
00125                 mCurBufferp(NULL),
00126                 mBufferSize(0)
00127         {
00128         }
00129 
00130         /*virtual*/ BOOL                packString(const char *value, const char *name);
00131         /*virtual*/ BOOL                unpackString(std::string& value, const char *name);
00132 
00133         /*virtual*/ BOOL                packBinaryData(const U8 *value, S32 size, const char *name);
00134         /*virtual*/ BOOL                unpackBinaryData(U8 *value, S32 &size, const char *name);
00135 
00136         // Constant size binary data packing
00137         /*virtual*/ BOOL                packBinaryDataFixed(const U8 *value, S32 size, const char *name);
00138         /*virtual*/ BOOL                unpackBinaryDataFixed(U8 *value, S32 size, const char *name);
00139 
00140         /*virtual*/ BOOL                packU8(const U8 value, const char *name);
00141         /*virtual*/ BOOL                unpackU8(U8 &value, const char *name);
00142 
00143         /*virtual*/ BOOL                packU16(const U16 value, const char *name);
00144         /*virtual*/ BOOL                unpackU16(U16 &value, const char *name);
00145 
00146         /*virtual*/ BOOL                packU32(const U32 value, const char *name);
00147         /*virtual*/ BOOL                unpackU32(U32 &value, const char *name);
00148 
00149         /*virtual*/ BOOL                packS32(const S32 value, const char *name);
00150         /*virtual*/ BOOL                unpackS32(S32 &value, const char *name);
00151 
00152         /*virtual*/ BOOL                packF32(const F32 value, const char *name);
00153         /*virtual*/ BOOL                unpackF32(F32 &value, const char *name);
00154 
00155         /*virtual*/ BOOL                packColor4(const LLColor4 &value, const char *name);
00156         /*virtual*/ BOOL                unpackColor4(LLColor4 &value, const char *name);
00157 
00158         /*virtual*/ BOOL                packColor4U(const LLColor4U &value, const char *name);
00159         /*virtual*/ BOOL                unpackColor4U(LLColor4U &value, const char *name);
00160 
00161         /*virtual*/ BOOL                packVector2(const LLVector2 &value, const char *name);
00162         /*virtual*/ BOOL                unpackVector2(LLVector2 &value, const char *name);
00163 
00164         /*virtual*/ BOOL                packVector3(const LLVector3 &value, const char *name);
00165         /*virtual*/ BOOL                unpackVector3(LLVector3 &value, const char *name);
00166 
00167         /*virtual*/ BOOL                packVector4(const LLVector4 &value, const char *name);
00168         /*virtual*/ BOOL                unpackVector4(LLVector4 &value, const char *name);
00169 
00170         /*virtual*/ BOOL                packUUID(const LLUUID &value, const char *name);
00171         /*virtual*/ BOOL                unpackUUID(LLUUID &value, const char *name);
00172 
00173                                 S32                     getCurrentSize() const  { return (S32)(mCurBufferp - mBufferp); }
00174                                 S32                     getBufferSize() const   { return mBufferSize; }
00175                                 void            reset()                         { mCurBufferp = mBufferp; mWriteEnabled = (mCurBufferp != NULL); }
00176                                 void            freeBuffer()            { delete [] mBufferp; mBufferp = mCurBufferp = NULL; mBufferSize = 0; mWriteEnabled = FALSE; }
00177                                 void            assignBuffer(U8 *bufferp, S32 size)
00178                                 {
00179                                         mBufferp = bufferp;
00180                                         mCurBufferp = bufferp;
00181                                         mBufferSize = size;
00182                                         mWriteEnabled = TRUE;
00183                                 }
00184                                 const LLDataPackerBinaryBuffer& operator=(const LLDataPackerBinaryBuffer &a);
00185 
00186         /*virtual*/ BOOL                hasNext() const                 { return getCurrentSize() < getBufferSize(); }
00187 
00188         /*virtual*/ void dumpBufferToLog();
00189 protected:
00190         inline BOOL verifyLength(const S32 data_size, const char *name);
00191 
00192         U8 *mBufferp;
00193         U8 *mCurBufferp;
00194         S32 mBufferSize;
00195 };
00196 
00197 inline BOOL LLDataPackerBinaryBuffer::verifyLength(const S32 data_size, const char *name)
00198 {
00199         if (mWriteEnabled && (mCurBufferp - mBufferp) > mBufferSize - data_size)
00200         {
00201                 llwarns << "Buffer overflow in BinaryBuffer length verify, field name " << name << "!" << llendl;
00202                 llwarns << "Current pos: " << (int)(mCurBufferp - mBufferp) << " Buffer size: " << mBufferSize << " Data size: " << data_size << llendl;
00203                 return FALSE;
00204         }
00205 
00206         return TRUE;
00207 }
00208 
00209 class LLDataPackerAsciiBuffer : public LLDataPacker
00210 {
00211 public:
00212         LLDataPackerAsciiBuffer(char* bufferp, S32 size)
00213         {
00214                 mBufferp = bufferp;
00215                 mCurBufferp = bufferp;
00216                 mBufferSize = size;
00217                 mPassFlags = 0;
00218                 mIncludeNames = FALSE;
00219                 mWriteEnabled = TRUE;
00220         }
00221 
00222         LLDataPackerAsciiBuffer()
00223         {
00224                 mBufferp = NULL;
00225                 mCurBufferp = NULL;
00226                 mBufferSize = 0;
00227                 mPassFlags = 0;
00228                 mIncludeNames = FALSE;
00229                 mWriteEnabled = FALSE;
00230         }
00231 
00232         /*virtual*/ BOOL                packString(const char *value, const char *name);
00233         /*virtual*/ BOOL                unpackString(std::string& value, const char *name);
00234 
00235         /*virtual*/ BOOL                packBinaryData(const U8 *value, S32 size, const char *name);
00236         /*virtual*/ BOOL                unpackBinaryData(U8 *value, S32 &size, const char *name);
00237 
00238         // Constant size binary data packing
00239         /*virtual*/ BOOL                packBinaryDataFixed(const U8 *value, S32 size, const char *name);
00240         /*virtual*/ BOOL                unpackBinaryDataFixed(U8 *value, S32 size, const char *name);
00241 
00242         /*virtual*/ BOOL                packU8(const U8 value, const char *name);
00243         /*virtual*/ BOOL                unpackU8(U8 &value, const char *name);
00244 
00245         /*virtual*/ BOOL                packU16(const U16 value, const char *name);
00246         /*virtual*/ BOOL                unpackU16(U16 &value, const char *name);
00247 
00248         /*virtual*/ BOOL                packU32(const U32 value, const char *name);
00249         /*virtual*/ BOOL                unpackU32(U32 &value, const char *name);
00250 
00251         /*virtual*/ BOOL                packS32(const S32 value, const char *name);
00252         /*virtual*/ BOOL                unpackS32(S32 &value, const char *name);
00253 
00254         /*virtual*/ BOOL                packF32(const F32 value, const char *name);
00255         /*virtual*/ BOOL                unpackF32(F32 &value, const char *name);
00256 
00257         /*virtual*/ BOOL                packColor4(const LLColor4 &value, const char *name);
00258         /*virtual*/ BOOL                unpackColor4(LLColor4 &value, const char *name);
00259 
00260         /*virtual*/ BOOL                packColor4U(const LLColor4U &value, const char *name);
00261         /*virtual*/ BOOL                unpackColor4U(LLColor4U &value, const char *name);
00262 
00263         /*virtual*/ BOOL                packVector2(const LLVector2 &value, const char *name);
00264         /*virtual*/ BOOL                unpackVector2(LLVector2 &value, const char *name);
00265 
00266         /*virtual*/ BOOL                packVector3(const LLVector3 &value, const char *name);
00267         /*virtual*/ BOOL                unpackVector3(LLVector3 &value, const char *name);
00268 
00269         /*virtual*/ BOOL                packVector4(const LLVector4 &value, const char *name);
00270         /*virtual*/ BOOL                unpackVector4(LLVector4 &value, const char *name);
00271 
00272         /*virtual*/ BOOL                packUUID(const LLUUID &value, const char *name);
00273         /*virtual*/ BOOL                unpackUUID(LLUUID &value, const char *name);
00274 
00275         void            setIncludeNames(BOOL b) { mIncludeNames = b; }
00276 
00277         // Include the trailing NULL so it's always a valid string
00278         S32                     getCurrentSize() const  { return (S32)(mCurBufferp - mBufferp) + 1; }
00279 
00280         S32                     getBufferSize() const   { return mBufferSize; }
00281         /*virtual*/ void                reset()                                 { mCurBufferp = mBufferp; mWriteEnabled = (mCurBufferp != NULL); }
00282 
00283         /*virtual*/ BOOL                hasNext() const                 { return getCurrentSize() < getBufferSize(); }
00284 
00285         inline void     freeBuffer();
00286         inline void     assignBuffer(char* bufferp, S32 size);
00287         void            dump();
00288 
00289 protected:
00290         void writeIndentedName(const char *name);
00291         BOOL getValueStr(const char *name, char *out_value, const S32 value_len);
00292         
00293 protected:
00294         inline BOOL verifyLength(const S32 data_size, const char *name);
00295 
00296         char *mBufferp;
00297         char *mCurBufferp;
00298         S32 mBufferSize;
00299         BOOL mIncludeNames;     // useful for debugging, print the name of each field
00300 };
00301 
00302 inline void     LLDataPackerAsciiBuffer::freeBuffer()
00303 {
00304         delete [] mBufferp; 
00305         mBufferp = mCurBufferp = NULL; 
00306         mBufferSize = 0;
00307         mWriteEnabled = FALSE;
00308 }
00309 
00310 inline void     LLDataPackerAsciiBuffer::assignBuffer(char* bufferp, S32 size)
00311 {
00312         mBufferp = bufferp;
00313         mCurBufferp = bufferp;
00314         mBufferSize = size;
00315         mWriteEnabled = TRUE;
00316 }
00317 
00318 inline BOOL LLDataPackerAsciiBuffer::verifyLength(const S32 data_size, const char *name)
00319 {
00320         if (mWriteEnabled && (mCurBufferp - mBufferp) > mBufferSize - data_size)
00321         {
00322                 llwarns << "Buffer overflow in AsciiBuffer length verify, field name " << name << "!" << llendl;
00323                 llwarns << "Current pos: " << (int)(mCurBufferp - mBufferp) << " Buffer size: " << mBufferSize << " Data size: " << data_size << llendl;
00324                 return FALSE;
00325         }
00326 
00327         return TRUE;
00328 }
00329 
00330 class LLDataPackerAsciiFile : public LLDataPacker
00331 {
00332 public:
00333         LLDataPackerAsciiFile(FILE *fp, const S32 indent = 2)
00334         :       LLDataPacker(),
00335                 mIndent(indent),
00336                 mFP(fp),
00337                 mOutputStream(NULL),
00338                 mInputStream(NULL)
00339         {
00340         }
00341 
00342         LLDataPackerAsciiFile(std::ostream& output_stream, const S32 indent = 2)
00343         :       LLDataPacker(),
00344                 mIndent(indent),
00345                 mFP(NULL),
00346                 mOutputStream(&output_stream),
00347                 mInputStream(NULL)
00348         {
00349                 mWriteEnabled = TRUE;
00350         }
00351 
00352         LLDataPackerAsciiFile(std::istream& input_stream, const S32 indent = 2)
00353         :       LLDataPacker(),
00354                 mIndent(indent),
00355                 mFP(NULL),
00356                 mOutputStream(NULL),
00357                 mInputStream(&input_stream)
00358         {
00359         }
00360 
00361         /*virtual*/ BOOL                packString(const char *value, const char *name);
00362         /*virtual*/ BOOL                unpackString(std::string& value, const char *name);
00363 
00364         /*virtual*/ BOOL                packBinaryData(const U8 *value, S32 size, const char *name);
00365         /*virtual*/ BOOL                unpackBinaryData(U8 *value, S32 &size, const char *name);
00366 
00367         /*virtual*/ BOOL                packBinaryDataFixed(const U8 *value, S32 size, const char *name);
00368         /*virtual*/ BOOL                unpackBinaryDataFixed(U8 *value, S32 size, const char *name);
00369 
00370         /*virtual*/ BOOL                packU8(const U8 value, const char *name);
00371         /*virtual*/ BOOL                unpackU8(U8 &value, const char *name);
00372 
00373         /*virtual*/ BOOL                packU16(const U16 value, const char *name);
00374         /*virtual*/ BOOL                unpackU16(U16 &value, const char *name);
00375 
00376         /*virtual*/ BOOL                packU32(const U32 value, const char *name);
00377         /*virtual*/ BOOL                unpackU32(U32 &value, const char *name);
00378 
00379         /*virtual*/ BOOL                packS32(const S32 value, const char *name);
00380         /*virtual*/ BOOL                unpackS32(S32 &value, const char *name);
00381 
00382         /*virtual*/ BOOL                packF32(const F32 value, const char *name);
00383         /*virtual*/ BOOL                unpackF32(F32 &value, const char *name);
00384 
00385         /*virtual*/ BOOL                packColor4(const LLColor4 &value, const char *name);
00386         /*virtual*/ BOOL                unpackColor4(LLColor4 &value, const char *name);
00387 
00388         /*virtual*/ BOOL                packColor4U(const LLColor4U &value, const char *name);
00389         /*virtual*/ BOOL                unpackColor4U(LLColor4U &value, const char *name);
00390 
00391         /*virtual*/ BOOL                packVector2(const LLVector2 &value, const char *name);
00392         /*virtual*/ BOOL                unpackVector2(LLVector2 &value, const char *name);
00393 
00394         /*virtual*/ BOOL                packVector3(const LLVector3 &value, const char *name);
00395         /*virtual*/ BOOL                unpackVector3(LLVector3 &value, const char *name);
00396 
00397         /*virtual*/ BOOL                packVector4(const LLVector4 &value, const char *name);
00398         /*virtual*/ BOOL                unpackVector4(LLVector4 &value, const char *name);
00399 
00400         /*virtual*/ BOOL                packUUID(const LLUUID &value, const char *name);
00401         /*virtual*/ BOOL                unpackUUID(LLUUID &value, const char *name);
00402 protected:
00403         void writeIndentedName(const char *name);
00404         BOOL getValueStr(const char *name, char *out_value, const S32 value_len);
00405         
00406         /*virtual*/ BOOL                hasNext() const                 { return true; }
00407 
00408 protected:
00409         S32 mIndent;
00410         FILE *mFP;
00411         std::ostream* mOutputStream;
00412         std::istream* mInputStream;
00413 };
00414 
00415 #endif // LL_LLDATAPACKER
00416 

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