Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

String.h

Go to the documentation of this file.
00001 
00039 #ifndef __SMART_ARRAY_STRING_H__
00040 #define __SMART_ARRAY_STRING_H__
00041 
00042 #include "SmartArray.h"
00043 #include <stdio.h>
00044 #include <string.h>
00045 
00046 typedef const char* LPCTSTR;
00047 
00057 class String : public SmartArray<char>
00058 {
00059 public:
00060     String(const String& str) : SmartArray<char>(str) {}
00061 
00062         String(const char* sz = NULL)
00063         {
00064                 operator=((sz) ? sz:"");
00065         }
00066 
00067         String(int nSize) : SmartArray<char>(nSize > 0 ? nSize:1)
00068         {
00069                 operator[](0) = '\0';
00070         }
00071 
00072         bool IsEmpty() const
00073         {
00074                 ASSERT(GetSize() > 0);
00075 
00076                 return GetSize() == 1;
00077         }
00078 
00079         void Clear()
00080         {
00081                 if (!IsEmpty())
00082                 {
00083                         ReSize(1);
00084                         operator[](0) = '\0';
00085                 }
00086         }
00087 
00089         String& operator=(const char* sz)
00090         {               
00091                 int i, nLen;
00092                 
00093                 for (nLen = 0; sz[nLen] != '\0'; nLen++);
00094 
00095                 ReSize(++nLen);
00096 
00097                 for (i = 0; i < nLen; i++)
00098                         operator[](i) = sz[i];
00099 
00100                 return *this;
00101         }
00102 
00103         String& operator=(int n)
00104         {
00105                 char szBuff[100];
00106                 sprintf(szBuff, "%d", n);
00107                 return operator=((const char*) szBuff);
00108         }
00109 
00110         String& operator=(const double& n)
00111         {
00112                 char szBuff[100];
00113                 sprintf(szBuff, "%.3f", n);
00114                 return operator=((const char*) szBuff);
00115         }
00116 
00117         String& operator+(const char* sz)
00118         {
00119                 int nLen1 = Len();
00120                 int nLen2 = strlen(sz);
00121                 String str;
00122                 int i;
00123 
00124                 str.ReSize(nLen1 + nLen2 + 1);
00125                 
00126                 for (i = 0; i < nLen1; i++)
00127                         str[i] = operator[](i);
00128 
00129                 for (i = 0; i < nLen2; i++)
00130                         str[i + nLen1] = sz[i];
00131 
00132                 str[nLen1 + nLen2] = '\0';
00133 
00134                 return operator=(str);
00135         }
00136 
00137         operator const char*() const { return GetData(); }
00138 
00139         bool operator==(const char* sz) const { return !strcmp(GetData(), sz); }
00140         bool operator!=(const char* sz) const { return !operator==(sz); }
00141 
00142         int Len() const { return strlen(GetData()); }
00143         int length() const { return Len(); }
00144 
00145         istream& Read(istream& is)
00146         {
00147                 SmartArray<char>::Read(is);
00148 
00149                 // Ensure is a zero terminated string
00150                 if (GetSize() == 0)
00151                 {
00152                         ReSize(1);
00153                         operator[](0) = '\0';
00154                 }
00155 
00156                 return is;
00157         }
00158 
00159         friend ostream& operator<<(ostream &os, const String& str) 
00160         {       
00161                 return os << (const char*) str;
00162         }
00163 };
00164 
00165 #endif //__SMART_ARRAY_STRING_H__

Generated on Sat Nov 13 11:21:26 2004 for Noisy DAG Matcher by doxygen1.2.18