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 Win32 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 CompoundString& a) const {
00083   CompoundString d;
00084   unsigned int len1 = length();
00085   unsigned int len2 = a.length();
00086   unsigned int len = len1 + len2 + 1;
00087 
00088   char *pTemp = new char[len];
00089 
00090   strcpy(pTemp, pBuffer);
00091   strcat(pTemp, a.pBuffer);
00092 
00093   d = pTemp;
00094 
00095   delete [] pTemp;
00096   return d;
00097 }
00098 
00099 CompoundString&
00100 CompoundString::operator+=(const CompoundString& a) {
00101   unsigned int len1 = length();
00102   unsigned int len2 = a.length();
00103   unsigned int len = len1 + len2 + 1;
00104 
00105   char *pTemp = new char[len];
00106 
00107   strcpy(pTemp, pBuffer);
00108   strcat(pTemp, a.pBuffer);
00109 
00110   delete [] pBuffer;
00111 
00112   pBuffer = pTemp;
00113   return *this;
00114 }
00115 
00116 CompoundString&
00117 CompoundString::operator-=(const CompoundString& a) {
00118   char *p;
00119 
00120   if (a.length() > length()) return *this;
00121 
00122   if ((p = strstr(pBuffer, a.pBuffer)) != 0) {
00123     unsigned int len = length() - a.length() + 1;
00124     unsigned int frontLen = p - pBuffer;
00125     unsigned int backLen = length() - frontLen - a.length();
00126 
00127     char *pTemp = new char[len];
00128     strncpy(pTemp, pBuffer, frontLen);
00129     strncat(pTemp, p + a.length(), backLen);
00130     pTemp[frontLen + backLen] = 0;
00131     delete [] pBuffer;
00132     pBuffer = pTemp;
00133   }
00134   return *this;
00135 }
00136 
00137 int
00138 CompoundString::operator==(const CompoundString &a) const {
00139   return strcmp(pBuffer, a.pBuffer) ? 0 : 1;
00140 }
00141 
00142 int
00143 CompoundString::compare(const char aBuffer[], int n) const {
00144   return !strncmp(pBuffer, aBuffer, n);
00145 }
00146 
00147 int
00148 CompoundString::compare(const char aBuffer[]) const {
00149   return !strcmp(pBuffer, aBuffer);
00150 }
00151 
00152 int
00153 CompoundString::search(const char aBuffer[]) const {
00154   char *p = strstr(pBuffer, aBuffer);
00155   return p == 0 ? -1: p - pBuffer;
00156 }
00157 
00158 unsigned int
00159 CompoundString::length() const {
00160   return strlen(pBuffer);
00161 }
00162