home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / cstring.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  2.1 KB  |  84 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "cstring.h"
  20.  
  21. #include "xpassert.h"
  22.  
  23. void
  24. cstring::operator+= (const char *s)
  25. {
  26.     if (s != NULL && s[0] != '\0')
  27.     {
  28.         int newLength = length() + strlen(s);
  29.         reserve (newLength+1, true);
  30.         strcat (data(), s);
  31.     }
  32. }
  33.  
  34. void
  35. cstring::truncAt (char c)
  36. {
  37.     char *cc = strchr (data(), c);
  38.     if (cc) {
  39.         *cc = 0;
  40.         reserve (length()+1, true);
  41.     }
  42. }
  43.  
  44. char*
  45. cstring::reserve (int len, Boolean preserve)
  46. {
  47.     char *dest = space.internal;
  48.     if (haveExternalSpace)
  49.         if (len > cstringMaxInternalLen) // can reuse space
  50.             dest = space.external = (char*) realloc (space.external, len);
  51.         else { // don't need external space
  52.             char *oldExternal = space.external;
  53.             if (preserve) { // can't guarantee preservation since it's shrinking
  54.                 memcpy (oldExternal, space.internal, cstringMaxInternalLen);
  55.                 dest[cstringMaxInternalLen] = 0;
  56.             }
  57.             free (oldExternal);
  58.             haveExternalSpace = false;
  59.         }
  60.     else
  61.         if (len > cstringMaxInternalLen) { // need new space
  62.             dest= (char*) malloc (len);
  63.             if (preserve)
  64.                 memcpy (dest, space.internal, cstringMaxInternalLen);
  65.             space.external = dest;
  66.             haveExternalSpace = true;
  67.         }
  68.     return dest; 
  69. }
  70.  
  71. void
  72. cstring::assign (const void *sd, int len)
  73. {
  74.     XP_ASSERT(sd != NULL || len == 0);
  75.     char *dest = reserve (len+1, false);
  76.  
  77.     if (sd != NULL)
  78.         memcpy (dest, sd, len);
  79.         
  80.     dest[len] = 0;
  81. }
  82.  
  83.  
  84.