source: trunk/src/ole32/oString.cpp@ 201

Last change on this file since 201 was 201, checked in by davidr, 26 years ago

String Handling Class

File size: 4.9 KB
Line 
1
2#include <string.h>
3
4// Prevent CALLCONV becoming _System
5#include <winbase.h>
6#include <uconv.h>
7#undef CALLCONV
8
9#include <os2win.h>
10
11#include "oString.h"
12
13// Block size.
14long oStringA::blockSize = 32;
15
16// calculate the buffersize needed for this string (quantised to blockSize)
17long oStringA::calcBufLen(long strLen)
18{
19 long tmp;
20
21 tmp = (strLen / blockSize) + 1;
22
23 return tmp * blockSize;
24}
25
26// Get a buffer of required length...
27void oStringA::getBuf(long strLen)
28{
29 // Sanity check...
30 if (strLen < 1)
31 {
32 dprintf(("Warning: oStringA::getBuf - strlen < 1!"));
33 strLen = 1;
34 }
35
36 // Allocate new buffer...
37 m_bufLen = calcBufLen(strLen);
38 m_strBuf = (LPSTR)HeapAlloc(m_Heap, 0, m_bufLen);
39 m_strLen = 1;
40 m_strBuf[0] = 0; // Ensure terminated.
41}
42
43// Free buffer...
44void oStringA::freeBuf()
45{
46 HeapFree(m_Heap, 0, m_strBuf);
47}
48
49// Re-allocate buffer if more space is required...
50void oStringA::adjBuf(long strLen)
51{
52 if (strLen > m_bufLen)
53 {
54 LPSTR strBuf;
55 long bufLen = calcBufLen(strLen);
56
57 // Allocate new buffer...
58 strBuf = (LPSTR)HeapAlloc(m_Heap, 0, bufLen);
59
60 // Copy old buffer to new...
61 strcpy(strBuf, m_strBuf); // Incl. terminator...
62 freeBuf();
63
64 // Save new buffer...
65 m_strBuf = strBuf;
66 m_bufLen = bufLen;
67 }
68}
69
70// Initialise to nothing...
71oStringA::oStringA( void)
72{
73 m_Heap = GetProcessHeap();
74 getBuf(1); // Incl. terminator...
75}
76
77// Initialise to a specified length...
78oStringA::oStringA(int defLen)
79{
80 m_Heap = GetProcessHeap();
81 getBuf(defLen + 1);
82}
83
84// Initialise from Unicode string...
85oStringA::oStringA(LPCWSTR pUnicode)
86{
87 long strLen = lstrlenW(pUnicode) + 1;
88
89 m_Heap = GetProcessHeap();
90 getBuf(strLen);
91 UnicodeToAscii((LPWSTR)pUnicode, m_strBuf);
92 m_strLen = strLen;
93}
94
95// Initialise from ASCII string...
96oStringA::oStringA(LPCSTR pAscii)
97{
98 long strLen = strlen(pAscii) + 1;
99
100 m_Heap = GetProcessHeap();
101 getBuf(strLen);
102 strcpy(m_strBuf, pAscii);
103 m_strLen = strLen;
104}
105
106// Initialise from another oString
107oStringA::oStringA(const oStringA &ref)
108{
109 m_Heap = GetProcessHeap();
110 getBuf(ref.m_strLen);
111 strcpy(m_strBuf, ref.m_strBuf);
112 m_strLen = ref.m_strLen;
113}
114
115//Destroy resources...
116oStringA::~oStringA()
117{
118 freeBuf();
119}
120
121// Return pointer to string...
122oStringA::operator LPSTR()
123{
124 return m_strBuf;
125}
126
127
128// Assign
129oStringA oStringA::operator = (const oStringA & string)
130{
131 // Lose old string...
132 freeBuf();
133
134 // Setup new string...
135 getBuf(string.m_strLen);
136 strcpy(m_strBuf, string.m_strBuf);
137 m_strLen = string.m_strLen;
138 return *this;
139}
140
141oStringA oStringA::operator = (LPCSTR pAscii)
142{
143 long strLen = strlen(pAscii) + 1;
144
145 // Lose old string...
146 freeBuf();
147
148 // Setup new string...
149 getBuf(strLen);
150 strcpy(m_strBuf, pAscii);
151 m_strLen = strLen;
152 return *this;
153}
154
155oStringA oStringA::operator = (LPCWSTR pUnicode)
156{
157 long strLen = lstrlenW(pUnicode) + 1;
158
159 // Lose old string...
160 freeBuf();
161
162 // Setup new string...
163 getBuf(strLen);
164 UnicodeToAscii((LPWSTR)pUnicode, m_strBuf);
165 m_strLen = strLen;
166 return *this;
167}
168
169
170// Add String to String
171oStringA oStringA::operator + (const oStringA & string)
172{
173 // two terminators to account for...
174 oStringA product(m_strLen + string.m_strLen - 1);
175
176 memcpy(product.m_strBuf, m_strBuf, m_strLen);
177 strcpy(product.m_strBuf + m_strLen - 1, string.m_strBuf);
178 product.m_strLen = m_strLen + string.m_strLen - 1;
179
180 return product;
181}
182
183// Add String to ASCII string
184oStringA oStringA::operator + (LPCSTR pAscii)
185{
186 long strLen = m_strLen + strlen(pAscii);
187
188 oStringA product(strLen);
189
190 memcpy(product.m_strBuf, m_strBuf, m_strLen);
191 strcpy(product.m_strBuf + m_strLen - 1, pAscii);
192 product.m_strLen = strLen;
193
194 return product;
195}
196
197// Add String to UNICODE string
198oStringA oStringA::operator + (LPCWSTR pUnicode)
199{
200 long strLen = m_strLen + lstrlenW(pUnicode);
201
202 oStringA product(strLen);
203
204 memcpy(product.m_strBuf, m_strBuf, m_strLen);
205 UnicodeToAscii((LPWSTR)pUnicode, product.m_strBuf + m_strLen - 1);
206 product.m_strLen = strLen;
207
208 return product;
209}
210
211// Concatenate string object
212oStringA oStringA::operator += (const oStringA & string)
213{
214 // two terminators to account for...
215 long strLen = m_strLen + string.m_strLen - 1;
216
217 adjBuf(strLen);
218 strcpy(m_strBuf + m_strLen - 1, string.m_strBuf);
219 m_strLen = strLen;
220
221 return *this;
222}
223
224// Concatenate Ascii string
225oStringA oStringA::operator += (LPCSTR pAscii)
226{
227 long strLen = m_strLen + strlen(pAscii);
228
229 adjBuf(strLen);
230 strcpy(m_strBuf + m_strLen - 1, pAscii);
231 m_strLen = strLen;
232
233 return *this;
234}
235
236// Concatenate Unicode string
237oStringA oStringA::operator += (LPCWSTR pUnicode)
238{
239 long strLen = m_strLen + lstrlenW(pUnicode);
240
241 adjBuf(strLen);
242 UnicodeToAscii((LPWSTR)pUnicode, m_strBuf + m_strLen - 1);
243 m_strLen = strLen;
244
245 return *this;
246}
247
248
Note: See TracBrowser for help on using the repository browser.