|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // e32\debug\crashMonitor\src\scmbytestreamutil.cpp |
|
15 // some utility classes for writing data to flash buffer |
|
16 // |
|
17 // |
|
18 |
|
19 /** |
|
20 @file |
|
21 @internalTechnology |
|
22 */ |
|
23 |
|
24 |
|
25 #include "scmbytestreamutil.h" |
|
26 #include "scmtrace.h" |
|
27 |
|
28 |
|
29 |
|
30 namespace Debug |
|
31 { |
|
32 /** |
|
33 * TByteStreamBase Constructor |
|
34 * @param aBuffer - pointer to buffer that this utility will use |
|
35 */ |
|
36 TByteStreamBase::TByteStreamBase(TUint8* aBuffer) |
|
37 : iBuffer(aBuffer) |
|
38 , iPos(0) |
|
39 { |
|
40 } |
|
41 |
|
42 /** |
|
43 * SetPosition |
|
44 * @param aBuffer - Sets iPos |
|
45 * @return void |
|
46 */ |
|
47 void TByteStreamBase::SetPosition(TInt aPos) |
|
48 { |
|
49 iPos = aPos; |
|
50 } |
|
51 |
|
52 /** |
|
53 * CurrentPosition |
|
54 * @param aBuffer - Returns the current value of iPos |
|
55 * @return Tint |
|
56 */ |
|
57 TInt TByteStreamBase::CurrentPosition() const |
|
58 { |
|
59 return iPos; |
|
60 } |
|
61 |
|
62 /** |
|
63 * TByteStreamReader Constructor |
|
64 * @param aBuffer - pointer to buffer that this utility will use |
|
65 */ |
|
66 TByteStreamReader::TByteStreamReader(TUint8* aBuffer) |
|
67 : TByteStreamBase(aBuffer) |
|
68 { |
|
69 } |
|
70 |
|
71 |
|
72 /** |
|
73 * Constructor for TByteStreamWriter |
|
74 * @param aBuffer buffer for writing |
|
75 * @param aPhysEnabled whether or not physical writing to another media is enabled |
|
76 */ |
|
77 TByteStreamWriter::TByteStreamWriter(TUint8* aBuffer, TBool aPhysEnabled) |
|
78 : TByteStreamBase(aBuffer) |
|
79 , iPhysEnabled(aPhysEnabled) |
|
80 , iBytesWritten(0) |
|
81 { |
|
82 } |
|
83 |
|
84 /** |
|
85 * Writes a byte to the buffer |
|
86 * @param aValue Byte to write |
|
87 */ |
|
88 void TByteStreamWriter::WriteByte(TUint8 aValue) |
|
89 { |
|
90 if(iBuffer) |
|
91 { |
|
92 iBuffer[iPos++] = aValue; |
|
93 ++iBytesWritten; |
|
94 } |
|
95 } |
|
96 |
|
97 /** |
|
98 * Resets the byte counter back to zero |
|
99 */ |
|
100 void TByteStreamWriter::ResetBytesWritten() |
|
101 { |
|
102 iBytesWritten = 0; |
|
103 } |
|
104 |
|
105 /** |
|
106 * TCachedByteStreamWriter Constructor |
|
107 * @param aBuffer - pointer to buffer that this utility will use |
|
108 * @param aCacheSize - suggested length of cache to use if greater than EMaxCache |
|
109 * cache length of EMaxCache will be used |
|
110 */ |
|
111 TCachedByteStreamWriter::TCachedByteStreamWriter(TUint8* aCacheBuffer, TInt aCacheSize, TBool aPhysEnabled) |
|
112 : TByteStreamWriter(NULL, aPhysEnabled) |
|
113 , iCacheSize(aCacheSize) |
|
114 , iCacheBuffer(aCacheBuffer) |
|
115 , iPhysicalWriter(NULL) |
|
116 { |
|
117 } |
|
118 |
|
119 |
|
120 /** |
|
121 * FlushCache |
|
122 * Writes the contents of the cache to buffer amd/or to physical writer implementation |
|
123 * if one is currently set |
|
124 * @return void |
|
125 */ |
|
126 TInt TCachedByteStreamWriter::FlushCache() |
|
127 { |
|
128 TInt padCount = iCacheSize - iPos; |
|
129 if(padCount > 0) |
|
130 { |
|
131 for(TInt i=0;i<padCount;i++) |
|
132 { |
|
133 iCacheBuffer[iPos++] = 0; |
|
134 } |
|
135 } |
|
136 |
|
137 if(iPhysEnabled) |
|
138 { |
|
139 if(iPhysicalWriter) // do we have a writer to send the cache data to |
|
140 { |
|
141 iPhysicalWriter->DoPhysicalWrite(iCacheBuffer, iBytesWritten, iPos); |
|
142 } |
|
143 } |
|
144 |
|
145 iPos = 0; |
|
146 return KErrNone; |
|
147 } |
|
148 |
|
149 /** |
|
150 * Writes a byte to the cached buffer |
|
151 * @param aValue Byte to write |
|
152 */ |
|
153 void TCachedByteStreamWriter::WriteByte(TUint8 aValue) |
|
154 { |
|
155 if(iPos == iCacheSize) |
|
156 { |
|
157 FlushCache(); |
|
158 } |
|
159 iCacheBuffer[iPos++] = aValue; |
|
160 ++iBytesWritten; |
|
161 } |
|
162 |
|
163 /** |
|
164 * CurrentPosition |
|
165 * @param aBuffer - need to return the position in buffer plus the write pos into cache |
|
166 * @return Tint |
|
167 */ |
|
168 TInt TCachedByteStreamWriter::CurrentPosition() const |
|
169 { |
|
170 return iBytesWritten; |
|
171 } |
|
172 |
|
173 /** |
|
174 * SetWriterImpl |
|
175 * @param aPhysicalWriter - sets the physical writer implementation to be used when the cache is flushed |
|
176 * pass NULL to remove a previous writer implementation |
|
177 * @return void |
|
178 */ |
|
179 void TCachedByteStreamWriter::SetWriterImpl(MPhysicalWriterImpl* aPhysicalWriter) |
|
180 { |
|
181 iPhysicalWriter = aPhysicalWriter; |
|
182 } |
|
183 } |
|
184 |
|
185 //eof |
|
186 |