|
1 /* |
|
2 * Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "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 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include "bsymutil.h" |
|
19 #include <stdio.h> |
|
20 |
|
21 |
|
22 MemoryWriter::MemoryWriter() |
|
23 { |
|
24 iChar = new char[4*MaxSize]; |
|
25 iOffset = 0; |
|
26 iTotalSize = 4*MaxSize; |
|
27 iStringTableStart = 0; |
|
28 } |
|
29 MemoryWriter::~MemoryWriter() |
|
30 { |
|
31 delete[] iChar; |
|
32 } |
|
33 int MemoryWriter::WriteBytes(const char* pChar, int size) |
|
34 { |
|
35 while(iOffset + size > iTotalSize) |
|
36 { |
|
37 ExtendMemory(); |
|
38 } |
|
39 memcpy(iChar + iOffset, pChar, size); |
|
40 iOffset += size; |
|
41 return size; |
|
42 } |
|
43 TUint32 MemoryWriter::GetOffset() |
|
44 { |
|
45 return iOffset; |
|
46 } |
|
47 char* MemoryWriter::GetDataPointer() |
|
48 { |
|
49 return iChar; |
|
50 } |
|
51 bool MemoryWriter::ExtendMemory() |
|
52 { |
|
53 char* pTmp = new char[iTotalSize + MaxSize]; |
|
54 memcpy(pTmp, iChar, iOffset); |
|
55 delete[] iChar; |
|
56 iChar = pTmp; |
|
57 iTotalSize += MaxSize; |
|
58 return true; |
|
59 } |
|
60 bool MemoryWriter::SetOffset(TUint32 aOffset) |
|
61 { |
|
62 while(aOffset > iTotalSize) |
|
63 { |
|
64 ExtendMemory(); |
|
65 } |
|
66 iOffset = aOffset; |
|
67 return true; |
|
68 } |
|
69 void MemoryWriter::AddEmptyString() |
|
70 { |
|
71 unsigned char len = 0; |
|
72 WriteBytes((char *)&len, 1); |
|
73 } |
|
74 TUint32 MemoryWriter::AddString(const string& aStr) |
|
75 { |
|
76 TUint32 result = 0; |
|
77 if(aStr.empty()) |
|
78 return result; |
|
79 result = iOffset - iStringTableStart; |
|
80 int len = aStr.length(); |
|
81 if(len >= 255) |
|
82 { |
|
83 TUint16 wlen = len; |
|
84 unsigned char mark = 0xff; |
|
85 WriteBytes((char*)&mark, 1); |
|
86 WriteBytes((char*)&wlen, 2); |
|
87 WriteBytes(aStr.c_str(), len); |
|
88 } |
|
89 else |
|
90 { |
|
91 unsigned char clen = len; |
|
92 WriteBytes((char *)&clen, 1); |
|
93 WriteBytes(aStr.c_str(), len); |
|
94 } |
|
95 return result; |
|
96 } |
|
97 TUint32 MemoryWriter::AddScopeName(const string& aStr) |
|
98 { |
|
99 TUint32 result = 0; |
|
100 if(aStr.empty()) |
|
101 return result; |
|
102 if(aStr == iLastScopeName) |
|
103 { |
|
104 return iLastScopeNameOffset; |
|
105 } |
|
106 else |
|
107 { |
|
108 iLastScopeName = aStr; |
|
109 iLastScopeNameOffset = AddString(aStr); |
|
110 } |
|
111 return iLastScopeNameOffset; |
|
112 } |
|
113 void MemoryWriter::SetStringTableStart(TUint32 aOffset) |
|
114 { |
|
115 iStringTableStart = aOffset; |
|
116 } |
|
117 TUint32 MemoryWriter::GetStringTableStart() |
|
118 { |
|
119 return iStringTableStart; |
|
120 } |