|
1 // DynamicBuffer.cpp |
|
2 // |
|
3 // Copyright (c) 2010 Accenture. All rights reserved. |
|
4 // This component and the accompanying materials are made available |
|
5 // under the terms of the "Eclipse Public License v1.0" |
|
6 // which accompanies this distribution, and is available |
|
7 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 // |
|
9 // Initial Contributors: |
|
10 // Accenture - Initial contribution |
|
11 // |
|
12 |
|
13 #include "Misc.h" |
|
14 #include "stdafx.h" |
|
15 #include "console_host.h" |
|
16 #include "DynamicBuffer.h" |
|
17 |
|
18 |
|
19 CDynamicReadBuffer* CDynamicReadBuffer::New(int aInitialLength) |
|
20 { |
|
21 std::auto_ptr<CDynamicReadBuffer> self(new(EThrow) CDynamicReadBuffer()); |
|
22 self->Construct(aInitialLength); |
|
23 return self.release(); |
|
24 } |
|
25 |
|
26 CDynamicReadBuffer::~CDynamicReadBuffer() |
|
27 { |
|
28 delete iBuf; |
|
29 } |
|
30 |
|
31 const char* CDynamicReadBuffer::Buffer() const |
|
32 { |
|
33 return iBuf; |
|
34 } |
|
35 |
|
36 char* CDynamicReadBuffer::Buffer(int aLength) |
|
37 { |
|
38 if (aLength > iMaxBufLength) |
|
39 { |
|
40 GrowBuf(aLength); |
|
41 } |
|
42 return iBuf; |
|
43 } |
|
44 |
|
45 CDynamicReadBuffer::CDynamicReadBuffer() : iBuf(NULL), iMaxBufLength(0) |
|
46 { |
|
47 } |
|
48 |
|
49 void CDynamicReadBuffer::Construct(int aInitialLength) |
|
50 { |
|
51 GrowBuf(aInitialLength); |
|
52 } |
|
53 |
|
54 void CDynamicReadBuffer::GrowBuf(int aNewLength) |
|
55 { |
|
56 ASSERT(aNewLength > iMaxBufLength); |
|
57 char* newBuf = new(EThrow) char[aNewLength]; |
|
58 delete iBuf; |
|
59 iBuf = newBuf; |
|
60 iMaxBufLength = aNewLength; |
|
61 } |
|
62 |
|
63 |
|
64 CDynamicWriteBuffer* CDynamicWriteBuffer::New(int aInitialLength) |
|
65 { |
|
66 std::auto_ptr<CDynamicWriteBuffer> self(new(EThrow) CDynamicWriteBuffer()); |
|
67 self->Construct(aInitialLength); |
|
68 return self.release(); |
|
69 } |
|
70 |
|
71 CDynamicWriteBuffer::~CDynamicWriteBuffer() |
|
72 { |
|
73 delete iBuf; |
|
74 } |
|
75 |
|
76 void CDynamicWriteBuffer::Add(char* aBuf, int aLength) |
|
77 { |
|
78 if (aLength > (iMaxBufLength - iCurrentBufLength)) |
|
79 { |
|
80 GrowBuf(iCurrentBufLength + aLength); |
|
81 } |
|
82 memcpy(iBuf + iCurrentBufLength, aBuf, aLength); |
|
83 iCurrentBufLength += aLength; |
|
84 } |
|
85 |
|
86 void CDynamicWriteBuffer::Remove(int aLength) |
|
87 { |
|
88 ASSERT(aLength <= iCurrentBufLength); |
|
89 memmove(iBuf, iBuf + aLength, iCurrentBufLength - aLength); |
|
90 iCurrentBufLength -= aLength; |
|
91 } |
|
92 |
|
93 const char* CDynamicWriteBuffer::Buffer() const |
|
94 { |
|
95 return iBuf; |
|
96 } |
|
97 |
|
98 int CDynamicWriteBuffer::Length() const |
|
99 { |
|
100 return iCurrentBufLength; |
|
101 } |
|
102 |
|
103 CDynamicWriteBuffer::CDynamicWriteBuffer() : iBuf(NULL), iMaxBufLength(0), iCurrentBufLength(0) |
|
104 { |
|
105 } |
|
106 |
|
107 void CDynamicWriteBuffer::Construct(int aInitialLength) |
|
108 { |
|
109 GrowBuf(aInitialLength); |
|
110 } |
|
111 |
|
112 void CDynamicWriteBuffer::GrowBuf(int aNewLength) |
|
113 { |
|
114 ASSERT(aNewLength > iMaxBufLength); |
|
115 char* newBuf = new(EThrow) char[aNewLength]; |
|
116 if (iBuf) |
|
117 { |
|
118 memcpy(newBuf, iBuf, iCurrentBufLength); |
|
119 delete iBuf; |
|
120 } |
|
121 iBuf = newBuf; |
|
122 iMaxBufLength = aNewLength; |
|
123 } |