AppEasy Core SDK
1.5.0
Cross platform mobile and desktop app and game development SDK - The easy way to make apps
|
00001 // 00002 // 00003 // AppEasy SDK - Cross Platform Multi-purpose Game and App Engine 00004 // 00005 // Developed by Matthew Hopwood of Pocketeers Limited - www.pocketeers.co.uk 00006 // 00007 // For updates, tutorials and more details check out www.appeasymobile.com 00008 // 00009 // This code is provided free of charge and without any warranty whatsoever. You must ensure that this whole notice is present in all files and derivatives, so the reader knows its origin. 00010 // If you use this SDK in your product then please ensure that you credit AppEasy's usage appropriately. Please see www.appeasymobile.com for licensing details and support 00011 // 00012 // 00013 00014 #ifndef __CZ_STRING_H__ 00015 #define __CZ_STRING_H__ 00016 00017 #include <string> 00018 #include "CzUtil.h" 00019 #include "CzSlotArray.h" 00020 00021 /** 00022 @addtogroup Core 00023 @{ 00024 */ 00025 00026 /** 00027 @class CzString 00028 00029 @brief A simple named string builder style class 00030 00031 <h1>Introduction</h1> 00032 00033 Strings are used extensively throughout app / game development, making it an incredibly important subject. We use strings for everything from naming objects to presenting interactive text to the player. 00034 00035 String building can be a nightmare for memory managers as constantly rebuilding strings causes many memory allocations and deallocations fragmenting the available memory into small hard to use units. 00036 00037 A string builder is a class that allows you to build a string using a predefined sized buffer or at the very least a buffer than can be resized. CzString supports the following features: 00038 - String builder functionality 00039 - String concetanation and resizing 00040 - String building from basic types such as integers, floats and boolean types 00041 - String comparison 00042 - String splitting 00043 - Stream style string searching 00044 - Find strings between markers 00045 - Character replacement 00046 - HTML decoding 00047 - Hex encoding / decoding 00048 - URL encoding / decoding 00049 - Change of case 00050 - Extraction of numbers / items and lists of numbers / items 00051 00052 <h1>Basic String Building</h1> 00053 00054 Strings can be created from raw text, integers, floats, boolean and even vector variables as shown below: 00055 00056 @code 00057 CzString string("Hello"); // Creation from raw text 00058 CzString int_string(1234); // Creation from an integer 00059 CzString int_string(100.234f); // Creation from a float 00060 CzString int_string(true); // Creation from a boolean 00061 @endcode 00062 00063 Strings can also be concatenated: 00064 00065 00066 @eode 00067 CzString string("Hello"); 00068 string += ". How you doing"; 00069 @endcode 00070 00071 00072 If you are creating a string and you know that it will require quite a number of concatenations then you should set its initial size to prevent memory resizing, here's an example: 00073 00074 @code 00075 CzString string; 00076 string.allocString(1024); // here we preallocate 1024 bytes of memory for the string 00077 string += "Hello!"; 00078 string += " How you doing."; 00079 string += " I'm great thanks, how are you?"; 00080 string += " Fantastico!"; 00081 @endcode 00082 00083 <h1>Comparing Strings</h1> 00084 00085 When two strings are compared using the CzString class their hash values are compared for equality. By default each time you create or modify a string a hash value for the string is recalculated. 00086 When building a long string it may be a good idea to turn auto string hashing off until the string has been built then turn itback on just before your last concatenation: 00087 00088 @code 00089 CzString string; 00090 string.allocString(1024); 00091 string.setAutoHash(false); 00092 string += "Hello!"; 00093 string += " How you doing."; 00094 string += " I'm great thanks, how are you?"; 00095 string.setAutoHash(true); 00096 string += " Fantastico!"; 00097 @endcode 00098 00099 Note that if string auto hashing is disabled then a full string compare will be carried out. 00100 00101 <h1>Stream Style Searching</h1> 00102 00103 CzString is set up to allow stream like searching whereby your last searched position will be saved, allowing you to carry out additional searches from where the last search left off. This 00104 type of string searching is incredibly useful when it comes to parsing areas of memory. The following methods can be used: 00105 00106 <h1>Getting Strings Values</h1> 00107 00108 CzString provides some useful methods for converting from strings to certain other types: 00109 00110 @code 00111 int getAsInt() const; // Returns the integer value of the string 00112 bool getAsBool() const; // Returns the boolean value of the string. Valid values include true and 1, all other values are classed as false 00113 float getAsFloat() const; // Returns the floating point value of the string 00114 int getAsListOfInt(int *int_pool); // Returns a list of integers (string should contain comma separated values) 00115 int getAsListOfFloat(float* float_pool); // Returns a list of floats (string should contain comma separated values) 00116 int getAsVectorOfFloat(CzVector<float> *int_pool); // Returns a vector of ints (string should contain comma separated values) 00117 int getAsVectorOfInt(CzVector<int> *int_pool); // Returns a vector of floats (string should contain comma separated values) 00118 @endcode 00119 00120 <h1>Other Useful String Tools</h1> 00121 00122 CzString contains many other useful utility methods to help make various tasks easier: 00123 - Replace() - Replaces all occurrences of char / string with the replacement in a string 00124 - Contains() - Returns true if a string contains the specified character 00125 - ReplaceHTMLCodes() - Replaces HTML style codes such as & with their ACII equivalents 00126 - URLEncode() - Encodes a string as URL encoded 00127 - URLDecode() - Decodes a URL encoded string 00128 - HexEncode() - Encodes the text as hexadecimal 00129 - HexDecode() - Decodes a string of Hex to text 00130 - ToLower() - Converts a string to all lower case 00131 - ToUpper() - Converts a string to all upper case 00132 - SplitFileName() - Splits a string into file name and extension strings 00133 - GetFilenameExt() - Extracts a file names extension as a string 00134 - Split() - Splits a string using a specified separator into an array of strings 00135 - SplitVarIndex() - Splits an XOML variable name / index (var_name:index) 00136 - SplitPropVarIndex() - Splits an XOML property variable name / index ([prop_name]var_name:index) 00137 00138 */ 00139 00140 class CzString 00141 { 00142 // Properties 00143 protected: 00144 char* Data; ///< The string data 00145 int Length; ///< Length of the string 00146 int Size; ///< Size of memory reserved for the string 00147 unsigned int DataHash; ///< Hash value of the data (for fast comparison) 00148 // unsigned int NameHash; ///< Hash value of the name (for fast searching) 00149 bool AutoHash; ///< true to calculate hash value every time string is updated 00150 int FindIndex; ///< Used to track multiple searches 00151 00152 public: 00153 void reallocString(int len); ///< Reallocate the amount of space available to the string 00154 void allocString(int len); ///< Allocates a new string, destroying the existing string 00155 void reset(); ///< Destroys the string 00156 void setString(const char *str); ///< Sets the string to the new string 00157 void setString(const char *str, int len); ///< Sets the string to the new string of specified length 00158 char* getString() { return Data; } ///< Returns the string 00159 CzString getSubString(int start, int max_chars); ///< Returns a sub string of this string 00160 CzString getSubString(int start); ///< Returns all chars from start to the enmd of the string 00161 // void setName(char *name); 00162 // unsigned int getNameHash() const { return NameHash; } 00163 unsigned int getHash() const { return DataHash; } ///< Gets ths strings hash value 00164 void setAutoHash(bool enable); ///< Sets string auo hashing on / off 00165 void setLength(int length) { Length = length; } ///< Sets ths strings length (does not affect the amount of memory allocated to the string) 00166 bool isAutohash() const { return AutoHash; } ///< Returns true if auto hashing is enabled 00167 // Properties end 00168 00169 private: 00170 char* alloc(int num_chars); 00171 void free(char*& mem); 00172 00173 public: 00174 CzString() : Data(NULL), Length(0), Size(0), AutoHash(true), FindIndex(0), DataHash(0) {} 00175 CzString(const CzString &string); 00176 CzString(const char *pString, int start, int num_chars); 00177 CzString(const char *pString); 00178 CzString(int v); 00179 CzString(unsigned int v); 00180 CzString(float v); 00181 CzString(bool v); 00182 CzString(const CzVec2& v); 00183 CzString(const CzVec3& v); 00184 CzString(const CzVec4& v); 00185 CzString(const CzColour& v); 00186 virtual ~CzString() 00187 { 00188 if (Data != NULL) 00189 delete [] Data; 00190 } 00191 00192 // Query 00193 int getSize() const { return Size; } ///< Returns the size of the string. Size is the amount of memory available to the string 00194 int getLength() const { return Length; } ///< Returns the length of the string 00195 int getAsInt() const; ///< Returns the integer value of the string 00196 bool getAsBool() const; ///< Returns the boolean value of the string. Valid values include true and 1, all other values are classed as false 00197 float getAsFloat() const; ///< Returns the floating point value of the string 00198 int getAsListOfInt(int *int_pool) const; ///< Returns a list of integers (string should contain comma separated values) 00199 int getAsListOfFloat(float* float_pool) const; ///< Returns a list of floats (string should contain comma separated values) 00200 int getAsVectorOfFloat(CzVector<float> *int_pool); ///< Returns a vector of ints (string should contain comma separated values) 00201 int getAsVectorOfInt(CzVector<int> *int_pool); ///< Returns a vector of floats (string should contain comma separated values) 00202 bool isEmpty() const { return Data == NULL || Length == 0; } ///< Returns true if the string is empty 00203 00204 void set(int v); 00205 void set(float v); 00206 void set(bool v); 00207 void set(const CzVec2& v); 00208 void set(const CzVec3& v); 00209 void set(const CzVec4& v); 00210 void set(const CzColour& v); 00211 00212 // Copy 00213 void Copy(const char* pString); 00214 void Copy(CzString& pString); 00215 void Copy(const char *pString, int start, int count); 00216 00217 // Operators 00218 CzString& operator= (const CzString& op); 00219 CzString& operator= (const char *op); 00220 char operator[] (int nIndex); 00221 CzString& operator+= (const CzString& op); 00222 CzString& operator+= (const char* op); 00223 CzString& operator= (int v); 00224 CzString& operator= (float v); 00225 CzString& operator= (char chr); 00226 CzString& operator+= (int v); 00227 CzString& operator+= (float v); 00228 CzString& operator+= (char chr); 00229 bool operator== (const CzString& op); 00230 bool operator== (const char* op); 00231 bool operator== (unsigned int hash); 00232 char* str() const { return Data; } 00233 const char* c_str() const { return (const char *)Data; } 00234 00235 bool Compare(const char* pString, int len) const; ///< Compares two strings, returns true if same 00236 bool Compare(int start, const char* pString, int len) const; ///< Compares a string and a sub string, returns true if same 00237 00238 // Searching 00239 int Find(const char* string); ///< Simple string search 00240 int FindNext(const char* string, int len); ///< Searches from last find position for text string 00241 int FindNext(const char* string); ///< Searches from last find position for text string 00242 int FindNext(char chr); ///< Searches from last find position for the specified character 00243 void FindReset(); ///< Resets the find position to start of string 00244 int StepFindIndex(int amount); ///< Adjust the find position by the specified 00245 int StepFindIndexNoneWhiteSpace(); ///< Adjust the find position to the next none white space 00246 int StepFindIndexWhiteSpace(); ///< Adjust the find position to the next white space 00247 void setFindIndex(int index) { FindIndex = index; } ///< Sets the current find index 00248 int getFindIndex() const { return FindIndex; } ///< Gets the current find index 00249 int GetNextMarkerOffset(char marker); ///< Returns length to the end of paramneter marker 00250 int GetNextString(); ///< Returns the next string 00251 int GetNextMarkedString(char start_mark, char end_mark, int &offset); ///< Returns a string marked by start and end marker characters 00252 int GetNextMarkedStringAfterString(const char* search_string, char start_mark, char end_mark, CzString& out_string); ///< Returns the next marked string after finding a certain string 00253 // Utility 00254 int Replace(const char* string, const char* with); 00255 void Replace(char chr, char with); 00256 bool ContainsHTMLCodes() const; 00257 int Contains(char c) const; 00258 int Occurrences(char c) const; 00259 void ReplaceHTMLCodes(); 00260 void URLEncode(const char* str); 00261 void URLEncode(); 00262 void URLDecode(); 00263 void HexEncode(const char* str, int num_bytes); 00264 void HexEncode(); 00265 void HexDecode(); 00266 bool SplitFilename(CzString& filename, CzString& ext); 00267 bool GetFilenameExt(CzString& ext); 00268 bool SplitVarIndex(CzString &var, int &index, CzString& vindex); 00269 bool SplitPropVarIndex(CzString &prop, CzString &var, int &index, CzString& vindex); 00270 CzSlotArray<CzString*>* Split(char split_char); 00271 void Split(char split_char, CzSlotArray<CzString*>* strings); 00272 00273 static unsigned int CalculateHash(const char* pString, int hash = 5381); 00274 00275 inline static bool IsNumber(char c) 00276 { 00277 if (c >= '0' && c <= '9') 00278 return true; 00279 return false; 00280 } 00281 00282 inline static bool IsAlpha(char c) 00283 { 00284 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) 00285 return true; 00286 return false; 00287 } 00288 00289 inline static bool IsLower(char c) 00290 { 00291 if (c >= 'a' && c <= 'z') 00292 return true; 00293 return false; 00294 } 00295 00296 inline static bool IsUpper(char c) 00297 { 00298 if (c >= 'A' && c <= 'Z') 00299 return true; 00300 return false; 00301 } 00302 00303 inline static int GetValueFromHexDigit(char c) 00304 { 00305 if (c >= '0' && c <= '9') 00306 return c - '0'; 00307 if (c >= 'a' && c <= 'f') 00308 return c - 'a' + 10; 00309 if (c >= 'A' && c <= 'F') 00310 return c - 'A' + 10; 00311 00312 return 0; 00313 } 00314 00315 void ToUpper(); 00316 void ToLower(); 00317 }; 00318 00319 /** 00320 @typedef CzList<CzString*> CzXmlStringList 00321 00322 @brief A list of CzString's 00323 */ 00324 typedef CzList<CzString*> CzStringList; 00325 00326 /// @} 00327 00328 #endif // __CZ_STRING_H__