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