PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Core/Dependencies/RakNet/Source/RakString.h

https://bitbucket.org/barakianc/nvidia-physx-and-apex-in-gge
C Header | 328 lines | 142 code | 77 blank | 109 comment | 1 complexity | a6c8c674949fa10a29b1205d44eb0750 MD5 | raw file
  1. #ifndef __RAK_STRING_H
  2. #define __RAK_STRING_H
  3. #include "Export.h"
  4. #include "DS_List.h"
  5. #include "RakNetTypes.h" // int64_t
  6. #include <stdio.h>
  7. #include "stdarg.h"
  8. #ifdef _WIN32
  9. #include "WindowsIncludes.h"
  10. #endif
  11. namespace RakNet
  12. {
  13. /// Forward declarations
  14. class SimpleMutex;
  15. class BitStream;
  16. /// \brief String class
  17. /// \details Has the following improvements over std::string
  18. /// -Reference counting: Suitable to store in lists
  19. /// -Variadic assignment operator
  20. /// -Doesn't cause linker errors
  21. class RAK_DLL_EXPORT RakString
  22. {
  23. public:
  24. // Constructors
  25. RakString();
  26. RakString(char input);
  27. RakString(unsigned char input);
  28. RakString(const unsigned char *format, ...);
  29. RakString(const char *format, ...);
  30. ~RakString();
  31. RakString( const RakString & rhs);
  32. /// Implicit return of const char*
  33. operator const char* () const {return sharedString->c_str;}
  34. /// Same as std::string::c_str
  35. const char *C_String(void) const {return sharedString->c_str;}
  36. // Lets you modify the string. Do not make the string longer - however, you can make it shorter, or change the contents.
  37. // Pointer is only valid in the scope of RakString itself
  38. char *C_StringUnsafe(void) {Clone(); return sharedString->c_str;}
  39. /// Assigment operators
  40. RakString& operator = ( const RakString& rhs );
  41. RakString& operator = ( const char *str );
  42. RakString& operator = ( char *str );
  43. RakString& operator = ( const unsigned char *str );
  44. RakString& operator = ( char unsigned *str );
  45. RakString& operator = ( const char c );
  46. /// Concatenation
  47. RakString& operator +=( const RakString& rhs);
  48. RakString& operator += ( const char *str );
  49. RakString& operator += ( char *str );
  50. RakString& operator += ( const unsigned char *str );
  51. RakString& operator += ( char unsigned *str );
  52. RakString& operator += ( const char c );
  53. /// Character index. Do not use to change the string however.
  54. unsigned char operator[] ( const unsigned int position ) const;
  55. #ifdef _WIN32
  56. // Return as Wide char
  57. // Deallocate with DeallocWideChar
  58. WCHAR * ToWideChar(void);
  59. void DeallocWideChar(WCHAR * w);
  60. #endif
  61. /// String class find replacement
  62. /// Searches the string for the content specified in stringToFind and returns the position of the first occurrence in the string.
  63. /// Search only includes characters on or after position pos, ignoring any possible occurrences in previous locations.
  64. /// \param[in] stringToFind The string to find inside of this object's string
  65. /// \param[in] pos The position in the string to start the search
  66. /// \return Returns the position of the first occurrence in the string.
  67. size_t Find(const char *stringToFind,size_t pos = 0 );
  68. /// Equality
  69. bool operator==(const RakString &rhs) const;
  70. bool operator==(const char *str) const;
  71. bool operator==(char *str) const;
  72. // Comparison
  73. bool operator < ( const RakString& right ) const;
  74. bool operator <= ( const RakString& right ) const;
  75. bool operator > ( const RakString& right ) const;
  76. bool operator >= ( const RakString& right ) const;
  77. /// Inequality
  78. bool operator!=(const RakString &rhs) const;
  79. bool operator!=(const char *str) const;
  80. bool operator!=(char *str) const;
  81. /// Change all characters to lowercase
  82. const char * ToLower(void);
  83. /// Change all characters to uppercase
  84. const char * ToUpper(void);
  85. /// Set the value of the string
  86. void Set(const char *format, ...);
  87. /// Sets a copy of a substring of str as the new content. The substring is the portion of str
  88. /// that begins at the character position pos and takes up to n characters
  89. /// (it takes less than n if the end of str is reached before).
  90. /// \param[in] str The string to copy in
  91. /// \param[in] pos The position on str to start the copy
  92. /// \param[in] n How many chars to copy
  93. /// \return Returns the string, note that the current string is set to that value as well
  94. RakString Assign(const char *str,size_t pos, size_t n );
  95. /// Returns if the string is empty. Also, C_String() would return ""
  96. bool IsEmpty(void) const;
  97. /// Returns the length of the string
  98. size_t GetLength(void) const;
  99. size_t GetLengthUTF8(void) const;
  100. /// Replace character(s) in starting at index, for count, with c
  101. void Replace(unsigned index, unsigned count, unsigned char c);
  102. /// Replace character at index with c
  103. void SetChar( unsigned index, unsigned char c );
  104. /// Replace character at index with string s
  105. void SetChar( unsigned index, RakNet::RakString s );
  106. /// Make sure string is no longer than \a length
  107. void Truncate(unsigned int length);
  108. void TruncateUTF8(unsigned int length);
  109. // Gets the substring starting at index for count characters
  110. RakString SubStr(unsigned int index, unsigned int count) const;
  111. /// Erase characters out of the string at index for count
  112. void Erase(unsigned int index, unsigned int count);
  113. /// Set the first instance of c with a NULL terminator
  114. void TerminateAtFirstCharacter(char c);
  115. /// Set the last instance of c with a NULL terminator
  116. void TerminateAtLastCharacter(char c);
  117. /// Returns how many occurances there are of \a c in the string
  118. int GetCharacterCount(char c);
  119. /// Remove all instances of c
  120. void RemoveCharacter(char c);
  121. /// Create a RakString with a value, without doing printf style parsing
  122. /// Equivalent to assignment operator
  123. static RakNet::RakString NonVariadic(const char *str);
  124. /// Has the string into an unsigned int
  125. static unsigned long ToInteger(const char *str);
  126. static unsigned long ToInteger(const RakString &rs);
  127. // Like strncat, but for a fixed length
  128. void AppendBytes(const char *bytes, unsigned int count);
  129. /// Compare strings (case sensitive)
  130. int StrCmp(const RakString &rhs) const;
  131. /// Compare strings (case sensitive), up to num characters
  132. int StrNCmp(const RakString &rhs, size_t num) const;
  133. /// Compare strings (not case sensitive)
  134. int StrICmp(const RakString &rhs) const;
  135. /// Clear the string
  136. void Clear(void);
  137. /// Print the string to the screen
  138. void Printf(void);
  139. /// Print the string to a file
  140. void FPrintf(FILE *fp);
  141. /// Does the given IP address match the IP address encoded into this string, accounting for wildcards?
  142. bool IPAddressMatch(const char *IP);
  143. /// Does the string contain non-printable characters other than spaces?
  144. bool ContainsNonprintableExceptSpaces(void) const;
  145. /// Is this a valid email address?
  146. bool IsEmailAddress(void) const;
  147. /// URL Encode the string. See http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029/
  148. RakNet::RakString& URLEncode(void);
  149. /// URL decode the string
  150. RakNet::RakString& URLDecode(void);
  151. /// https://servers.api.rackspacecloud.com/v1.0 to https://, servers.api.rackspacecloud.com, /v1.0
  152. void SplitURI(RakNet::RakString &header, RakNet::RakString &domain, RakNet::RakString &path);
  153. /// Scan for quote, double quote, and backslash and prepend with backslash
  154. RakNet::RakString& SQLEscape(void);
  155. /// Format as a POST command that can be sent to a webserver
  156. /// \param[in] uri For example, jenkinssoftware.com/raknet/DirectoryServer.php?query=download&downloadPassword=a
  157. /// \param[in] contentType For example, application/x-www-form-urlencoded
  158. /// \param[in] port Port you plan to send to. Use 80 if you don't know.
  159. /// \param[in] body Body of the post
  160. /// \return Formatted string
  161. static RakNet::RakString FormatForPOST(RakString &uri, RakString &contentType, unsigned int port, RakString &body);
  162. /// Format as a GET comand that can be sent to a webserver
  163. /// \param[in] uri For example, jenkinssoftware.com/raknet/DirectoryServer.php?query=download&downloadPassword=a
  164. /// \param[in] contentType For example, application/x-www-form-urlencoded
  165. /// \param[in] port Port you plan to send to. Use 80 if you don't know.
  166. /// \return Formatted string
  167. static RakNet::RakString FormatForGET(RakString &uri, unsigned int port);
  168. /// Fix to be a file path, ending with /
  169. RakNet::RakString& MakeFilePath(void);
  170. /// RakString uses a freeList of old no-longer used strings
  171. /// Call this function to clear this memory on shutdown
  172. static void FreeMemory(void);
  173. /// \internal
  174. static void FreeMemoryNoMutex(void);
  175. /// Serialize to a bitstream, uncompressed (slightly faster)
  176. /// \param[out] bs Bitstream to serialize to
  177. void Serialize(BitStream *bs) const;
  178. /// Static version of the Serialize function
  179. static void Serialize(const char *str, BitStream *bs);
  180. /// Serialize to a bitstream, compressed (better bandwidth usage)
  181. /// \param[out] bs Bitstream to serialize to
  182. /// \param[in] languageId languageId to pass to the StringCompressor class
  183. /// \param[in] writeLanguageId encode the languageId variable in the stream. If false, 0 is assumed, and DeserializeCompressed will not look for this variable in the stream (saves bandwidth)
  184. /// \pre StringCompressor::AddReference must have been called to instantiate the class (Happens automatically from RakPeer::Startup())
  185. void SerializeCompressed(BitStream *bs, uint8_t languageId=0, bool writeLanguageId=false) const;
  186. /// Static version of the SerializeCompressed function
  187. static void SerializeCompressed(const char *str, BitStream *bs, uint8_t languageId=0, bool writeLanguageId=false);
  188. /// Deserialize what was written by Serialize
  189. /// \param[in] bs Bitstream to serialize from
  190. /// \return true if the deserialization was successful
  191. bool Deserialize(BitStream *bs);
  192. /// Static version of the Deserialize() function
  193. static bool Deserialize(char *str, BitStream *bs);
  194. /// Deserialize compressed string, written by SerializeCompressed
  195. /// \param[in] bs Bitstream to serialize from
  196. /// \param[in] readLanguageId If true, looks for the variable langaugeId in the data stream. Must match what was passed to SerializeCompressed
  197. /// \return true if the deserialization was successful
  198. /// \pre StringCompressor::AddReference must have been called to instantiate the class (Happens automatically from RakPeer::Startup())
  199. bool DeserializeCompressed(BitStream *bs, bool readLanguageId=false);
  200. /// Static version of the DeserializeCompressed() function
  201. static bool DeserializeCompressed(char *str, BitStream *bs, bool readLanguageId=false);
  202. static const char *ToString(int64_t i);
  203. static const char *ToString(uint64_t i);
  204. /// \internal
  205. static size_t GetSizeToAllocate(size_t bytes)
  206. {
  207. const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2;
  208. if (bytes<=smallStringSize)
  209. return smallStringSize;
  210. else
  211. return bytes*2;
  212. }
  213. /// \internal
  214. struct SharedString
  215. {
  216. SimpleMutex *refCountMutex;
  217. unsigned int refCount;
  218. size_t bytesUsed;
  219. char *bigString;
  220. char *c_str;
  221. char smallString[128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2];
  222. };
  223. /// \internal
  224. RakString( SharedString *_sharedString );
  225. /// \internal
  226. SharedString *sharedString;
  227. // static SimpleMutex poolMutex;
  228. // static DataStructures::MemoryPool<SharedString> pool;
  229. /// \internal
  230. static SharedString emptyString;
  231. //static SharedString *sharedStringFreeList;
  232. //static unsigned int sharedStringFreeListAllocationCount;
  233. /// \internal
  234. /// List of free objects to reduce memory reallocations
  235. static DataStructures::List<SharedString*> freeList;
  236. static int RakStringComp( RakString const &key, RakString const &data );
  237. static void LockMutex(void);
  238. static void UnlockMutex(void);
  239. protected:
  240. void Allocate(size_t len);
  241. void Assign(const char *str);
  242. void Assign(const char *str, va_list ap);
  243. void Clone(void);
  244. void Free(void);
  245. unsigned char ToLower(unsigned char c);
  246. unsigned char ToUpper(unsigned char c);
  247. void Realloc(SharedString *sharedString, size_t bytes);
  248. };
  249. }
  250. const RakNet::RakString RAK_DLL_EXPORT operator+(const RakNet::RakString &lhs, const RakNet::RakString &rhs);
  251. #endif