MemoryBuffer.h

00001 //
00002 //  Copyright (c) 1996-2006 by Autodesk, Inc.
00003 //
00004 //  By using this code, you are agreeing to the terms and conditions of
00005 //  the License Agreement included in the documentation for this code.
00006 //
00007 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED, AS TO THE CORRECTNESS
00008 //  OF THIS CODE OR ANY DERIVATIVE WORKS WHICH INCORPORATE IT. AUTODESK
00009 //  PROVIDES THE CODE ON AN "AS-IS" BASIS AND EXPLICITLY DISCLAIMS ANY
00010 //  LIABILITY, INCLUDING CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00011 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00012 //
00013 //  Use, duplication, or disclosure by the U.S. Government is subject to
00014 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00015 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00016 //  Data and Computer Software), as applicable.
00017 //
00018 
00019 
00020 #if !defined MEMORY_BUFFER_HEADER
00021 #define MEMORY_BUFFER_HEADER
00022 
00023 class tMemoryBuffer
00024 {
00025 public:
00026     tMemoryBuffer( size_t nAllocSize ) throw (DWFException)
00027         : _size(nAllocSize)
00028         , _buffer(NULL)
00029         , _currentStrlen(0)
00030     {
00031         _buffer = DWFCORE_ALLOC_MEMORY(WT_Byte, _size);
00032         if (_buffer == NULL)
00033         {
00034             _DWFCORE_THROW( DWFMemoryException, L"Cannot allocate buffer" );
00035         }
00036     }
00037 
00038     ~tMemoryBuffer()
00039     {
00040         if (_buffer)
00041         {
00042             DWFCORE_FREE_MEMORY( _buffer );
00043         }
00044     }
00045 
00046     const char * buffer() const
00047     {
00048         return (char *) _buffer;
00049     }
00050 
00051     size_t size() const
00052     {
00053         return _size;
00054     }
00055 
00056     void init()
00057     {
00058         if(buffer() != NULL)
00059         {
00060             DWFCORE_ZERO_MEMORY(buffer(), 4); //zero the first 4 bytes, because we're good like that
00061         }
00062         _currentStrlen = 0;
00063     }
00064 
00065     size_t strlen(bool bForceComputation=false)
00066     {
00067         if( (_currentStrlen == 0) || bForceComputation)
00068         {
00069             _currentStrlen = DWFCORE_ASCII_STRING_LENGTH((char *)_buffer);
00070         }
00071         return _currentStrlen;
00072     }
00073 
00074     void set(const char *pStr)
00075     {
00076         size_t inStrLen = DWFCORE_ASCII_STRING_LENGTH(pStr);
00077         DWFCORE_ASCII_STRING_COPY( ((char *)_buffer), pStr );
00078         _currentStrlen = inStrLen;
00079     }
00080 
00081     void concatenate(const char *pStr)
00082     {
00083         size_t inStrLen = DWFCORE_ASCII_STRING_LENGTH(pStr);
00084                 DWFCORE_ASCII_STRING_CONCATENATE( ((char *)_buffer)+_currentStrlen, pStr );
00085         _currentStrlen += inStrLen;
00086     }
00087 
00088     void copy(tMemoryBuffer *pOtherBuffer)
00089     {
00090         size_t otherLen = pOtherBuffer->strlen();
00091         DWFCORE_COPY_MEMORY( buffer(), pOtherBuffer->buffer(), pOtherBuffer->size() ); // would like to just copy strlen, but might not be safe
00092         _currentStrlen = otherLen;
00093     }
00094 
00095 private:
00096     //
00097     // The number of bytes allocated for this buffer
00098     //
00099     size_t _size;
00100 
00101     //
00102     // The bytes themselves
00103     //
00104     WT_Byte* _buffer;
00105 
00106     //
00107     // The string length of the bytes stored in the buffer. Note that
00108     // this is only valid if the set/concatenate/copy methods are used.
00109     // If someone grabs the buffer pointer and plays with it directly,
00110     // all bets are off.
00111     //
00112     size_t _currentStrlen;
00113 
00114 };
00115 
00116 typedef multimap< size_t, tMemoryBuffer* > tMemoryBufferMap;
00117 
00118 #endif

Generated on Tue Jan 6 22:40:04 2009 for Autodesk DWF Toolkit by  doxygen 1.4.5