G3string.cpp

Go to the documentation of this file.
00001 #include <string.h>
00002 #include "G3string.h"
00003 
00004 String::String(const char s[])
00005 {
00006    info_ = new char[strlen(s) + 1];      // Leave room for the '\0'. 
00007    strcpy(info_, s);                     // Copy from s to info.
00008 }
00009 
00010 String::String(const String& s)
00011 {
00012    info_ = new char[strlen(s.info_) + 1]; // Allocate memory for the copy.
00013    strcpy(info_, s.info_);                // Copy the argument's characters.
00014 }
00015 
00016 String::~String( )
00017 {
00018    delete [] info_;    // Deallocate the array.
00019 }
00020 
00021 String& String::operator= (const String& rhs) 
00022 {
00023    if (this != &rhs) {                       // Cover the case of s = s.
00024       delete [] info_;                       //   Deallocate the old buffer.
00025       info_ = new char[strlen(rhs.info_) +1];// Allocate memory for a new one.
00026       strcpy(info_, rhs.info_);              // Copy the characters from the
00027    }                                         //  right side to the left
00028    return *this;                             // Return this object.
00029 }     
00030 
00031 String& String::operator+= (const String& s)
00032 {
00033    char* temp = new char[strlen(info_) + strlen(s.info_) + 1]; // Create a new 
00034                            // array to hold the two string arrays.
00035    strcpy(temp,info_);     // Copy the characters from this array into temp. 
00036    strcat(temp,s.info_);   // Then copy the characters of s.info_ into temp. 
00037 
00038    delete [] info_;        // Replace the old value for info_ by temp.
00039    info_ = temp;
00040    return *this;
00041 }
00042 
00043 String operator+ (const String& s, const String& t)
00044 {
00045    String temp(s);    // temp is a copy of s.
00046    temp += t;         // Add t to the end of temp.
00047    return temp;      
00048 }
00049 
00050 char& String::operator[](int i)
00051 {
00052    return info_[i];
00053 } 
00054 
00055 char String::operator[](int i) const
00056 {
00057    return info_[i];
00058 } 
00059 
00060 const char* String::charString() const       
00061 {  
00062    return info_;
00063 }
00064 
00065 OPS_Stream& operator<< (OPS_Stream& out, const String& s)
00066 {
00067    out << s.charString();
00068    return out;
00069 }
00070 
00071 istream& operator>> (istream& in, String& s)   
00072 {
00073    char buffer[1000];             // Buffer to store the stream characters
00074    in.getline(buffer,1000,'\n');  // Remove up to 999 characters from in, 
00075                                   //   up to and including first occurrence                                
00076                                   //   of '\n'.  Store all but '\n' in the  
00077                                   //   buffer; terminate the buffer with '\0'.   
00078    s = String(buffer);            // Create a new String from the buffer and
00079                                   //   assign it to s.
00080    return in;
00081 }
00082 
00083 int String::length() const
00084 {
00085    return strlen(info_); 
00086 }
00087 
00088 bool operator== (const String& s, const String& t)
00089 {
00090    return strcmp(s.info_,t.info_) == 0;
00091 }
00092 
00093 bool operator!= (const String& s, const String& t)
00094 {
00095    return strcmp(s.info_,t.info_) != 0;
00096 }
00097 
00098 bool operator< (const String& s, const String& t)
00099 {
00100    return strcmp(s.info_,t.info_) < 0;
00101 }
00102 
00103 bool operator<= (const String& s, const String& t)
00104 {
00105    return strcmp(s.info_,t.info_) <= 0;
00106 }
00107 
00108 bool operator> (const String& s, const String& t)
00109 {
00110    return strcmp(s.info_,t.info_) > 0;
00111 }
00112 
00113 bool operator>= (const String& s, const String& t)
00114 {
00115    return strcmp(s.info_,t.info_) >= 0;
00116 }
00117 
00118 

Generated on Mon Oct 23 15:05:27 2006 for OpenSees by doxygen 1.5.0