Wiselib
|
00001 /*************************************************************************** 00002 ** This file is part of the generic algorithm library Wiselib. ** 00003 ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project. ** 00004 ** ** 00005 ** The Wiselib is free software: you can redistribute it and/or modify ** 00006 ** it under the terms of the GNU Lesser General Public License as ** 00007 ** published by the Free Software Foundation, either version 3 of the ** 00008 ** License, or (at your option) any later version. ** 00009 ** ** 00010 ** The Wiselib is distributed in the hope that it will be useful, ** 00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** 00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** 00013 ** GNU Lesser General Public License for more details. ** 00014 ** ** 00015 ** You should have received a copy of the GNU Lesser General Public ** 00016 ** License along with the Wiselib. ** 00017 ** If not, see <http://www.gnu.org/licenses/>. ** 00018 ***************************************************************************/ 00019 #ifndef WISELIB_PSTL_STRING 00020 #define WISELIB_PSTL_STRING 00021 #define MAX_STRING_LENGTH 64 00022 00023 namespace wiselib { 00024 00025 char *mystrncpy(char *target1, const char *source1, int no) { 00026 char *tempt = target1; 00027 for (int n = 0; n < no;) { 00028 *target1 = *source1; 00029 target1++; 00030 source1++; 00031 n++; 00032 } 00033 00034 *target1 = '\0'; 00035 return tempt; 00036 } 00037 00038 class StaticString { 00039 private: 00040 unsigned char length_; 00041 char buffer_[MAX_STRING_LENGTH]; 00042 00043 char max(const char a, const char b){ 00044 if(a>=b) 00045 return a; 00046 else return b; 00047 } 00048 00049 char min(const char a, const char b){ 00050 if(a<=b) 00051 return a; 00052 else return b; 00053 } 00054 00055 public: 00056 00057 StaticString() { 00058 length_ = 0; 00059 } 00060 00061 00062 00063 StaticString(const char* value) 00064 { 00065 length_ = strlen(value); 00066 memcpy( buffer_, value, length_ + 1 ); 00067 } 00068 00069 StaticString(char *buffer, char buffer_size) { 00070 length_ = buffer_size; 00071 memcpy( buffer_, buffer, length_ + 1 ); 00072 } 00073 00074 char operator[] (int pos) const { 00075 return (pos >= length_) ? NULL : buffer_[pos]; 00076 } 00077 00078 StaticString& append(const char *other) { 00079 int length = strlen(other); 00080 if (length_ + length + 1 <= MAX_STRING_LENGTH) { 00081 // memcpy( buffer_[length_], other, length ); 00082 00083 mystrncpy(&(buffer_[length_]), other, length); 00084 // buffer_[length_ - 1] = '\0'; 00085 length_ += length; 00086 } 00087 00088 return *this; 00089 } 00090 00091 StaticString& append(StaticString& other) { 00092 return append(other.c_str()); 00093 } 00094 00095 char* c_str() { 00096 return buffer_; 00097 } 00098 00099 int length() const{ 00100 return length_; 00101 } 00102 00103 inline bool operator == (const StaticString& s) 00104 { 00105 if(s.length()!=length_) 00106 return false; 00107 for(int i = 0; i<length_;i++){ 00108 if(buffer_[i]!=s[i]) 00109 return false; 00110 } 00111 return true; 00112 } 00113 00114 inline bool operator == (const StaticString* s){ 00115 return operator ==(*s); 00116 } 00117 00118 inline bool operator!= (const StaticString& s) 00119 { 00120 return (!operator==(s)); 00121 } 00122 inline bool operator!= (const StaticString* s) 00123 { 00124 return (!operator==(s)); 00125 } 00126 00127 inline bool operator < (const StaticString& s){ 00128 for(int i = 0; i<min(length_,s.length());i++){ 00129 if(buffer_[i]<s[i]) 00130 return true; 00131 if(buffer_[i]>s[i]) 00132 return false; 00133 } 00134 return length_<=s.length(); 00135 } 00136 00137 inline bool operator > (const StaticString& s){ 00138 for(int i = 0; i<min(length_,s.length());i++){ 00139 if(buffer_[i]>s[i]) 00140 return true; 00141 if(buffer_[i]<s[i]) 00142 return false; 00143 } 00144 return length_>=s.length(); 00145 } 00146 00147 inline bool operator < (const StaticString* s){ 00148 return operator <(*s); 00149 } 00150 00151 inline bool operator > (const StaticString* s){ 00152 return operator <(*s); 00153 } 00154 inline bool operator >= (const StaticString& s){ 00155 return !(operator <(s)); 00156 } 00157 inline bool operator >= (const StaticString* s){ 00158 return operator >=(*s); 00159 } 00160 inline bool operator <= (const StaticString& s){ 00161 return !(operator >(s)); 00162 } 00163 inline bool operator <= (const StaticString* s){ 00164 return operator <=(*s); 00165 } 00166 inline void operator = (const char* s) 00167 { 00168 length_ = strlen(s); 00169 memcpy( buffer_, s, length_ + 1 ); 00170 } 00171 00172 00173 }; 00174 00175 } 00176 00177 #endif