1 /* 2 * Copyright 2007, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * All rights reserved. Distributed under the terms of the MIT license. 4 */ 5 #ifndef STRING_H 6 #define STRING_H 7 8 #include <SupportDefs.h> 9 #include <string.h> 10 #include <new> 11 12 13 // String 14 class String { 15 public: 16 inline String(); 17 inline String(const String &string); 18 inline String(const char *string, int32 length = -1); 19 inline ~String(); 20 21 inline bool SetTo(const char *string, int32 maxLength = -1); 22 inline void Unset(); 23 24 inline void Truncate(int32 newLength); 25 26 inline const char *GetString() const; 27 inline int32 GetLength() const { return fLength; } 28 29 inline String &operator=(const String &string); 30 inline bool operator==(const String &string) const; 31 inline bool operator!=(const String &string) const { return !(*this == string); } 32 33 private: 34 inline bool _SetTo(const char *string, int32 length); 35 36 private: 37 int32 fLength; 38 char *fString; 39 }; 40 41 /*! 42 \class String 43 \brief A very simple string class. 44 */ 45 46 // constructor 47 String::String() 48 : fLength(0), 49 fString(NULL) 50 { 51 } 52 53 // copy constructor 54 String::String(const String &string) 55 : fLength(0), 56 fString(NULL) 57 { 58 *this = string; 59 } 60 61 // constructor 62 String::String(const char *string, int32 length) 63 : fLength(0), 64 fString(NULL) 65 { 66 SetTo(string, length); 67 } 68 69 // destructor 70 String::~String() 71 { 72 Unset(); 73 } 74 75 // SetTo 76 bool 77 String::SetTo(const char *string, int32 maxLength) 78 { 79 if (string) { 80 if (maxLength > 0) 81 maxLength = strnlen(string, maxLength); 82 else if (maxLength < 0) 83 maxLength = strlen(string); 84 } 85 return _SetTo(string, maxLength); 86 } 87 88 // Unset 89 void 90 String::Unset() 91 { 92 if (fString) { 93 delete[] fString; 94 fString = NULL; 95 } 96 fLength = 0; 97 } 98 99 // Truncate 100 void 101 String::Truncate(int32 newLength) 102 { 103 if (newLength < 0) 104 newLength = 0; 105 if (newLength < fLength) { 106 char *string = fString; 107 fString = NULL; 108 if (!_SetTo(string, newLength)) { 109 fString = string; 110 fLength = newLength; 111 fString[fLength] = '\0'; 112 } else 113 delete[] string; 114 } 115 } 116 117 // GetString 118 const char * 119 String::GetString() const 120 { 121 if (fString) 122 return fString; 123 return ""; 124 } 125 126 // = 127 String & 128 String::operator=(const String &string) 129 { 130 if (&string != this) 131 _SetTo(string.fString, string.fLength); 132 return *this; 133 } 134 135 // == 136 bool 137 String::operator==(const String &string) const 138 { 139 return (fLength == string.fLength 140 && (fLength == 0 || !strcmp(fString, string.fString))); 141 } 142 143 // _SetTo 144 bool 145 String::_SetTo(const char *string, int32 length) 146 { 147 bool result = true; 148 Unset(); 149 if (string && length > 0) { 150 fString = new(std::nothrow) char[length + 1]; 151 if (fString) { 152 memcpy(fString, string, length); 153 fString[length] = '\0'; 154 fLength = length; 155 } else 156 result = false; 157 } 158 return result; 159 } 160 161 162 #endif // STRING_H 163