Back to index

im-sdk  12.3.91
CompoundStringP.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright 1990-2001 Sun Microsystems, Inc. All Rights Reserved.
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a
00005 copy of this software and associated documentation files (the
00006 "Software"), to deal in the Software without restriction, including
00007 without limitation the rights to use, copy, modify, merge, publish,
00008 distribute, sublicense, and/or sell copies of the Software, and to
00009 permit persons to whom the Software is furnished to do so, subject to
00010 the following conditions: The above copyright notice and this
00011 permission notice shall be included in all copies or substantial
00012 portions of the Software.
00013 
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018 IN NO EVENT SHALL THE OPEN GROUP OR SUN MICROSYSTEMS, INC. BE LIABLE
00019 FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021 THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022 ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024 
00025 Except as contained in this notice, the names of The Open Group and/or
00026 Sun Microsystems, Inc. shall not be used in advertising or otherwise to
00027 promote the sale, use or other dealings in this Software without prior
00028 written authorization from The Open Group and/or Sun Microsystems,
00029 Inc., as applicable.
00030 
00031 
00032 X Window System is a trademark of The Open Group
00033 
00034 OSF/1, OSF/Motif and Motif are registered trademarks, and OSF, the OSF
00035 logo, LBX, X Window System, and Xinerama are trademarks of the Open
00036 Group. All other trademarks and registered trademarks mentioned herein
00037 are the property of their respective owners. No right, title or
00038 interest in or to any trademark, service mark, logo or trade name of
00039 Sun Microsystems, Inc. or its licensors is granted.
00040 
00041 */
00042 //
00043 // defines Solaris platform dependent method implemetation
00044 // for CompoundString class
00045 //
00046 #include "CompoundString.hh"
00047 #include <string.h>
00048 #include <stdlib.h>
00049 
00050 CompoundString::CompoundString(const char astring[]) {
00051   if (astring == 0) {
00052     pBuffer = new char[1];
00053     pBuffer[0] = 0;
00054   } else {
00055     pBuffer = new char[strlen(astring) + 1];
00056     strcpy(pBuffer, astring);
00057   }
00058 }
00059 
00060 CompoundString::CompoundString(const wchar_t astring[]) {
00061   if (astring == 0) {
00062     pBuffer = new char[1];
00063     pBuffer[0] = 0;
00064   } else {
00065     int len = wcstombs(0, astring, 0); // 3rd argument is ignored
00066     if (len == -1) return;
00067     pBuffer = new char[len + 1];
00068     wcstombs(pBuffer, astring, len);
00069   }
00070 }
00071 
00072 CompoundString&
00073 CompoundString::operator=(const CompoundString& a) {
00074   char *pTemp = new char[a.length() + 1];
00075   strcpy(pTemp, a.pBuffer);
00076   delete [] pBuffer;
00077   pBuffer = pTemp;
00078   return *this;
00079 }
00080 
00081 CompoundString&
00082 CompoundString::operator=(const char * s) {
00083   delete [] pBuffer;
00084   pBuffer = new char[strlen(s) + 1];
00085   strcpy(pBuffer, s);
00086   return *this;
00087 }
00088 
00089 CompoundString&
00090 CompoundString::operator=(const unsigned char * s) {
00091   delete [] pBuffer;
00092   pBuffer = new char[strlen((const char *)s) + 1];
00093   strcpy(pBuffer, (const char *)s);
00094   return *this;
00095 }
00096 
00097 CompoundString
00098 CompoundString::operator+(const CompoundString& a) const {
00099   CompoundString d;
00100   unsigned int len1 = length();
00101   unsigned int len2 = a.length();
00102   unsigned int len = len1 + len2 + 1;
00103 
00104   char *pTemp = new char[len];
00105 
00106   strcpy(pTemp, pBuffer);
00107   strcat(pTemp, a.pBuffer);
00108 
00109   d = pTemp;
00110 
00111   delete [] pTemp;
00112   return d;
00113 }
00114 
00115 CompoundString&
00116 CompoundString::operator+=(const CompoundString& a) {
00117   unsigned int len1 = length();
00118   unsigned int len2 = a.length();
00119   unsigned int len = len1 + len2 + 1;
00120 
00121   char *pTemp = new char[len];
00122 
00123   strcpy(pTemp, pBuffer);
00124   strcat(pTemp, a.pBuffer);
00125 
00126   delete [] pBuffer;
00127 
00128   pBuffer = pTemp;
00129   return *this;
00130 }
00131 
00132 CompoundString&
00133 CompoundString::operator-=(const CompoundString& a) {
00134   char *p;
00135 
00136   if (a.length() > length()) return *this;
00137 
00138   if ((p = strstr(pBuffer, a.pBuffer)) != 0) {
00139     unsigned int len = length() - a.length() + 1;
00140     unsigned int frontLen = p - pBuffer;
00141     unsigned int backLen = length() - frontLen - a.length();
00142 
00143     char *pTemp = new char[len];
00144     strncpy(pTemp, pBuffer, frontLen);
00145     strncat(pTemp, p + a.length(), backLen);
00146     pTemp[frontLen + backLen] = 0;
00147     delete [] pBuffer;
00148     pBuffer = pTemp;
00149   }
00150   return *this;
00151 }
00152 
00153 int
00154 CompoundString::operator==(const CompoundString &a) const {
00155   return strcmp(pBuffer, a.pBuffer) ? 0 : 1;
00156 }
00157 
00158 int
00159 CompoundString::compare(const char aBuffer[], int n) const {
00160   return !strncmp(pBuffer, aBuffer, n);
00161 }
00162 
00163 int
00164 CompoundString::compare(const char aBuffer[]) const {
00165   return !strcmp(pBuffer, aBuffer);
00166 }
00167 
00168 int
00169 CompoundString::search(const char aBuffer[]) const {
00170   char *p = strstr(pBuffer, aBuffer);
00171   return p == 0 ? -1: p - pBuffer;
00172 }
00173 
00174 unsigned int
00175 CompoundString::length() const {
00176   return strlen(pBuffer);
00177 }
00178