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

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

Initial port of OLE2.
Added regsvr32

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