home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / Sessions / Traut / ZStrings / Source / CrossPlatform / ZStringData.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  1.9 KB  |  77 lines

  1. /*==================================================================
  2.     File:        ZStringData.cpp
  3.     
  4.     Contains:    Data backing object for ZStrings.
  5.  
  6.     Written by:    Eric Traut
  7.     
  8.     Copyright:    2000-2001 Connectix Corporation
  9.     
  10.     This source has been placed into the public domain by
  11.     Connectix Corporation. You have the right to modify, 
  12.     distribute or use this code without any legal limitations
  13.     or finanicial/licensing requirements. Connectix is not 
  14.     liable for any problems that result from the use of this 
  15.     code.
  16.     
  17.     If you have comments, feedback, questions, or would like
  18.     to submit bug fixes or updates to this code, please email
  19.     opensource@connectix.com.
  20. ==================================================================*/
  21.  
  22. #include "ZStringData.h"
  23.  
  24. #include <string.h>
  25. #include <stddef.h>
  26. #include <new>
  27.  
  28.  
  29. /*------------------------------------------------------------------
  30.     Allocate                                        [static]
  31. ------------------------------------------------------------------*/
  32.  
  33. ZStringData *
  34. ZStringData::Allocate(
  35.     Z_UInt32        inStringLength)
  36. {
  37.     Z_UInt32            sizeNeeded = offsetof(ZStringData, mStringData) + inStringLength + 1;
  38.     ZStringData *        newData = reinterpret_cast<ZStringData *>(new (std::nothrow) Z_UInt8[sizeNeeded]);
  39.     
  40.     if (newData != NULL)
  41.     {
  42.         newData->mRefCount = 0;
  43.         newData->mStringLength = inStringLength;
  44.         newData->mStringData[inStringLength] = '\0';
  45.     }
  46.     
  47.     return newData;
  48. }
  49.         
  50.  
  51. /*------------------------------------------------------------------
  52.     Deallocate
  53. ------------------------------------------------------------------*/
  54.  
  55. void
  56. ZStringData::Deallocate()
  57. {
  58.     delete [] (Z_UInt8 *)this;
  59. }
  60.  
  61.  
  62. /*------------------------------------------------------------------
  63.     FillInString
  64. ------------------------------------------------------------------*/
  65.  
  66. void
  67. ZStringData::FillInString(
  68.     const char *        inString,
  69.     Z_UInt16            inOffset,
  70.     Z_UInt16            inLength)
  71. {
  72.     check(inOffset + inLength <= mStringLength);
  73.     memcpy(mStringData + inOffset, inString, inLength);
  74. }
  75.  
  76.  
  77.