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