Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members

String.C

Go to the documentation of this file.
00001 
00026 #include <strstream>
00027 #include <iostream>
00028 
00029 using namespace std;
00030 
00032 #include <string.h>
00033 
00034 #include "String.h"
00035 
00036 
00041 void String::init( const char *initCstr )
00042 {
00043   if (initCstr != 0) {
00044     char ch;
00045     for (ch = *initCstr; ch != '\0'; ch = *initCstr) {
00046       value->data.append( ch );
00047       initCstr++;
00048     }
00049     value->data.append( ch );
00050   }
00051 }
00052 
00053 
00054 
00067 void String::appendCString( const char *CStr )
00068 {
00069   if (CStr != 0) {
00070     size_t len = length();
00071     if (len > 0) {
00072       // if there is a null at the end of the string
00073       // (which there should be) write over it.
00074       if (value->data[len-1] == '\0') {
00075         value->data[len-1] = *CStr++;
00076       }
00077     }
00078     while (*CStr) {
00079       value->data.append( *CStr++ );
00080     }
00081     value->data.append('\0');
00082   }
00083 } // appendCString
00084 
00085 
00097 void String::appendString( String &s )
00098 {
00099   size_t i = 0;
00100   size_t len = s.length();
00101   size_t thisLen = length(); // length of "this" String
00102   
00103   if (thisLen > 0 && len > 0) {
00104     // write over the null character
00105     if (value->data[thisLen-1] == '\0') {
00106       value->data[thisLen-1] = s.value->data[i];
00107       i++;
00108     }
00109   } // if
00110   while (i < len) {
00111     char ch = s.value->data[i];
00112     value->data.append( ch );
00113     i++;
00114   } // while
00115 } // appendString
00116 
00117 
00118 
00122 int String::compareTo( String &s )
00123 {
00124   if (length() == 0 && s.length() == 0) {
00125     // Both String objects contain no data and so are
00126     // equal
00127     return 0;
00128   }
00129 
00130   if (value == s.value) {
00131       // The two String objects point to the same shared data
00132       return 0;
00133   }
00134 
00135   const char *pThisCStr = *this;
00136   const char *Cstr = s;
00137 
00138   return Cstrcmp( pThisCStr, Cstr );
00139 } // compareTo
00140 
00141 
00158 String &String::resize( const size_t newSize )
00159 {
00160   if (newSize == 0) {
00161     newEmptyString();
00162   }
00163   else { // newSize > 0
00164     size_t new_len = newSize + 1;
00165     size_t old_len = length();
00166 
00167     if (value->isShared()) {
00168       if (new_len >= old_len) {
00169         // this will copy the data into a new shared data object
00170         newSharedData();
00171         value->data.set_size( new_len );
00172       }
00173       else { // new_len < old_len
00174         RCPtr<SharedData> new_value = new SharedData();
00175         for (size_t i = 0; i < newSize; i++) {
00176           new_value->data.append( value->data[i] );
00177         }
00178         new_value->data.append('\0');
00179         // note: smart pointer assignment
00180         value = new_value;
00181       }
00182     }
00183     else {
00184       value->data.set_size( new_len );
00185     }
00186 
00187     for (size_t i = old_len-1; i < new_len-1; i++) {
00188       value->data[i] = ' ';
00189     }
00190     value->data[ new_len-1 ] = '\0';
00191   }
00192   return *this;
00193 } // resize
00194 
00195 
00196 
00204 int String::Cstrcmp(const char *pThisCStr, const char *Cstr )
00205 {
00206   int rslt;
00207 
00208   if (pThisCStr == 0) {
00209     if (Cstr == 0)
00210       rslt = 0; // both this and CStr are null
00211     else {
00212       if (::strlen( Cstr ) == 0)
00213         rslt = 0;  // compare to an empty string ("")
00214       else
00215         rslt = -1; // this < Cstr
00216     }
00217   }
00218   else { // pThisCStr != 0
00219     if (Cstr == 0)
00220       rslt = 1; // this > Cstr
00221     else // pThis != 0 && Cstr != 0
00222       rslt = strcmp(pThisCStr, Cstr );
00223   }
00224 
00225   return rslt;
00226 }  // Cstrcmp
00227 
00228 
00229 
00256 String::operator const char *() const
00257 {
00258   const char *pCstr = "";
00259   size_t len = value->data.length();
00260 
00261   if (len > 0) {
00262     if (value->data[len-1] != '\0') {
00263       // then add a NULL terminator
00264       value->data.append('\0');
00265     }
00266     pCstr = value->data.getData();
00267   }
00268 
00269   return pCstr;
00270 } // operator const char *
00271 
00272 
00278 void String::newEmptyString()
00279 {
00280   if (value->isShared()) {
00281     value = new SharedData();
00282   }
00283   else {
00284     value->data.set_size(0);
00285   }
00286 } // newEmptyString
00287 
00288 
00289 
00293 String &String::operator =(const char ch)
00294 {
00295   newEmptyString();
00296   if (ch != '\0') {
00297     value->data.append( ch );
00298     value->data.append('\0');
00299   }
00300   return *this;
00301 } // operator= (String = char)
00302 
00303 
00304 
00323 String &String::operator =(const char *Cstr)
00324 {
00325   newEmptyString();
00326   if (Cstr != 0) {
00327     char ch;
00328     for (ch = *Cstr; ch != '\0'; ch = *Cstr) {
00329       value->data.append(ch);
00330       Cstr++;
00331     }
00332     value->data.append(ch);
00333   }
00334   return *this;
00335 } // operator= (String = C-string)
00336 
00337 
00338 
00352 String &String::operator +=( const char *Cstr )
00353 {
00354   if (value->isShared()) {
00355     newSharedData();
00356   }
00357 
00358   // If there is a null character, delete it so we don't
00359   // append after the null.
00360   size_t len = value->data.length();
00361   if (len > 0 && value->data[len-1] == '\0')
00362     value->data.remove();
00363 
00364   appendCString( Cstr );
00365 
00366   return *this;
00367 } // operator +=
00368 
00369 
00370 
00375 String &String::operator +=(String &s )
00376 {
00377   if (s.value->data.length() > 0) {
00378     if (value->isShared()) {
00379        newSharedData();
00380     }
00381 
00382     // If there is a null character, delete it so we don't
00383     // append after the null.
00384     size_t len = value->data.length();
00385     if (len > 0 && value->data[len-1] == '\0') {
00386       value->data.remove();
00387     }
00388 
00389     appendString( s );
00390   }
00391   return *this;
00392 } // operator +=
00393 
00394 
00395 /*
00396  * ======== Global binary operators ========
00397  *
00398  */
00399 
00409 String operator +(const char *Cstr, String &s)
00410 {
00411   String tmp;
00412   if (Cstr != 0) {
00413     tmp = Cstr;
00414   }
00415   
00416   tmp += s;
00417   return tmp;
00418 } // global operator +
00419 
00420 
00434 String operator +(const char ch, String &s)
00435 {
00436   String tmp;
00437 
00438   tmp = ch;
00439   tmp += s;
00440 
00441   return tmp;
00442 } // global operator +
00443 
00444 
00449 bool operator ==(const char *Cstr, String &s)
00450 {
00451   return (s == Cstr);
00452 } // global operator ==
00453 
00454 
00459 bool operator !=(const char *Cstr, String &s)
00460 {
00461   return (s != Cstr);
00462 } // operator !=
00463 
00464 
00471 bool operator <=(const char *Cstr, String &s)
00472 {
00473   return (s >= Cstr);
00474 } // global operator <=
00475 
00476 
00483 bool operator >=(const char *Cstr, String &s)
00484 {
00485   return (s <= Cstr);
00486 } // global operator >=
00487 
00488 
00495 bool operator <(const char *Cstr, String &s)
00496 {
00497   return (s > Cstr);
00498 } // global operator <
00499 
00500 
00507 bool operator >(const char *Cstr, String &s)
00508 {
00509   return (s < Cstr);
00510 } // global operator >
00511 
00512 

Generated on Mon Sep 22 20:22:58 2003 by doxygen 1.3.3