GDCM 2.2.1

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

gdcmByteBuffer.h
Go to the documentation of this file.
1 /*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2011 Mathieu Malaterre
6 All rights reserved.
7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13 =========================================================================*/
14 #ifndef GDCMBYTEBUFFER_H
15 #define GDCMBYTEBUFFER_H
16
17 #include "gdcmTypes.h"
18 #include <vector>
19 #include <assert.h>
20 #include <string.h> // memmove
21
22 #error should not be used
23
24 namespace gdcm
25 {
34 class ByteBuffer
35 {
36 static const int InitBufferSize = 1024;
37 public:
38 ByteBuffer() : Start(0), End(0),Limit(0) {}
39 char *Get(int len)
40 {
41 char *buffer = &Internal[0];
42 if (len > Limit - End)
43 {
44 // FIXME avoid integer overflow
45 int neededSize = len + (End - Start);
46 if (neededSize <= Limit - buffer)
47 {
48 memmove(buffer, Start, End - Start);
49 End = buffer + (End - Start);
50 Start = buffer;
51 }
52 else
53 {
54 char *newBuf;
55 int bufferSize = Limit - Start;
56 if ( bufferSize == 0 )
57 {
58 bufferSize = InitBufferSize;
59 }
60 do
61 {
62 bufferSize *= 2;
63 } while (bufferSize < neededSize);
64 //newBuf = malloc(bufferSize);
65 try
66 {
67 Internal.reserve(bufferSize);
68 newBuf = &Internal[0];
69 }
70 catch(...)
71 {
72 //errorCode = NoMemoryError;
73 return 0;
74 }
75 Limit = newBuf + bufferSize;
76
77 if (Start)
78 {
79 memcpy(newBuf, Start, End - Start);
80 }
81 End = newBuf + (End - Start);
82 Start = /*buffer =*/ newBuf;
83 }
84 }
85 assert( (int)Internal.capacity() >= len );
86 return End;
87 }
88
89 void UpdatePosition() {}
90 void ShiftEnd(int len) {
91 End += len;
92 }
93 const char *GetStart() const {
94 return Start;
95 }
96
97 private:
98 typedef std::vector<char> CharVector;
99 const char *Start;
100 char *End;
101 const char *Limit;
102 CharVector Internal;
103 };
104
105 } // end namespace gdcm
106
107 #endif //GDCMBYTEBUFFER_H