Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vuString.cpp

Go to the documentation of this file.
00001 #include "vuString.h"
00002 
00003 #include <iostream.h>
00004 #include <string.h>
00005 #include <stdio.h>
00006 
00007 vuString::vuString()
00008 {
00009         m_Data = new char[1];
00010         m_Data[0] = '\0';
00011 }
00012 
00013 vuString::vuString(char c)
00014 {
00015     m_Data = new char[2];
00016     m_Data[0] = c;
00017     m_Data[1] = '\0';
00018 }
00019 
00020 vuString::vuString(const char* s)
00021 {
00022     if (s != 0)
00023     {
00024             m_Data = new char[strlen(s) + 1];
00025             strcpy(m_Data, s);
00026     }
00027     else
00028         {
00029         m_Data = new char[1];
00030                 m_Data[0] = '\0';
00031         }
00032 }
00033 
00034 vuString::vuString(int i)
00035 {
00036         char temp[12];
00037 
00038         sprintf(temp,"%i",i);
00039     m_Data = new char[strlen(temp) + 1];
00040     strcpy(m_Data, temp);
00041 }
00042 
00043 vuString::vuString(long l)
00044 {
00045         char temp[12];
00046 
00047         sprintf(temp,"%li",l);
00048     m_Data = new char[strlen(temp) + 1];
00049     strcpy(m_Data, temp);
00050 }
00051 
00052 vuString::vuString(unsigned long ul)
00053 {
00054         char temp[12];
00055 
00056         sprintf(temp,"%lu",ul);
00057     m_Data = new char[strlen(temp) + 1];
00058     strcpy(m_Data, temp);
00059 }
00060 
00061 vuString::vuString(const vuString& s)
00062 {
00063     m_Data = new char[strlen(s.m_Data) + 1];
00064     strcpy(m_Data, s.m_Data);
00065 };
00066 
00067 vuString::~vuString()
00068 {
00069     delete [] m_Data;
00070 }
00071 
00072 dword vuString::getLength(void) const
00073 {
00074         return strlen(m_Data);
00075 }
00076 
00077 bool vuString::isEmpty() const
00078 {
00079     return (strlen(m_Data)==0);
00080 }
00081 
00082 vuString& vuString::empty(void)
00083 {
00084     (*this) = "";
00085 
00086     return *this;
00087 }
00088 
00089 int vuString::compare(const vuString& s) const
00090 {
00091         return strcmp(m_Data, s.m_Data);
00092 }
00093 
00094 vuString vuString::substr(dword start,dword end) const
00095 {
00096     if (isEmpty())
00097         return "";
00098     else if (start >= getLength())
00099         return "";
00100     else if (start > end)
00101         return "";
00102     else
00103     {
00104         //fix the end index.
00105         if (end >= getLength()) end = getLength()-1;
00106         //Create a string to hold all the characters.
00107         char *newstr = new char[end-start+2];
00108         //Copy over the string.
00109         memcpy(newstr,m_Data+start,end-start+1);
00110         //Append the end of string character.
00111         newstr[end-start+1]='\0';
00112         return newstr;
00113     }
00114 }
00115 
00116 vuString& vuString::operator=(const vuString& rhs)
00117 {
00118         if (this != &rhs)
00119         {
00120         delete [] m_Data;
00121 
00122             m_Data = new char[strlen(rhs)+1];
00123             strcpy(m_Data, rhs.m_Data);
00124         }
00125 
00126         return *this;
00127 }
00128 
00129 char& vuString::operator[](dword index)
00130 {
00131         return m_Data[index];
00132 }
00133 
00134 char vuString::operator[](dword index) const
00135 {
00136         return m_Data[index];
00137 }
00138 
00139 vuString vuString::operator+(const vuString& rhs) const
00140 {
00141         vuString str = *this;
00142 
00143         str += rhs;
00144         return str;
00145 }
00146 
00147 vuString& vuString::operator+=(const vuString& rhs)
00148 {
00149     int len = strlen(m_Data) + strlen(rhs.m_Data);
00150     char *temp = new char[len + 1];
00151     strcpy(temp,m_Data);
00152     strcpy(temp+strlen(m_Data),rhs.m_Data);
00153     delete [] m_Data;
00154     m_Data = temp;
00155 
00156     return *this;
00157 }
00158 
00159 vuString& vuString::operator<<(const vuString& rhs)
00160 {
00161     *this += rhs;
00162     return *this;
00163 }
00164 
00165 bool vuString::operator==(const vuString& rhs) const
00166 {
00167         return (strcmp(m_Data,rhs.m_Data) == 0);
00168 }
00169 
00170 bool vuString::operator!=(const vuString& rhs) const
00171 {
00172     return (strcmp(m_Data,rhs.m_Data) != 0);
00173 }
00174 
00175 bool vuString::operator<(const vuString& rhs) const
00176 {
00177     return (strcmp(m_Data,rhs.m_Data) < 0);
00178 }
00179 
00180 bool vuString::operator>(const vuString& rhs) const
00181 {
00182     return (strcmp(m_Data,rhs.m_Data) > 0);
00183 }
00184 
00185 bool vuString::operator<=(const vuString& rhs) const
00186 {
00187     return (strcmp(m_Data,rhs.m_Data) <= 0);
00188 }
00189 
00190 bool vuString::operator>=(const vuString& rhs) const
00191 {
00192     return (strcmp(m_Data,rhs.m_Data) >= 0);
00193 }
00194 
00195 vuString& vuString::operator=(const char * rhs)
00196 {
00197         delete [] m_Data;
00198 
00199     m_Data = new char[strlen(rhs) + 1];
00200     strcpy(m_Data, rhs);
00201 
00202         return *this;
00203 }
00204 
00205 vuString vuString::operator+(const char* rhs) const
00206 {
00207         vuString str = *this;
00208 
00209         str += rhs;
00210         return str;
00211 }
00212 
00213 vuString& vuString::operator+=(const char* rhs)
00214 {
00215     int len = strlen(m_Data) + strlen(rhs);
00216     char *temp = new char[len + 1];
00217     strcpy(temp,m_Data);
00218     strcpy(temp+strlen(m_Data),rhs);
00219     delete [] m_Data;
00220     m_Data = temp;
00221 
00222     return *this;
00223 }
00224 
00225 vuString& vuString::operator<<(const char* rhs)
00226 {
00227     *this += rhs;
00228     return *this;
00229 }
00230 
00231 bool vuString::operator==(const char* rhs) const
00232 {
00233         return (strcmp(m_Data, rhs) == 0);
00234 }
00235 
00236 bool vuString::operator!=(const char* rhs) const
00237 {
00238     return (strcmp(m_Data, rhs) != 0);
00239 }
00240 
00241 bool vuString::operator<(const char* rhs) const
00242 {
00243     return (strcmp(m_Data, rhs) < 0);
00244 }
00245 
00246 bool vuString::operator>(const char* rhs) const
00247 {
00248     return (strcmp(m_Data, rhs) > 0);
00249 }
00250 
00251 bool vuString::operator<=(const char* rhs) const
00252 {
00253     return (strcmp(m_Data, rhs) <= 0);
00254 }
00255 
00256 bool vuString::operator>=(const char* rhs) const
00257 {
00258     return (strcmp(m_Data, rhs) >= 0);
00259 }
00260 
00261 ostream& operator<<(ostream& os,const vuString& rhs)
00262 {
00263     os << rhs.m_Data;
00264     return os;
00265 }
00266 
00267 vuString operator+(const char *str,const vuString& rhs)
00268 {
00269         vuString newstr = str;
00270         newstr += rhs;
00271 
00272     return newstr;
00273 }
00274 
00275 bool vuString::hasPrefix(const vuString &prefix) const
00276 {
00277   dword cnt = prefix.getLength();
00278   if (getLength() < cnt) return false;
00279 
00280   for (dword i=0; i<cnt; i++) {
00281     if (m_Data[i] != prefix[i]) return false;
00282   }
00283   return true;
00284 }
00285 
00286 bool vuString::hasSuffix(const vuString &suffix) const
00287 {
00288   dword cnt  = suffix.getLength();
00289   dword cnt2 = getLength();
00290 
00291   if (cnt2 < cnt) return false;
00292 
00293   for (dword i=1; i<=cnt; i++) {
00294     if (m_Data[cnt2-i] != suffix[cnt-i]) return false;
00295   }
00296   return true;
00297 }
00298 
00299 vuString vuString::getLastPathComponent() const
00300 {
00301   vuString result;
00302 
00303   dword pos = 0; // last occurance of '/'
00304   dword cnt = getLength();
00305 
00306   for (dword i=0; i<cnt; i++) {
00307     if (m_Data[i] == '/') pos = i+1;
00308   }
00309   return substr(pos, cnt-1);
00310 }
00311 
00312 vuString vuString::getPathExtension() const
00313 {
00314   vuString result;
00315 
00316   dword pos = getLength(); // last occurance of '.'
00317   dword cnt = getLength();
00318 
00319   for (dword i=0; i<cnt; i++) {
00320     if (m_Data[i] == '.') pos = i+1;
00321   }
00322   return substr(pos, cnt-1);
00323 }

Generated on Wed Dec 15 21:20:38 2004 for vuVolume by  doxygen 1.3.9.1