gdcmByteBuffer.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program: GDCM (Grassroots DICOM). A DICOM library
00004   Module:  $URL$
00005 
00006   Copyright (c) 2006-2010 Mathieu Malaterre
00007   All rights reserved.
00008   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 #ifndef GDCMBYTEBUFFER_H
00016 #define GDCMBYTEBUFFER_H
00017 
00018 #include "gdcmTypes.h"
00019 #include <vector>
00020 #include <assert.h>
00021 #include <string.h> // memmove
00022 
00023 #error should not be used
00024 
00025 namespace gdcm
00026 {
00035 class ByteBuffer
00036 {
00037   static const int InitBufferSize = 1024;
00038 public:
00039   ByteBuffer() : Start(0), End(0),Limit(0) {}
00040   char *Get(int len)
00041     {
00042     char *buffer = &Internal[0];
00043     if (len > Limit - End)
00044       {
00045       // FIXME avoid integer overflow
00046       int neededSize = len + (End - Start);
00047       if (neededSize  <= Limit - buffer)
00048         {
00049         memmove(buffer, Start, End - Start);
00050         End = buffer + (End - Start);
00051         Start = buffer;
00052         }
00053       else
00054         {
00055         char *newBuf;
00056         int bufferSize = Limit - Start;
00057         if ( bufferSize == 0 )
00058           {
00059           bufferSize = InitBufferSize;
00060           }
00061         do
00062           {
00063           bufferSize *= 2;
00064           } while (bufferSize < neededSize);
00065         //newBuf = malloc(bufferSize);
00066         try
00067           {
00068           Internal.reserve(bufferSize);
00069           newBuf = &Internal[0];
00070           }
00071         catch(...)
00072           {
00073           //errorCode = NoMemoryError;
00074           return 0;
00075           }
00076         Limit = newBuf + bufferSize;
00077 
00078         if (Start) 
00079           {
00080           memcpy(newBuf, Start, End - Start);
00081           }
00082         End = newBuf + (End - Start);
00083         Start = /*buffer =*/ newBuf;
00084         }
00085       }
00086     assert( (int)Internal.capacity() >= len );
00087     return End;
00088     }
00089 
00090   void UpdatePosition() {}
00091   void ShiftEnd(int len) {
00092     End += len;
00093   }
00094   const char *GetStart() const {
00095     return Start;
00096   }
00097 
00098 private:
00099   typedef std::vector<char> CharVector;
00100   const char *Start;
00101         char *End;
00102   const char *Limit;
00103   CharVector Internal;
00104 };
00105 
00106 } // end namespace gdcm
00107 
00108 #endif //GDCMBYTEBUFFER_H

Generated on Sat Dec 4 2010 08:58:39 for GDCM by doxygen 1.7.2
SourceForge.net Logo