// dstring.cpp (203 lines) // // Coded by: Joseph Bertolami // Email: jbertola@uci.edu // // ICS 175A, Winter 2004 // // This file defines the basic lowest-level string object used for // the entire project. DString allows the other programmers to // abstract from the character pointer level and provides high // level functionality needed for this project. #include "dstring.h" // Author: Joseph Bertolami // Email: jbertola@uci.edu DString::DString(const DString & rhs) { buffer = strdup(rhs.buffer); length = rhs.length; } // Author: Joseph Bertolami // Email: jbertola@uci.edu DString::DString(const char *str) { buffer= strdup(str); length=(unsigned)strlen(str); } // Author: Joseph Bertolami // Email: jbertola@uci.edu DString::~DString() { if ( buffer ) free(buffer); buffer=NULL; length=0; } // operator = // assigns rhs to *this /* char * DString::operator = (DString & rhs) { if ( buffer ) free( buffer ); buffer = strdup(rhs.buffer); length = rhs.length; return buffer; } */ // Author: Joseph Bertolami // Email: jbertola@uci.edu DString DString::operator = (const DString & rhs) { if ( buffer ) free( buffer ); buffer = strdup(rhs.buffer); length = rhs.length; return *this; } // Author: Joseph Bertolami // Email: jbertola@uci.edu void DString::set(const char *text) { if ( buffer ) free(buffer); buffer = strdup(text); length = strlen(text); } // Author: Joseph Bertolami // Email: jbertola@uci.edu DString DString::operator + (DString & rhs) { // operator + // cat's rhs onto *this char * temp = (char*) malloc(strlen(buffer) + strlen(rhs.buffer) + 1); memcpy(temp, buffer, strlen(buffer)); memcpy(temp+strlen(buffer), rhs.buffer, strlen(rhs.buffer)); temp[strlen(buffer)+strlen(rhs.buffer)] = '\0'; DString tmp(temp); return tmp; } // Author: Joseph Bertolami // Email: jbertola@uci.edu // operator += DString DString::operator += (DString & rhs) { *this = *this + rhs; return *this; } // Author: Joseph Bertolami // Email: jbertola@uci.edu DString DString::operator += (char * rhs) { ::strcat(buffer, rhs); length += strlen(rhs); return *this; } // Author: Joseph Bertolami // Email: jbertola@uci.edu // // char *substr // returns a pointer to the first occurrence of // string 'token' in string 'str' char * DString::substr(const char *token, const char *str) { return strstr(str, token); } // Author: Joseph Bertolami // Email: jbertola@uci.edu // Edited by: Jeff Weiss, Nicholas Urrea DString DString::substr(int start, int end) { DString newstr; if ( end < start ) { printf("DString::substr (error) end < start\n"); return newstr; } char *temp = new char[(end-start)+1]; // define valid new length memcpy(temp, buffer+start, end-start); temp[(end-start)]='\0'; // add end char newstr.set(temp); delete [] temp; newstr.length=(end-start); // re-define length return newstr; } // Author: Joseph Bertolami // Email: jbertola@uci.edu bool DString::operator ==(DString & rhs) { if ( 0 == ::strcmp(buffer, rhs.buffer) ) return true; return false; } // Author: Joseph Bertolami // Email: jbertola@uci.edu char DString::operator [](unsigned int index) { if ( index < 0 || index >= length ) return 0; return buffer[index]; } // Author: Joseph Bertolami // Email: jbertola@uci.edu unsigned DString::nsubstr(const char *token, const char *str) { // unsigned nsubstr // returns number of occurrences of substring token // in string 'str' unsigned count = 0; char *marker = NULL; char *temp = strdup(str); while ( (marker = substr(token,temp)) != NULL ) { temp = marker + strlen(token) + 1; count++; } return count; } // Author: Joseph Bertolami // Email: jbertola@uci.edu int DString::strcmp(DString & string2) { // returns 0 if no match // note, if comparisons begin coming up wrong, // slow this function down by enforcing ascii // comparison to avoid erroneous data if ( length != string2.length ) return 0; for ( unsigned i = 0; i < length; i++ ) { if ( buffer[i] ^ string2[i] ) return 0; } return 1; } // Author: Joseph Bertolami // Email: jbertola@uci.edu char * DString::data() { return buffer; } void DString::clear() { if ( buffer ) free( buffer ); } unsigned DString::size() { return length; } void DString::print() { printf("%s\n", buffer); } // Edited by: Vishakh void DString::setChar(char c, int index) { buffer[index] = c; }