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

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

Added CVS ID lines to all source files

File size: 12.1 KB
Line 
1/* $Id: oString.cpp,v 1.5 1999-09-08 11:29:27 davidr Exp $ */
2/*
3 *
4 * Project Odin Software License can be found in LICENSE.TXT
5 *
6 */
7/*
8 * ClassID Manipulation.
9 *
10 * 12/7/99
11 *
12 * Copyright 1999 David J. Raison
13 *
14 */
15
16#include "ole32.h"
17
18#include "oString.h"
19
20// ======================================================================
21// oStringBase - Base stuff for oString classes
22// ======================================================================
23
24// Block size.
25long oStringBase::blockSize = 32;
26
27// Initialise to nothing...
28oStringBase::oStringBase(long bytePerChar)
29{
30 m_bytePerChar = bytePerChar;
31 m_Heap = GetProcessHeap();
32}
33
34// Destructor
35oStringBase::~oStringBase()
36{
37 freeBuf();
38}
39
40// calculate the buffersize needed for this string (quantised to blockSize)
41long oStringBase::calcBufLen(long strLen)
42{
43 long tmp;
44
45 tmp = ((strLen * m_bytePerChar) / blockSize) + 1;
46
47 return tmp * blockSize;
48}
49
50// Get a buffer of required length...
51void oStringBase::getBuf(long strLen)
52{
53 // Sanity check...
54 if (strLen < 1)
55 {
56 dprintf(("oStringBase: Warning! ::getBuf - strlen < 1!"));
57 strLen = 1;
58 }
59
60 // Allocate new buffer...
61 m_bufLen = calcBufLen(strLen);
62 m_strBuf = (LPSTR)HeapAlloc(m_Heap, 0, m_bufLen);
63 m_strLen = 1;
64 m_strBuf[0] = 0; // Ensure terminated.
65}
66
67// Free buffer...
68void oStringBase::freeBuf()
69{
70 HeapFree(m_Heap, 0, m_strBuf);
71}
72
73// Re-allocate buffer if more space is required...
74void oStringBase::adjBuf(long strLen)
75{
76 if ((strLen * m_bytePerChar) > m_bufLen)
77 {
78 long bufLen = calcBufLen(strLen);
79
80 // Allocate new buffer...
81 m_strBuf = (LPSTR)HeapReAlloc(m_Heap, 0, m_strBuf, bufLen);
82
83 // Save new buffer...
84 m_bufLen = bufLen;
85 }
86}
87
88// ======================================================================
89// oStringA - String manipulation based on ASCII
90// ======================================================================
91
92// Initialise to nothing...
93oStringA::oStringA( void) : oStringBase(sizeof(char))
94{
95 getBuf( 1); // Incl. terminator...
96}
97
98// Initialise to a specified length...
99oStringA::oStringA(int defLen, int fill) : oStringBase(sizeof(char))
100{
101 getBuf( defLen + 1);
102}
103
104// Initialise from Unicode string...
105oStringA::oStringA(LPCWSTR pUnicode) : oStringBase(sizeof(char))
106{
107 long strLen = lstrlenW(pUnicode) + 1;
108
109 getBuf( strLen);
110 UnicodeToAscii((LPWSTR)pUnicode, m_strBuf);
111 m_strLen = strLen;
112}
113
114// Initialise from ASCII string...
115oStringA::oStringA(LPCSTR pAscii) : oStringBase(sizeof(char))
116{
117 long strLen = strlen(pAscii) + 1;
118
119 getBuf( strLen);
120 strcpy(m_strBuf, pAscii);
121 m_strLen = strLen;
122}
123
124// Initialise from another oString
125oStringA::oStringA(const oStringA &ref) : oStringBase(sizeof(char))
126{
127 getBuf( ref.m_strLen);
128 strcpy(m_strBuf, ref.m_strBuf);
129 m_strLen = ref.m_strLen;
130}
131
132// Initialise from CLSID
133oStringA::oStringA( REFCLSID pClsId) : oStringBase(sizeof(char))
134{
135 getBuf( 50); // Incl. terminator...
136
137 // Assign string...
138 operator=(pClsId);
139}
140
141oStringA::oStringA(const ULONG val) : oStringBase(sizeof(char))
142{
143 getBuf( 30); // Incl. terminator...
144 m_strLen = sprintf(m_strBuf, "%lu", val);
145}
146
147oStringA::oStringA(const LONG val) : oStringBase(sizeof(char))
148{
149 getBuf( 30); // Incl. terminator...
150 m_strLen = sprintf(m_strBuf, "%ld", val);
151}
152
153oStringA::oStringA(const USHORT val) : oStringBase(sizeof(char))
154{
155 getBuf( 30); // Incl. terminator...
156 m_strLen = sprintf(m_strBuf, "%u", val);
157}
158
159oStringA::oStringA(const SHORT val) : oStringBase(sizeof(char))
160{
161 getBuf( 30); // Incl. terminator...
162 m_strLen = sprintf(m_strBuf, "%d", val);
163}
164
165// Return pointer to string...
166oStringA::operator LPSTR()
167{
168 return (LPSTR)m_strBuf;
169}
170
171
172// Assign
173oStringA oStringA::operator = (const oStringA & string)
174{
175 // Lose old string...
176 freeBuf();
177
178 // Setup new string...
179 getBuf( string.m_strLen);
180 strcpy(m_strBuf, string.m_strBuf);
181 m_strLen = string.m_strLen;
182 return *this;
183}
184
185oStringA oStringA::operator = (LPCSTR pAscii)
186{
187 long strLen = strlen(pAscii) + 1;
188
189 // Lose old string...
190 freeBuf();
191
192 // Setup new string...
193 getBuf( strLen);
194 strcpy(m_strBuf, pAscii);
195 m_strLen = strLen;
196 return *this;
197}
198
199oStringA oStringA::operator = (LPCWSTR pUnicode)
200{
201 long strLen = lstrlenW(pUnicode) + 1;
202
203 // Lose old string...
204 freeBuf();
205
206 // Setup new string...
207 getBuf( strLen);
208 UnicodeToAscii((LPWSTR)pUnicode, m_strBuf);
209 m_strLen = strLen;
210 return *this;
211}
212
213oStringA oStringA::operator = (REFCLSID rClsId)
214{
215 // Lose old string...
216 freeBuf();
217
218 // Setup new string...
219 getBuf( 50);
220 WINE_StringFromCLSID(rClsId, m_strBuf);
221 m_strLen = strlen(m_strBuf);
222
223 return *this;
224}
225
226// Add String to String
227oStringA oStringA::operator + (const oStringA & string)
228{
229 // two terminators to account for...
230 oStringA product(m_strLen + string.m_strLen - 1, 1);
231
232 memcpy(product.m_strBuf, m_strBuf, m_strLen);
233 strcpy(product.m_strBuf + m_strLen - 1, string.m_strBuf);
234 product.m_strLen = m_strLen + string.m_strLen - 1;
235
236 return product;
237}
238
239// Add String to ASCII string
240oStringA oStringA::operator + (LPCSTR pAscii)
241{
242 long strLen = m_strLen + strlen(pAscii);
243
244 oStringA product(strLen, 1);
245
246 memcpy(product.m_strBuf, m_strBuf, m_strLen);
247 strcpy(product.m_strBuf + m_strLen - 1, pAscii);
248 product.m_strLen = strLen;
249
250 return product;
251}
252
253// Add String to UNICODE string
254oStringA oStringA::operator + (LPCWSTR pUnicode)
255{
256 long strLen = m_strLen + lstrlenW(pUnicode);
257
258 oStringA product(strLen, 1);
259
260 memcpy(product.m_strBuf, m_strBuf, m_strLen);
261 UnicodeToAscii((LPWSTR)pUnicode, product.m_strBuf + m_strLen - 1);
262 product.m_strLen = strLen;
263
264 return product;
265}
266
267// Concatenate string object
268oStringA oStringA::operator += (const oStringA & string)
269{
270 // two terminators to account for...
271 long strLen = m_strLen + string.m_strLen - 1;
272
273 adjBuf( strLen);
274 strcpy(m_strBuf + m_strLen - 1, string.m_strBuf);
275 m_strLen = strLen;
276
277 return *this;
278}
279
280// Concatenate Ascii string
281oStringA oStringA::operator += (LPCSTR pAscii)
282{
283 long strLen = m_strLen + strlen(pAscii);
284
285 adjBuf( strLen);
286 strcpy(m_strBuf + m_strLen - 1, pAscii);
287 m_strLen = strLen;
288
289 return *this;
290}
291
292// Concatenate Unicode string
293oStringA oStringA::operator += (LPCWSTR pUnicode)
294{
295 long strLen = m_strLen + lstrlenW(pUnicode);
296
297 adjBuf( strLen);
298 UnicodeToAscii((LPWSTR)pUnicode, m_strBuf + m_strLen - 1);
299 m_strLen = strLen;
300
301 return *this;
302}
303
304// ======================================================================
305// oStringW - String manipulation based on Unicode
306// ======================================================================
307
308// Initialise to nothing...
309oStringW::oStringW( void) : oStringBase(sizeof(WCHAR))
310{
311 getBuf( 1); // Incl. terminator...
312}
313
314// Initialise to a specified length...
315oStringW::oStringW(int defLen, int fill) : oStringBase(sizeof(WCHAR))
316{
317 getBuf( defLen + 1);
318}
319
320// Initialise from Unicode string...
321oStringW::oStringW(LPCWSTR pUnicode) : oStringBase(sizeof(WCHAR))
322{
323 long strLen = lstrlenW((const WCHAR *)pUnicode) + 1;
324
325 getBuf(strLen);
326 lstrcpyW((LPWSTR)m_strBuf, pUnicode);
327 m_strLen = strLen;
328}
329
330// Initialise from Unicode string...
331oStringW::oStringW(const wchar_t * pUnicode) : oStringBase(sizeof(WCHAR))
332{
333 long strLen = lstrlenW((LPWSTR)pUnicode) + 1;
334
335 getBuf(strLen);
336 lstrcpyW((LPWSTR)m_strBuf, (LPCWSTR)pUnicode);
337 m_strLen = strLen;
338}
339
340// Initialise from ASCII string...
341oStringW::oStringW(LPCSTR pAscii) : oStringBase(sizeof(WCHAR))
342{
343 long strLen = strlen(pAscii) + 1;
344
345 getBuf( strLen);
346 AsciiToUnicode((char *)pAscii, (LPWSTR)m_strBuf);
347 m_strLen = strLen;
348}
349
350// Initialise from another oString
351oStringW::oStringW(const oStringW &ref) : oStringBase(sizeof(WCHAR))
352{
353 getBuf( ref.m_strLen);
354 lstrcpyW((LPWSTR)m_strBuf, (LPWSTR)ref.m_strBuf);
355 m_strLen = ref.m_strLen;
356}
357
358// Initialise from CLSID
359oStringW::oStringW( REFCLSID pClsId) : oStringBase(sizeof(WCHAR))
360{
361 getBuf( 50); // Incl. terminator...
362
363 // Assign string...
364 operator=(pClsId);
365}
366
367// Return pointer to string...
368oStringW::operator LPWSTR()
369{
370 return (LPWSTR)m_strBuf;
371}
372
373// Assign
374oStringW oStringW::operator = (const oStringW & string)
375{
376 // Lose old string...
377 freeBuf();
378
379 // Setup new string...
380 getBuf( string.m_strLen);
381 lstrcpyW((LPWSTR)m_strBuf, (LPWSTR)string.m_strBuf);
382 m_strLen = string.m_strLen;
383 return *this;
384}
385
386oStringW oStringW::operator = (LPCSTR pAscii)
387{
388 long strLen = strlen(pAscii) + 1;
389
390 // Lose old string...
391 freeBuf();
392
393 // Setup new string...
394 getBuf( strLen);
395 AsciiToUnicode((char *)pAscii, (LPWSTR)m_strBuf);
396 m_strLen = strLen;
397 return *this;
398}
399
400oStringW oStringW::operator = (LPCWSTR pUnicode)
401{
402 long strLen = lstrlenW(pUnicode) + 1;
403
404 // Lose old string...
405 freeBuf();
406
407 // Setup new string...
408 getBuf( strLen);
409 lstrcpyW((LPWSTR)m_strBuf, pUnicode);
410 m_strLen = strLen;
411 return *this;
412}
413
414oStringW oStringW::operator = (const wchar_t * pUnicode)
415{
416 long strLen = lstrlenW((LPWSTR)pUnicode) + 1;
417
418 // Lose old string...
419 freeBuf();
420
421 // Setup new string...
422 getBuf( strLen);
423 lstrcpyW((LPWSTR)m_strBuf, (LPWSTR)pUnicode);
424 m_strLen = strLen;
425 return *this;
426}
427
428oStringW oStringW::operator = (REFCLSID rClsId)
429{
430 char tmp[50];
431
432 // Lose old string...
433 freeBuf();
434
435 // Setup new string...
436 getBuf( 50);
437 WINE_StringFromCLSID(rClsId, tmp);
438 m_strLen = strlen(tmp);
439 AsciiToUnicode(tmp, (LPWSTR)m_strBuf);
440 return *this;
441}
442
443// Add String to String
444oStringW oStringW::operator + (const oStringW & string)
445{
446 // two terminators to account for...
447 oStringW product(m_strLen + string.m_strLen - 1, 1);
448
449 lstrcpyW((LPWSTR)product.m_strBuf, (LPWSTR)m_strBuf);
450 lstrcpyW((LPWSTR)product.m_strBuf + m_strLen - 1, (LPWSTR)string.m_strBuf);
451 product.m_strLen = m_strLen + string.m_strLen - 1;
452
453 return product;
454}
455
456// Add String to ASCII string
457oStringW oStringW::operator + (LPCSTR pAscii)
458{
459 long strLen = m_strLen + strlen(pAscii);
460
461 oStringW product(strLen, 1);
462
463 lstrcpyW((LPWSTR)product.m_strBuf, (LPWSTR)m_strBuf);
464 AsciiToUnicode((char *)pAscii, (LPWSTR)product.m_strBuf + m_strLen - 1);
465 product.m_strLen = strLen;
466
467 return product;
468}
469
470// Add String to UNICODE string
471oStringW oStringW::operator + (LPCWSTR pUnicode)
472{
473 long strLen = m_strLen + lstrlenW(pUnicode);
474
475 oStringW product(strLen, 1);
476
477 lstrcpyW((LPWSTR)product.m_strBuf, (LPWSTR)m_strBuf);
478 lstrcpyW((LPWSTR)product.m_strBuf + m_strLen - 1, (LPWSTR)pUnicode);
479 product.m_strLen = strLen;
480
481 return product;
482}
483
484// Add String to UNICODE string
485oStringW oStringW::operator + (const wchar_t * pUnicode)
486{
487 long strLen = m_strLen + lstrlenW((LPWSTR)pUnicode);
488
489 oStringW product(strLen, 1);
490
491 lstrcpyW((LPWSTR)product.m_strBuf, (LPWSTR)m_strBuf);
492 lstrcpyW((LPWSTR)product.m_strBuf + m_strLen - 1, (LPWSTR)pUnicode);
493 product.m_strLen = strLen;
494
495 return product;
496}
497
498// Concatenate string object
499oStringW oStringW::operator += (const oStringW & string)
500{
501 // two terminators to account for...
502 long strLen = m_strLen + string.m_strLen - 1;
503
504 adjBuf( strLen);
505 lstrcpyW((LPWSTR)m_strBuf + m_strLen - 1, (LPWSTR)string.m_strBuf);
506 m_strLen = strLen;
507
508 return *this;
509}
510
511// Concatenate Ascii string
512oStringW oStringW::operator += (LPCSTR pAscii)
513{
514 long strLen = m_strLen + strlen(pAscii);
515
516 adjBuf( strLen);
517 AsciiToUnicode((char *)pAscii, (LPWSTR)m_strBuf + m_strLen - 1);
518 m_strLen = strLen;
519
520 return *this;
521}
522
523// Concatenate Unicode string
524oStringW oStringW::operator += (LPCWSTR pUnicode)
525{
526 long strLen = m_strLen + lstrlenW(pUnicode);
527
528 adjBuf( strLen);
529 lstrcpyW((LPWSTR)m_strBuf + m_strLen - 1, (LPWSTR)pUnicode);
530 m_strLen = strLen;
531
532 return *this;
533}
534
535// Concatenate Unicode string
536oStringW oStringW::operator += (const wchar_t * pUnicode)
537{
538 long strLen = m_strLen + lstrlenW((LPWSTR)pUnicode);
539
540 adjBuf( strLen);
541 lstrcpyW((LPWSTR)m_strBuf + m_strLen - 1, (LPWSTR)pUnicode);
542 m_strLen = strLen;
543
544 return *this;
545}
546
547
Note: See TracBrowser for help on using the repository browser.