1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 // -*- c++ -*- 19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 20 21 // O S C L _ S T R I N G C L A S S 22 23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 24 25 // - - Inclusion - - - - - - - - - - - - - - - - - - - - - - - - - - - - 26 27 #include "oscl_string_containers.h" 28 #include "oscl_stdstring.h" 29 #include "oscl_error.h" 30 31 // ************************************************************** 32 // CHeapRep Implementation 33 // ************************************************************** 34 35 OSCL_EXPORT_REF void CHeapRep::set_rep(CHeapRep*& aRep, Oscl_DefAlloc& aAlloc, const char* cp, uint32 len) 36 //set heap rep to new string. 37 { 38 CHeapRep*newrep = New(aAlloc); 39 if (newrep 40 && newrep->set(len, cp, aAlloc)) 41 { 42 CHeapRep::assign(aRep, newrep, aAlloc); 43 } 44 else 45 { 46 //could not allocate newrep or could not allocate buffer. 47 if (newrep) 48 aAlloc.deallocate(newrep); 49 OsclError::Leave(OsclErrNoMemory);//no memory 50 } 51 } 52 53 // ************************************************************** 54 OSCL_EXPORT_REF void CHeapRep::set_rep(CHeapRep*& aRep, Oscl_DefAlloc& aAlloc, const oscl_wchar* cp, uint32 len) 55 //set heap rep to new string. 56 { 57 CHeapRep*newrep = New(aAlloc); 58 if (newrep 59 && newrep->set(len, cp, aAlloc)) 60 { 61 CHeapRep::assign(aRep, newrep, aAlloc); 62 } 63 else 64 { 65 //could not allocate newrep or could not allocate buffer. 66 if (newrep) 67 aAlloc.deallocate(newrep); 68 OsclError::Leave(OsclErrNoMemory);//no memory 69 } 70 } 71 72 // ************************************************************** 73 OSCL_EXPORT_REF void CHeapRep::append_rep(CHeapRep*& aRep, Oscl_DefAlloc& aAlloc, const char* cp, uint32 len) 74 //set heap rep to current string plus new string. 75 { 76 CHeapRep*newrep = New(aAlloc); 77 if (newrep 78 && newrep->append((aRep) ? aRep->size : 0, (char*)((aRep) ? aRep->buffer : NULL), len, cp, aAlloc)) 79 { 80 CHeapRep::assign(aRep, newrep, aAlloc); 81 } 82 else 83 { 84 //could not allocate newrep or could not allocate buffer. 85 if (newrep) 86 aAlloc.deallocate(newrep); 87 OsclError::Leave(OsclErrNoMemory);//no memory 88 } 89 } 90 91 // ************************************************************** 92 OSCL_EXPORT_REF void CHeapRep::append_rep(CHeapRep*& aRep, Oscl_DefAlloc& aAlloc, const oscl_wchar* cp, uint32 len) 93 //set heap rep to current string plus new string. 94 { 95 CHeapRep*newrep = New(aAlloc); 96 if (newrep 97 && newrep->append((aRep) ? aRep->size : 0, (oscl_wchar*)((aRep) ? aRep->buffer : NULL), len, cp, aAlloc)) 98 { 99 CHeapRep::assign(aRep, newrep, aAlloc); 100 } 101 else 102 { 103 //could not allocate newrep or could not allocate buffer. 104 if (newrep) 105 aAlloc.deallocate(newrep); 106 OsclError::Leave(OsclErrNoMemory);//no memory 107 } 108 } 109 110 // ************************************************************** 111 OSCL_EXPORT_REF void CHeapRep::assign(CHeapRep*& dest, CHeapRep* src, Oscl_DefAlloc &alloc) 112 { 113 if (src) 114 src->add_ref(); 115 if (dest) 116 dest->remove_ref(alloc); 117 dest = src; 118 } 119 120 // ************************************************************** 121 CHeapRep* CHeapRep::New(Oscl_DefAlloc &alloc) 122 { 123 OsclAny *ptr = alloc.ALLOCATE(sizeof(CHeapRep)); 124 if (ptr) 125 { 126 CHeapRep*newrep = new(ptr) CHeapRep; 127 return newrep; 128 } 129 return NULL; 130 } 131 132 // ************************************************************** 133 OSCL_EXPORT_REF bool CHeapRep::set(uint32 nsz, const char*cp, Oscl_DefAlloc &alloc) 134 { 135 // allocate enough space including terminator 136 OsclAny* ptr = alloc.ALLOCATE(sizeof(char) * (nsz + 1)); 137 if (!ptr) 138 { 139 refcount = 0; 140 size = 0; 141 maxsize = 0; 142 buffer = NULL; 143 return false;//can't allocate buffer. 144 } 145 refcount = 0; 146 size = nsz; 147 maxsize = nsz; 148 buffer = ptr; 149 if (cp) 150 oscl_strncpy((char*)buffer, cp, size); 151 ((char*)buffer)[size] = '\0'; 152 return true; 153 } 154 OSCL_EXPORT_REF bool CHeapRep::set(uint32 nsz, const oscl_wchar*cp, Oscl_DefAlloc &alloc) 155 { 156 // allocate enough space including terminator 157 OsclAny* ptr = alloc.ALLOCATE(sizeof(oscl_wchar) * (nsz + 1)); 158 if (!ptr) 159 { 160 refcount = 0; 161 size = 0; 162 maxsize = 0; 163 buffer = NULL; 164 return false;//can't allocate buffer. 165 } 166 refcount = 0; 167 size = nsz; 168 maxsize = nsz; 169 buffer = ptr; 170 if (cp) 171 oscl_strncpy((oscl_wchar*)buffer, cp, size); 172 ((oscl_wchar*)buffer)[size] = '\0'; 173 return true; 174 } 175 176 // ************************************************************** 177 OSCL_EXPORT_REF bool CHeapRep::append(uint32 s1, const char*cp1, 178 uint32 s2, const char*cp2, 179 Oscl_DefAlloc &alloc) 180 { 181 maxsize = s1 + s2; 182 size = maxsize; 183 // allocate enough space including terminator 184 OsclAny* ptr = alloc.ALLOCATE(sizeof(char) * (maxsize + 1)); 185 if (!ptr) 186 { 187 refcount = 0; 188 size = 0; 189 maxsize = 0; 190 buffer = NULL; 191 return false;//can't allocate buffer. 192 } 193 refcount = 0; 194 buffer = ptr; 195 if (cp1) 196 oscl_strncpy((char*)buffer, cp1, s1); 197 ((char*)buffer)[s1] = '\0'; 198 if (cp2) 199 oscl_strncat((char*)buffer, cp2, s2); 200 ((char*)buffer)[size] = '\0'; 201 return true; 202 } 203 204 OSCL_EXPORT_REF bool CHeapRep::append(uint32 s1, const oscl_wchar*cp1, 205 uint32 s2, const oscl_wchar*cp2, 206 Oscl_DefAlloc &alloc) 207 { 208 maxsize = s1 + s2; 209 size = maxsize; 210 // allocate enough space including terminator 211 OsclAny* ptr = alloc.ALLOCATE(sizeof(oscl_wchar) * (maxsize + 1)); 212 if (!ptr) 213 { 214 refcount = 0; 215 size = 0; 216 maxsize = 0; 217 buffer = NULL; 218 return false;//can't allocate buffer. 219 } 220 refcount = 0; 221 buffer = ptr; 222 if (cp1) 223 oscl_strncpy((oscl_wchar*)buffer, cp1, s1); 224 ((oscl_wchar*)buffer)[s1] = '\0'; 225 if (cp2) 226 oscl_strncat((oscl_wchar*)buffer, cp2, s2); 227 ((oscl_wchar*)buffer)[size] = '\0'; 228 return true; 229 } 230 231 // ************************************************************** 232 OSCL_EXPORT_REF void CHeapRep::add_ref() 233 { 234 refcount++; 235 } 236 237 // ************************************************************** 238 OSCL_EXPORT_REF void CHeapRep::remove_ref(Oscl_DefAlloc &alloc) 239 { 240 --refcount; 241 if (refcount == 0) 242 { 243 alloc.deallocate(buffer); 244 alloc.deallocate(this); 245 } 246 } 247 248 // ************************************************************** 249 // OSCL_HeapStringA Implementation 250 // OSCL_wHeapStringA Implementation 251 // ************************************************************** 252 253 void OSCL_HeapStringA::set_rep(const chartype* cp, uint32 len) 254 //set heap rep to new string. 255 { 256 CHeapRep::set_rep(iRep, *iAlloc, cp, len); 257 } 258 259 void OSCL_wHeapStringA::set_rep(const chartype* cp, uint32 len) 260 //set heap rep to new string. 261 { 262 CHeapRep::set_rep(iRep, *iAlloc, cp, len); 263 } 264 265 // ************************************************************** 266 void OSCL_HeapStringA::append_rep(const chartype* cp, uint32 len) 267 //set heap rep to current string plus new string. 268 { 269 CHeapRep::append_rep(iRep, *iAlloc, cp, len); 270 } 271 272 void OSCL_wHeapStringA::append_rep(const chartype* cp, uint32 len) 273 //set heap rep to current string plus new string. 274 { 275 CHeapRep::append_rep(iRep, *iAlloc, cp, len); 276 } 277 278 // ************************************************************** 279 void OSCL_HeapStringA::set_rep(const chartype* cp) 280 { 281 uint32 len = (cp) ? oscl_strlen(cp) : 0; 282 set_rep(cp, len); 283 } 284 285 void OSCL_wHeapStringA::set_rep(const chartype* cp) 286 { 287 uint32 len = (cp) ? oscl_strlen(cp) : 0; 288 set_rep(cp, len); 289 } 290 291 292 // ************************************************************** 293 void OSCL_HeapStringA::append_rep(const chartype* cp) 294 { 295 uint32 len = (cp) ? oscl_strlen(cp) : 0; 296 append_rep(cp, len); 297 } 298 299 void OSCL_wHeapStringA::append_rep(const chartype* cp) 300 { 301 uint32 len = (cp) ? oscl_strlen(cp) : 0; 302 append_rep(cp, len); 303 } 304 305 306 // ************************************************************** 307 void OSCL_HeapStringA::set_rep(const OSCL_String& src) 308 { 309 set_rep(src.get_cstr(), src.get_size()); 310 } 311 312 void OSCL_wHeapStringA::set_rep(const OSCL_wString& src) 313 { 314 set_rep(src.get_cstr(), src.get_size()); 315 } 316 317 318 // ************************************************************** 319 void OSCL_HeapStringA::append_rep(const OSCL_String& src) 320 { 321 append_rep(src.get_cstr(), src.get_size()); 322 } 323 324 void OSCL_wHeapStringA::append_rep(const OSCL_wString& src) 325 { 326 append_rep(src.get_cstr(), src.get_size()); 327 } 328 329 330 // ************************************************************** 331 OSCL_EXPORT_REF uint32 OSCL_HeapStringA::get_size() const 332 { 333 if (iRep) 334 return iRep->size; 335 return 0; 336 } 337 338 OSCL_EXPORT_REF uint32 OSCL_wHeapStringA::get_size() const 339 { 340 if (iRep) 341 return iRep->size; 342 return 0; 343 } 344 345 // ************************************************************** 346 void OSCL_HeapStringA::set_len(uint32 len) 347 { 348 iRep->size = len; 349 } 350 351 void OSCL_wHeapStringA::set_len(uint32 len) 352 { 353 iRep->size = len; 354 } 355 356 357 // ************************************************************** 358 OSCL_EXPORT_REF uint32 OSCL_HeapStringA::get_maxsize() const 359 { 360 if (iRep) 361 return iRep->maxsize; 362 return 0; 363 } 364 365 OSCL_EXPORT_REF uint32 OSCL_wHeapStringA::get_maxsize() const 366 { 367 if (iRep) 368 return iRep->maxsize; 369 return 0; 370 } 371 372 373 // ************************************************************** 374 OSCL_EXPORT_REF const OSCL_HeapStringA::chartype* OSCL_HeapStringA::get_cstr() const 375 { 376 if (iRep) 377 return (chartype*)iRep->buffer; 378 return NULL; 379 } 380 381 OSCL_EXPORT_REF const OSCL_wHeapStringA::chartype* OSCL_wHeapStringA::get_cstr() const 382 { 383 if (iRep) 384 return (chartype*)iRep->buffer; 385 return NULL; 386 } 387 388 389 // ************************************************************** 390 OSCL_EXPORT_REF OSCL_HeapStringA::chartype* OSCL_HeapStringA::get_str() const 391 { 392 if (iRep) 393 return (chartype*)iRep->buffer; 394 return NULL; 395 } 396 397 OSCL_EXPORT_REF OSCL_wHeapStringA::chartype* OSCL_wHeapStringA::get_str() const 398 { 399 if (iRep) 400 return (chartype*)iRep->buffer; 401 return NULL; 402 } 403 404 405 // ************************************************************** 406 void OSCL_HeapStringA::create(Oscl_DefAlloc *alloc, OsclRefCounter *ref) 407 { 408 iRep = NULL; 409 if (alloc) 410 {//use an allocator owned by the caller that may also have a ref counter. 411 iAllocRef = ref; 412 if (iAllocRef) 413 iAllocRef->addRef(); 414 iAlloc = alloc; 415 } 416 else 417 {//use a basic allocator that resides in this object. 418 iAlloc = &iDefAlloc; 419 iAllocRef = NULL; 420 } 421 } 422 423 void OSCL_wHeapStringA::create(Oscl_DefAlloc *alloc, OsclRefCounter *ref) 424 { 425 iRep = NULL; 426 if (alloc) 427 {//use an allocator owned by the caller that may also have a ref counter. 428 iAllocRef = ref; 429 if (iAllocRef) 430 iAllocRef->addRef(); 431 iAlloc = alloc; 432 } 433 else 434 {//use a basic allocator that resides in this object. 435 iAlloc = &iDefAlloc; 436 iAllocRef = NULL; 437 } 438 } 439 440 441 // ************************************************************** 442 OSCL_EXPORT_REF OSCL_HeapStringA::OSCL_HeapStringA() 443 { 444 create(NULL, NULL); 445 set_rep(NULL); 446 } 447 OSCL_EXPORT_REF OSCL_HeapStringA::OSCL_HeapStringA(Oscl_DefAlloc *alloc, OsclRefCounter *ref) 448 { 449 create(alloc, ref); 450 set_rep(NULL); 451 } 452 453 OSCL_EXPORT_REF OSCL_wHeapStringA::OSCL_wHeapStringA() 454 { 455 create(NULL, NULL); 456 set_rep(NULL); 457 } 458 OSCL_EXPORT_REF OSCL_wHeapStringA::OSCL_wHeapStringA(Oscl_DefAlloc *alloc, OsclRefCounter *ref) 459 { 460 create(alloc, ref); 461 set_rep(NULL); 462 } 463 464 // ************************************************************** 465 OSCL_EXPORT_REF OSCL_HeapStringA::OSCL_HeapStringA(const chartype* cp, Oscl_DefAlloc *alloc, OsclRefCounter *ref) 466 { 467 create(alloc, ref); 468 set_rep(cp); 469 } 470 471 OSCL_EXPORT_REF OSCL_wHeapStringA::OSCL_wHeapStringA(const chartype* cp, Oscl_DefAlloc *alloc, OsclRefCounter *ref) 472 { 473 create(alloc, ref); 474 set_rep(cp); 475 } 476 477 // ************************************************************** 478 OSCL_EXPORT_REF void OSCL_HeapStringA::set(const chartype* cp, uint32 length) 479 { 480 set_rep(cp, length); 481 //just in case input string is shorter than 'length' 482 iRep->size = oscl_strlen(get_cstr()); 483 } 484 485 OSCL_EXPORT_REF void OSCL_wHeapStringA::set(const chartype* cp, uint32 length) 486 { 487 set_rep(cp, length); 488 //just in case input string is shorter than 'length' 489 iRep->size = oscl_strlen(get_cstr()); 490 } 491 492 493 // ************************************************************** 494 OSCL_EXPORT_REF OSCL_HeapStringA::OSCL_HeapStringA(const chartype* cp, uint32 length, Oscl_DefAlloc *alloc, OsclRefCounter *ref) 495 { 496 create(alloc, ref); 497 set(cp, length); 498 } 499 500 OSCL_EXPORT_REF OSCL_wHeapStringA::OSCL_wHeapStringA(const chartype* cp, uint32 length, Oscl_DefAlloc *alloc, OsclRefCounter *ref) 501 { 502 create(alloc, ref); 503 set(cp, length); 504 } 505 506 // ************************************************************** 507 OSCL_EXPORT_REF OSCL_HeapStringA::OSCL_HeapStringA(const OSCL_HeapStringA& src) : OSCL_String(src) 508 { 509 create(NULL, NULL); 510 *this = src;//uses heapstring=heapstring overload. 511 } 512 OSCL_EXPORT_REF OSCL_HeapStringA::OSCL_HeapStringA(const OSCL_HeapStringA& src, Oscl_DefAlloc *alloc, OsclRefCounter *ref) 513 { 514 create(alloc, ref); 515 *this = src;//uses heapstring=heapstring overload. 516 } 517 518 OSCL_EXPORT_REF OSCL_wHeapStringA::OSCL_wHeapStringA(const OSCL_wHeapStringA& src) : OSCL_wString(src) 519 { 520 create(NULL, NULL); 521 *this = src;//uses heapstring=heapstring overload. 522 } 523 OSCL_EXPORT_REF OSCL_wHeapStringA::OSCL_wHeapStringA(const OSCL_wHeapStringA& src , Oscl_DefAlloc *alloc, OsclRefCounter *ref) 524 { 525 create(alloc, ref); 526 *this = src;//uses heapstring=heapstring overload. 527 } 528 529 // ************************************************************** 530 OSCL_EXPORT_REF OSCL_HeapStringA::OSCL_HeapStringA(const OSCL_String& src, Oscl_DefAlloc *alloc, OsclRefCounter *ref) 531 { 532 create(alloc, ref); 533 set_rep(src); 534 } 535 536 OSCL_EXPORT_REF OSCL_wHeapStringA::OSCL_wHeapStringA(const OSCL_wString& src , Oscl_DefAlloc *alloc, OsclRefCounter *ref) 537 { 538 create(alloc, ref); 539 set_rep(src); 540 } 541 542 // ************************************************************** 543 OSCL_EXPORT_REF OSCL_HeapStringA::~OSCL_HeapStringA() 544 { 545 //remove ref to string rep 546 if (iRep) 547 iRep->remove_ref(*iAlloc); 548 //remove ref to allocator. 549 if (iAllocRef) 550 iAllocRef->removeRef(); 551 } 552 553 OSCL_EXPORT_REF OSCL_wHeapStringA::~OSCL_wHeapStringA() 554 { 555 //remove ref to string rep 556 if (iRep) 557 iRep->remove_ref(*iAlloc); 558 //remove ref to allocator. 559 if (iAllocRef) 560 iAllocRef->removeRef(); 561 } 562 563 // ************************************************************** 564 OSCL_EXPORT_REF OSCL_HeapStringA& OSCL_HeapStringA::operator=(const OSCL_HeapStringA & src) 565 { 566 //the allocators must match in order to re-use 567 //the heap string representation. 568 //Allocators match if they're the same object, 569 //or they're both the default allocators. 570 if (src.iRep 571 && ((src.iAlloc == (Oscl_DefAlloc*)&src.iDefAlloc 572 && iAlloc == &iDefAlloc) 573 || src.iAlloc == iAlloc)) 574 { 575 CHeapRep::assign(iRep, src.iRep, *iAlloc); 576 } 577 else 578 {//otherwise, create a new representation 579 set_rep(src); 580 } 581 return (*this); 582 } 583 584 OSCL_EXPORT_REF OSCL_wHeapStringA& OSCL_wHeapStringA::operator=(const OSCL_wHeapStringA & src) 585 { 586 //the allocators must match in order to re-use 587 //the heap string representation. 588 //Allocators match if they're the same object, 589 //or they're both the default allocators. 590 if (src.iRep 591 && ((src.iAlloc == (Oscl_DefAlloc*)&src.iDefAlloc 592 && iAlloc == &iDefAlloc) 593 || src.iAlloc == iAlloc)) 594 { 595 CHeapRep::assign(iRep, src.iRep, *iAlloc); 596 } 597 else 598 {//otherwise, create a new representation 599 set_rep(src); 600 } 601 return (*this); 602 } 603 604 // ************************************************************** 605 OSCL_EXPORT_REF OSCL_HeapStringA& OSCL_HeapStringA::operator=(const chartype * cp) 606 { 607 set_rep(cp); 608 return (*this); 609 } 610 611 OSCL_EXPORT_REF OSCL_wHeapStringA& OSCL_wHeapStringA::operator=(const chartype * cp) 612 { 613 set_rep(cp); 614 return (*this); 615 } 616 617 // ************************************************************** 618 // CStackRep Implementation 619 // ************************************************************** 620 621 OSCL_EXPORT_REF void CStackRep::set(const char* cp, uint32 len) 622 { 623 if (len > maxsize) 624 size = maxsize;//truncate 625 else 626 size = len; 627 if (cp) 628 oscl_strncpy((char*)buffer, cp, size); 629 ((char*)buffer)[size] = '\0'; 630 } 631 OSCL_EXPORT_REF void CStackRep::set(const oscl_wchar* cp, uint32 len) 632 { 633 if (len > maxsize) 634 size = maxsize;//truncate 635 else 636 size = len; 637 if (cp) 638 oscl_strncpy((oscl_wchar*)buffer, cp, size); 639 ((oscl_wchar*)buffer)[size] = '\0'; 640 } 641 642 // ************************************************************** 643 OSCL_EXPORT_REF void CStackRep::append(const char* cp, uint32 alen) 644 { 645 uint32 len = alen; 646 if (size + len > maxsize) 647 len = maxsize - size;//truncate 648 if (len > 0) 649 { 650 size = size + len; 651 oscl_strncat((char*)buffer, cp, len); 652 ((char*)buffer)[size] = '\0'; 653 } 654 } 655 OSCL_EXPORT_REF void CStackRep::append(const oscl_wchar* cp, uint32 alen) 656 { 657 uint32 len = alen; 658 if (size + len > maxsize) 659 len = maxsize - size;//truncate 660 if (len > 0) 661 { 662 size = size + len; 663 oscl_strncat((oscl_wchar*)buffer, cp, len); 664 ((oscl_wchar*)buffer)[size] = '\0'; 665 } 666 } 667 668 // ************************************************************** 669 // CFastRep Implementation 670 // ************************************************************** 671 672 OSCL_EXPORT_REF void CFastRep::set_r(const char* cp, uint32 len) 673 { 674 size = len; 675 maxsize = len; 676 buffer = (OsclAny*)cp; 677 writable = false; 678 } 679 OSCL_EXPORT_REF void CFastRep::set_r(const oscl_wchar* cp, uint32 len) 680 { 681 size = len; 682 maxsize = len; 683 buffer = (OsclAny*)cp; 684 writable = false; 685 } 686 687 // ************************************************************** 688 OSCL_EXPORT_REF void CFastRep::set_w(char* cp, uint32 len, uint32 maxlen) 689 { 690 size = len; 691 maxsize = maxlen; 692 buffer = (OsclAny*)cp; 693 writable = true; 694 } 695 OSCL_EXPORT_REF void CFastRep::set_w(oscl_wchar* cp, uint32 len, uint32 maxlen) 696 { 697 size = len; 698 maxsize = maxlen; 699 buffer = (OsclAny*)cp; 700 writable = true; 701 } 702 703 // ************************************************************** 704 OSCL_EXPORT_REF void CFastRep::append(const char* cp, uint32 len) 705 { 706 uint32 ncopy = len; 707 if (size + len > maxsize) 708 ncopy = maxsize - size;//truncate 709 if (ncopy > 0) 710 { 711 oscl_strncat((char*)buffer, cp, ncopy); 712 size += ncopy; 713 if (!writable) 714 maxsize = size; 715 } 716 } 717 OSCL_EXPORT_REF void CFastRep::append(const oscl_wchar* cp, uint32 len) 718 { 719 uint32 ncopy = len; 720 if (size + len > maxsize) 721 ncopy = maxsize - size;//truncate 722 if (ncopy > 0) 723 { 724 oscl_strncat((oscl_wchar*)buffer, cp, ncopy); 725 size += ncopy; 726 if (!writable) 727 maxsize = size; 728 } 729 } 730 731 // ************************************************************** 732 // OSCL_FastString Implementation 733 // OSCL_wFastString Implementation 734 // ************************************************************** 735 void OSCL_FastString::set_rep(const chartype* cp) 736 { 737 uint32 len = (cp) ? oscl_strlen(cp) : 0; 738 rep.set_r(cp, len); 739 } 740 741 void OSCL_wFastString::set_rep(const chartype* cp) 742 { 743 uint32 len = (cp) ? oscl_strlen(cp) : 0; 744 rep.set_r(cp, len); 745 } 746 747 // ************************************************************** 748 void OSCL_FastString::append_rep(const chartype* cp) 749 { 750 uint32 len = (cp) ? oscl_strlen(cp) : 0; 751 rep.append(cp, len); 752 } 753 754 void OSCL_wFastString::append_rep(const chartype* cp) 755 { 756 uint32 len = (cp) ? oscl_strlen(cp) : 0; 757 rep.append(cp, len); 758 } 759 760 761 // ************************************************************** 762 void OSCL_FastString::set_rep(const OSCL_String& src) 763 { 764 //inherit the writable-ness of the source string. 765 if (src.get_str()) 766 rep.set_w(src.get_str(), src.get_size(), src.get_maxsize()); 767 else 768 rep.set_r(src.get_cstr(), src.get_size()); 769 } 770 771 void OSCL_wFastString::set_rep(const OSCL_wString& src) 772 { 773 //inherit the writable-ness of the source string. 774 if (src.get_str()) 775 rep.set_w(src.get_str(), src.get_size(), src.get_maxsize()); 776 else 777 rep.set_r(src.get_cstr(), src.get_size()); 778 } 779 780 // ************************************************************** 781 void OSCL_FastString::append_rep(const OSCL_String& src) 782 { 783 rep.append(src.get_cstr(), src.get_size()); 784 } 785 786 void OSCL_wFastString::append_rep(const OSCL_wString& src) 787 { 788 rep.append(src.get_cstr(), src.get_size()); 789 } 790 791 // ************************************************************** 792 void OSCL_FastString::set_len(uint32 len) 793 { 794 rep.size = len; 795 } 796 void OSCL_wFastString::set_len(uint32 len) 797 { 798 rep.size = len; 799 } 800 801 // ************************************************************** 802 OSCL_EXPORT_REF OSCL_FastString::OSCL_FastString() 803 { 804 set_rep(NULL); 805 } 806 807 OSCL_EXPORT_REF OSCL_wFastString::OSCL_wFastString() 808 { 809 set_rep(NULL); 810 } 811 812 // ************************************************************** 813 OSCL_EXPORT_REF OSCL_FastString::OSCL_FastString(const chartype* cp) 814 { 815 set_rep(cp); 816 } 817 818 OSCL_EXPORT_REF OSCL_wFastString::OSCL_wFastString(const chartype* cp) 819 { 820 set_rep(cp); 821 } 822 823 // ************************************************************** 824 OSCL_EXPORT_REF void OSCL_FastString::set(chartype* cp, uint32 maxlen) 825 { 826 //set string to new writable buffer. 827 //make sure buffer is null-terminated 828 for (uint32 i = 0; i <= maxlen; i++) 829 { 830 if (cp[i] == '\0') 831 { 832 rep.set_w(cp, i, maxlen); 833 return; 834 } 835 } 836 OsclError::Leave(OsclErrGeneral);//not null-terminated 837 } 838 839 OSCL_EXPORT_REF void OSCL_wFastString::set(chartype* cp, uint32 maxlen) 840 { 841 //set string to new writable buffer. 842 //make sure buffer is null-terminated 843 for (uint32 i = 0; i <= maxlen; i++) 844 { 845 if (cp[i] == '\0') 846 { 847 rep.set_w(cp, i, maxlen); 848 return; 849 } 850 } 851 OsclError::Leave(OsclErrGeneral);//not null-terminated 852 } 853 854 // ************************************************************** 855 OSCL_EXPORT_REF OSCL_FastString::OSCL_FastString(chartype* cp, uint32 maxlen) 856 { 857 set(cp, maxlen); 858 } 859 860 OSCL_EXPORT_REF OSCL_wFastString::OSCL_wFastString(chartype* cp, uint32 maxlen) 861 { 862 set(cp, maxlen); 863 } 864 865 // ************************************************************** 866 OSCL_EXPORT_REF OSCL_FastString::OSCL_FastString(const OSCL_FastString& src) : OSCL_String(src) 867 { 868 set_rep(src); 869 } 870 871 OSCL_EXPORT_REF OSCL_wFastString::OSCL_wFastString(const OSCL_wFastString& src) : OSCL_wString(src) 872 { 873 set_rep(src); 874 } 875 876 // ************************************************************** 877 OSCL_EXPORT_REF OSCL_FastString::~OSCL_FastString() 878 { 879 } 880 881 OSCL_EXPORT_REF OSCL_wFastString::~OSCL_wFastString() 882 { 883 } 884 885 // ************************************************************** 886 OSCL_EXPORT_REF void OSCL_FastString::set_length() 887 { 888 rep.size = oscl_strlen(get_cstr()); 889 //for read-only string, the maxsize tracks the size. 890 if (!rep.writable) 891 rep.maxsize = rep.size; 892 } 893 894 OSCL_EXPORT_REF void OSCL_wFastString::set_length() 895 { 896 rep.size = oscl_strlen(get_cstr()); 897 //for read-only string, the maxsize tracks the size. 898 if (!rep.writable) 899 rep.maxsize = rep.size; 900 } 901 902 // ************************************************************** 903 OSCL_EXPORT_REF OSCL_FastString& OSCL_FastString::operator=(const OSCL_FastString & src) 904 { 905 set_rep(src); 906 return (*this); 907 } 908 909 OSCL_EXPORT_REF OSCL_wFastString& OSCL_wFastString::operator=(const OSCL_wFastString & src) 910 { 911 set_rep(src); 912 return (*this); 913 } 914 915 916 // ************************************************************** 917 OSCL_EXPORT_REF OSCL_FastString& OSCL_FastString::operator=(const chartype * cp) 918 { 919 set_rep(cp); 920 return (*this); 921 } 922 923 OSCL_EXPORT_REF OSCL_wFastString& OSCL_wFastString::operator=(const chartype * cp) 924 { 925 set_rep(cp); 926 return (*this); 927 } 928 929 930 // ************************************************************** 931 OSCL_EXPORT_REF uint32 OSCL_FastString::get_size() const 932 { 933 return rep.size; 934 } 935 936 OSCL_EXPORT_REF uint32 OSCL_wFastString::get_size() const 937 { 938 return rep.size; 939 } 940 941 // ************************************************************** 942 OSCL_EXPORT_REF uint32 OSCL_FastString::get_maxsize() const 943 { 944 return rep.maxsize; 945 } 946 947 OSCL_EXPORT_REF uint32 OSCL_wFastString::get_maxsize() const 948 { 949 return rep.maxsize; 950 } 951 952 // ************************************************************** 953 OSCL_EXPORT_REF const OSCL_FastString::chartype* OSCL_FastString::get_cstr() const 954 { 955 return (chartype*)rep.buffer; 956 } 957 958 OSCL_EXPORT_REF const OSCL_wFastString::chartype* OSCL_wFastString::get_cstr() const 959 { 960 return (chartype*)rep.buffer; 961 } 962 963 // ************************************************************** 964 OSCL_EXPORT_REF OSCL_FastString::chartype* OSCL_FastString::get_str() const 965 { 966 if (rep.writable) 967 return (chartype*)rep.buffer; 968 return NULL; 969 } 970 971 OSCL_EXPORT_REF OSCL_wFastString::chartype* OSCL_wFastString::get_str() const 972 { 973 if (rep.writable) 974 return (chartype*)rep.buffer; 975 return NULL; 976 } 977 978 979 980 981 982