home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / cstring.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.3 KB  |  109 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. #pragma once
  20.  
  21. #include "fredmem.h"
  22.  
  23. #define cstringMaxInternalLen 30
  24.  
  25. class cstring 
  26. {
  27. public:
  28.     inline            cstring ()
  29.                     { haveExternalSpace = false; space.internal[0] = 0; }
  30.     inline            cstring (const char *s)
  31.                     { haveExternalSpace = false; assign (s); }
  32.     inline            cstring (const unsigned char *ps)
  33.                     { haveExternalSpace = false; assign (ps+1, ps[0]); }
  34.     inline            cstring (const cstring& s)
  35.                     { haveExternalSpace = false; assign (s.data(), s.length() ); }
  36.     inline            cstring( void* ptr, long len )
  37.                     { haveExternalSpace = false; assign( ptr, len ); }
  38.     inline             ~cstring () { reserve (0, false); }
  39.     char&            operator[] (int i) { return data()[i]; }
  40.     inline void        operator= (const char *s);
  41.     inline void        operator= (const unsigned char *ps);
  42.     inline void        operator= (const cstring& s);
  43.     void            operator+= (const char *s);
  44.     void            truncAt (char c);
  45.     // comparison
  46.     inline int        operator== (const cstring& s);
  47.     inline int        operator== (const char *s);
  48.     inline int        operator!= (const cstring& s);
  49.     inline int        operator!= (const char *s);
  50.     // query
  51.     inline const char* data() const { return haveExternalSpace? space.external: space.internal; }
  52.     inline operator const char* () const { return data(); }
  53. /*    This is not a bug because
  54.  inline operator char* () { return data(); }
  55. cannot convert a "const cstring" to "char *"
  56. This can be avoided by adding:
  57.  inline operator const char* () const { return data(); }
  58. */
  59.     inline char*    data() { return haveExternalSpace? space.external: space.internal; }
  60.     inline             operator char* () { return data(); }
  61.     inline int        length() const { return strlen (data()); }
  62.     char*            reserve (int len, Boolean preserve = false);
  63. private:
  64.     void             assign (const char *s) { assign (s, s? strlen (s): 0); }
  65.     void             assign (const void *sd, int len);
  66.     union {
  67.         char*            external;
  68.         char            internal [cstringMaxInternalLen+1];
  69.     }                space;
  70.     int                haveExternalSpace;
  71. };
  72.  
  73. inline void        
  74. cstring::operator= (const char *s) { 
  75.     assign (s); 
  76. }
  77.  
  78. inline void        
  79. cstring::operator= (const unsigned char *ps) { 
  80.     assign (ps+1, ps[0]);
  81. }
  82.  
  83. inline void        
  84. cstring::operator= (const cstring& s) { 
  85.     assign (s.data(), s.length() );
  86. }
  87.  
  88. inline int
  89. cstring::operator== (const cstring& s) {
  90.     return !strcmp (data(), s.data());
  91. }
  92.  
  93. inline int
  94. cstring::operator== (const char *s) {
  95.     return !strcmp (data(), s);
  96. }
  97.  
  98. inline int
  99. cstring::operator!= (const cstring& s) {
  100.     return strcmp (data(), s.data());
  101. }
  102.  
  103. inline int
  104. cstring::operator!= (const char *s) {
  105.     return strcmp (data(), s);
  106. }
  107.  
  108.  
  109.