Blender V2.61 - r43446
|
00001 /* 00002 * ***** BEGIN GPL LICENSE BLOCK ***** 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software Foundation, 00016 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00017 * 00018 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00019 * All rights reserved. 00020 * 00021 * The Original Code is: all of this file. 00022 * 00023 * Contributor(s): none yet. 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 */ 00027 00040 #ifndef _STR_String_H_ 00041 #define _STR_String_H_ 00042 00043 #ifndef STR_NO_ASSERTD 00044 #undef assertd 00045 #define assertd(exp) ((void)NULL) 00046 #endif 00047 00048 #include <vector> 00049 #include <limits.h> 00050 00051 #include <cstring> 00052 #include <cstdlib> 00053 00054 using namespace std; 00055 00056 #ifdef WITH_CXX_GUARDEDALLOC 00057 #include "MEM_guardedalloc.h" 00058 #endif 00059 00060 #ifdef _WIN32 00061 #define stricmp _stricmp 00062 #endif 00063 00064 class STR_String; 00065 00066 typedef unsigned long dword; 00067 typedef const STR_String& rcSTR_String; 00068 typedef unsigned char byte; 00069 00074 class STR_String 00075 { 00076 public: 00077 // Initialization 00078 STR_String(); 00079 STR_String(char c); 00080 STR_String(char c, int len); 00081 STR_String(const char *str); 00082 STR_String(const char *str, int len); 00083 STR_String(const STR_String &str); 00084 STR_String(const STR_String & str, int len); 00085 STR_String(const char *src1, int src1_len, const char *src2, int src2_len); 00086 explicit STR_String(int val); 00087 explicit STR_String(dword val); 00088 explicit STR_String(float val); 00089 explicit STR_String(double val); 00090 inline ~STR_String() { delete[] pData; } 00091 00092 // Operations 00093 STR_String& Format(const char *fmt, ...); // Set formatted text to string 00094 STR_String& FormatAdd(const char *fmt, ...); // Add formatted text to string 00095 inline void Clear() { Len = pData[0] = 0; } 00096 inline const STR_String & Reverse() 00097 { 00098 for (int i1=0, i2=Len-1; i1<i2; i1++, i2--) 00099 swap(pData[i1], pData[i2]); return *this; 00100 } 00101 00102 // Properties 00103 bool IsUpper() const; 00104 bool IsLower() const; 00105 inline bool IsEmpty() const { return Len==0; } 00106 inline int Length() const { return Len; } 00107 00108 // Data access 00109 inline STR_String& SetLength(int len) { AllocBuffer(len, true); Len=len; pData[len]=0; return *this; } 00110 inline char GetAt(int pos) const { assertd(pos<Len); return pData[pos]; } 00111 inline void SetAt(int pos, char c) { assertd(pos<Len); pData[pos]=c; } 00112 inline void SetAt(int pos, rcSTR_String str); 00113 inline void SetAt(int pos, int num, rcSTR_String str); 00114 void Replace(int pos, rcSTR_String str); 00115 void Replace(int pos, int num, rcSTR_String str); 00116 00117 // Substrings 00118 inline STR_String Left(int num) const { num = (num < Len ? num:Len ); return STR_String(pData, num); } 00119 inline STR_String Right(int num) const { num = (num < Len ? num:Len ); return STR_String(pData+Len-num, num); } 00120 inline STR_String Mid(int pos, int num = INT_MAX) const { pos = (pos < Len ? pos:Len ); num = (num < (Len - pos) ? num : (Len - pos)); return STR_String(pData+pos, num); } 00121 00122 // Comparison 00123 int Compare(rcSTR_String rhs) const; 00124 int CompareNoCase(rcSTR_String rhs) const; 00125 inline bool IsEqual(rcSTR_String rhs) const { return (Compare(rhs)==0); } 00126 inline bool IsEqualNoCase(rcSTR_String rhs) const { return (CompareNoCase(rhs)==0); } 00127 00128 // Search/replace 00129 int Find(char c, int pos = 0) const; 00130 int Find(const char *str, int pos = 0) const; 00131 int Find(rcSTR_String str, int pos = 0) const; 00132 int RFind(char c) const; 00133 int FindOneOf(const char *set, int pos = 0) const; 00134 int RFindOneOf(const char *set, int pos = 0) const; 00135 00136 vector<STR_String> Explode(char c) const; 00137 00138 // Formatting 00139 STR_String& Upper(); 00140 STR_String& Lower(); 00141 STR_String& Capitalize(); 00142 STR_String& TrimLeft(); 00143 STR_String& TrimLeft(char *set); 00144 STR_String& TrimRight(); 00145 STR_String& TrimRight(char *set); 00146 STR_String& Trim(); 00147 STR_String& Trim(char *set); 00148 STR_String& TrimQuotes(); 00149 00150 // Conversions 00151 // inline operator char*() { return pData; } 00152 inline operator const char *() const { return pData; } 00153 inline char *Ptr() { return pData; } 00154 inline const char *ReadPtr() const { return pData; } 00155 inline float ToFloat() const { float x=(float)(atof(pData)); return x; } 00156 inline int ToInt() const { return atoi(pData); } 00157 00158 // Operators 00159 inline rcSTR_String operator=(const byte *rhs) { return Copy((const char *)rhs, strlen((const char *)rhs)); } 00160 inline rcSTR_String operator=(rcSTR_String rhs) { return Copy(rhs.ReadPtr(), rhs.Length()); } 00161 inline rcSTR_String operator=(char rhs) { return Copy(&rhs, 1); } 00162 inline rcSTR_String operator=(const char *rhs) { return Copy(rhs, strlen(rhs)); } 00163 00164 inline rcSTR_String operator+=(const char *rhs) { return Concat(rhs, strlen(rhs)); } 00165 inline rcSTR_String operator+=(rcSTR_String rhs) { return Concat(rhs.ReadPtr(), rhs.Length()); } 00166 inline rcSTR_String operator+=(char rhs) { return Concat(&rhs, 1); } 00167 00168 00169 inline friend bool operator<(rcSTR_String lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)<0); } 00170 inline friend bool operator<(rcSTR_String lhs, const char *rhs) { return (strcmp(lhs, rhs)<0); }; 00171 inline friend bool operator<(const char *lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)<0); } 00172 inline friend bool operator>(rcSTR_String lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)>0); } 00173 inline friend bool operator>(rcSTR_String lhs, const char *rhs) { return (strcmp(lhs, rhs)>0); } 00174 inline friend bool operator>(const char *lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)>0); } 00175 inline friend bool operator<=(rcSTR_String lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)<=0); } 00176 inline friend bool operator<=(rcSTR_String lhs, const char *rhs) { return (strcmp(lhs, rhs)<=0); } 00177 inline friend bool operator<=(const char *lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)<=0); } 00178 inline friend bool operator>=(rcSTR_String lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)>=0); } 00179 inline friend bool operator>=(rcSTR_String lhs, const char *rhs) { return (strcmp(lhs, rhs)>=0); } 00180 inline friend bool operator>=(const char *lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)>=0); } 00181 inline friend bool operator==(rcSTR_String lhs, rcSTR_String rhs) { return ((lhs.Length() == rhs.Length()) && (memcmp(lhs, rhs, lhs.Length())==0)); } 00182 inline friend bool operator==(rcSTR_String lhs, const char *rhs) { return (memcmp(lhs, rhs, lhs.Length()+1)==0); } 00183 inline friend bool operator==(const char *lhs, rcSTR_String rhs) { return (memcmp(lhs, rhs, rhs.Length()+1)==0); } 00184 inline friend bool operator!=(rcSTR_String lhs, rcSTR_String rhs) { return ((lhs.Length() != rhs.Length()) || (memcmp(lhs, rhs, lhs.Length())!=0)); } 00185 inline friend bool operator!=(rcSTR_String lhs, const char *rhs) { return (memcmp(lhs, rhs, lhs.Length()+1)!=0); } 00186 inline friend bool operator!=(const char *lhs, rcSTR_String rhs) { return (memcmp(lhs, rhs, rhs.Length()+1)!=0); } 00187 00188 // serializing 00189 //int Serialize(pCStream stream); 00190 00191 protected: 00192 // Implementation 00193 void AllocBuffer(int len, bool keep_contents); 00194 rcSTR_String Copy(const char *src, int len); 00195 rcSTR_String Concat(const char *data, int len); 00196 00197 static bool isLower(char c) { return !isUpper(c); } 00198 static bool isUpper(char c) { return (c>='A') && (c <= 'Z'); } 00199 static bool isSpace(char c) { return (c==' ') || (c=='\t'); } 00200 00201 char *pData; // -> STR_String data 00202 int Len; // Data length 00203 int Max; // Space in data buffer 00204 00205 00206 #ifdef WITH_CXX_GUARDEDALLOC 00207 public: 00208 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "CXX:STR_String"); } 00209 void operator delete(void *mem) { MEM_freeN(mem); } 00210 #endif 00211 }; 00212 00213 inline STR_String operator+(rcSTR_String lhs, rcSTR_String rhs) { return STR_String(lhs.ReadPtr(), lhs.Length(), rhs.ReadPtr(), rhs.Length()); } 00214 inline STR_String operator+(rcSTR_String lhs, char rhs) { return STR_String(lhs.ReadPtr(), lhs.Length(), &rhs, 1); } 00215 inline STR_String operator+(char lhs, rcSTR_String rhs) { return STR_String(&lhs, 1, rhs.ReadPtr(), rhs.Length()); } 00216 inline STR_String operator+(rcSTR_String lhs, const char *rhs) { return STR_String(lhs.ReadPtr(), lhs.Length(), rhs, strlen(rhs)); } 00217 inline STR_String operator+(const char *lhs, rcSTR_String rhs) { return STR_String(lhs, strlen(lhs), rhs.ReadPtr(), rhs.Length()); } 00218 00219 00220 #endif //_STR_String_H_ 00221