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