SLikeNet  0.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
string.h
Go to the documentation of this file.
1 /*
2  * Original work: Copyright (c) 2014, Oculus VR, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7  * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8  *
9  *
10  * Modified work: Copyright (c) 2016-2017, SLikeSoft UG (haftungsbeschränkt)
11  *
12  * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13  * license found in the license.txt file in the root directory of this source tree.
14  */
15 
16 #ifndef __RAK_STRING_H
17 #define __RAK_STRING_H
18 
19 #include "Export.h"
20 #include "DS_List.h"
21 #include "types.h" // int64_t
22 #include <stdio.h>
23 #include "stdarg.h"
24 
25 
26 #ifdef _WIN32
27 
28 
29 
30 #include "WindowsIncludes.h"
31 #endif
32 
33 namespace SLNet
34 {
36 class SimpleMutex;
37 class BitStream;
38 
45 {
46 public:
47  // Constructors
48  RakString();
49  RakString(char input);
50  RakString(unsigned char input);
51  RakString(const unsigned char *format, ...);
52  RakString(const char *format, ...);
53  ~RakString();
54  RakString( const RakString & rhs);
55 
57  operator const char* () const {return sharedString->c_str;}
58 
60  const char *C_String(void) const {return sharedString->c_str;}
61 
62  // Lets you modify the string. Do not make the string longer - however, you can make it shorter, or change the contents.
63  // Pointer is only valid in the scope of RakString itself
64  char *C_StringUnsafe(void) {Clone(); return sharedString->c_str;}
65 
67  RakString& operator = ( const RakString& rhs );
68  RakString& operator = ( const char *str );
69  RakString& operator = ( char *str );
70  RakString& operator = ( const unsigned char *str );
71  RakString& operator = ( char unsigned *str );
72  RakString& operator = ( const char c );
73 
75  RakString& operator +=( const RakString& rhs);
76  RakString& operator += ( const char *str );
77  RakString& operator += ( char *str );
78  RakString& operator += ( const unsigned char *str );
79  RakString& operator += ( char unsigned *str );
80  RakString& operator += ( const char c );
81 
83  unsigned char operator[] ( const unsigned int position ) const;
84 
85 #ifdef _WIN32
86  // Return as Wide char
87  // Deallocate with DeallocWideChar
88  WCHAR * ToWideChar(void);
89  void DeallocWideChar(WCHAR * w);
90 
91  void FromWideChar(const wchar_t *source);
92  static SLNet::RakString FromWideChar_S(const wchar_t *source);
93 #endif
94 
101  size_t Find(const char *stringToFind,size_t pos = 0 );
102 
104  bool operator==(const RakString &rhs) const;
105  bool operator==(const char *str) const;
106  bool operator==(char *str) const;
107 
108  // Comparison
109  bool operator < ( const RakString& right ) const;
110  bool operator <= ( const RakString& right ) const;
111  bool operator > ( const RakString& right ) const;
112  bool operator >= ( const RakString& right ) const;
113 
115  bool operator!=(const RakString &rhs) const;
116  bool operator!=(const char *str) const;
117  bool operator!=(char *str) const;
118 
120  const char * ToLower(void);
121 
123  const char * ToUpper(void);
124 
126  void Set(const char *format, ...);
127 
135  RakString Assign(const char *str,size_t pos, size_t n );
136 
138  bool IsEmpty(void) const;
139 
141  size_t GetLength(void) const;
142  size_t GetLengthUTF8(void) const;
143 
145  void Replace(unsigned index, unsigned count, unsigned char c);
146 
148  void SetChar( unsigned index, unsigned char c );
149 
151  void SetChar( unsigned index, SLNet::RakString s );
152 
154  void Truncate(unsigned int length);
155  void TruncateUTF8(unsigned int length);
156 
157  // Gets the substring starting at index for count characters
158  RakString SubStr(unsigned int index, size_t count) const;
159 
161  void Erase(unsigned int index, unsigned int count);
162 
164  void TerminateAtFirstCharacter(char c);
166  void TerminateAtLastCharacter(char c);
167 
168  void StartAfterFirstCharacter(char c);
169  void StartAfterLastCharacter(char c);
170 
172  int GetCharacterCount(char c);
173 
175  void RemoveCharacter(char c);
176 
179  static SLNet::RakString NonVariadic(const char *str);
180 
182  static unsigned long ToInteger(const char *str);
183  static unsigned long ToInteger(const RakString &rs);
184 
189  static int ReadIntFromSubstring(const char *str, size_t pos, size_t n);
190 
191  // Like strncat, but for a fixed length
192  void AppendBytes(const char *bytes, size_t count);
193 
195  int StrCmp(const RakString &rhs) const;
196 
198  int StrNCmp(const RakString &rhs, size_t num) const;
199 
201  int StrICmp(const RakString &rhs) const;
202 
204  void Clear(void);
205 
207  void Printf(void);
208 
210  void FPrintf(FILE *fp);
211 
213  bool IPAddressMatch(const char *IP);
214 
216  bool ContainsNonprintableExceptSpaces(void) const;
217 
219  bool IsEmailAddress(void) const;
220 
222  SLNet::RakString& URLEncode(void);
223 
225  SLNet::RakString& URLDecode(void);
226 
228  void SplitURI(SLNet::RakString &header, SLNet::RakString &domain, SLNet::RakString &path);
229 
231  SLNet::RakString& SQLEscape(void);
232 
238  static SLNet::RakString FormatForPOST(const char* uri, const char* contentType, const char* body, const char* extraHeaders="");
239  static SLNet::RakString FormatForPUT(const char* uri, const char* contentType, const char* body, const char* extraHeaders="");
240 
244  static SLNet::RakString FormatForGET(const char* uri, const char* extraHeaders="");
245 
249  static SLNet::RakString FormatForDELETE(const char* uri, const char* extraHeaders="");
250 
252  SLNet::RakString& MakeFilePath(void);
253 
256  static void FreeMemory(void);
258  static void FreeMemoryNoMutex(void);
259 
262  void Serialize(BitStream *bs) const;
263 
265  static void Serialize(const char *str, BitStream *bs);
266 
272  void SerializeCompressed(BitStream *bs, uint8_t languageId=0, bool writeLanguageId=false) const;
273 
275  static void SerializeCompressed(const char *str, BitStream *bs, uint8_t languageId=0, bool writeLanguageId=false);
276 
280  bool Deserialize(BitStream *bs);
281 
283  static bool Deserialize(char *str, BitStream *bs);
284 
290  bool DeserializeCompressed(BitStream *bs, bool readLanguageId=false);
291 
293  static bool DeserializeCompressed(char *str, BitStream *bs, bool readLanguageId=false);
294 
295  static const char *ToString(int64_t i);
296  static const char *ToString(uint64_t i);
297 
299  static size_t GetSizeToAllocate(size_t bytes)
300  {
301  const size_t smallStringSize = 128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2;
302  if (bytes<=smallStringSize)
303  return smallStringSize;
304  else
305  return bytes*2;
306  }
307 
310  {
312  unsigned int refCount;
313  size_t bytesUsed;
314  char *bigString;
315  char *c_str;
316  char smallString[128-sizeof(unsigned int)-sizeof(size_t)-sizeof(char*)*2];
317  };
318 
320  RakString( SharedString *_sharedString );
321 
324 
325 // static SimpleMutex poolMutex;
326 // static DataStructures::MemoryPool<SharedString> pool;
329 
330  //static SharedString *sharedStringFreeList;
331  //static unsigned int sharedStringFreeListAllocationCount;
335 
336  static int RakStringComp( RakString const &key, RakString const &data );
337 
338  static void LockMutex(void);
339  static void UnlockMutex(void);
340 
341 protected:
342  static SLNet::RakString FormatForPUTOrPost(const char* type, const char* uri, const char* contentType, const char* body, const char* extraHeaders);
343  void Allocate(size_t len);
344  void Assign(const char *str);
345  void Assign(const char *str, va_list ap);
346 
347  void Clone(void);
348  void Free(void);
349  unsigned char ToLower(unsigned char c);
350  unsigned char ToUpper(unsigned char c);
351  void Realloc(SharedString *inSharedString, size_t bytes);
352 };
353 
354 }
355 
357 
358 
359 #endif