1 | /**************************************************************************************** |
2 | |
3 | Copyright (C) 2015 Autodesk, Inc. |
4 | All rights reserved. |
5 | |
6 | Use of this software is subject to the terms of the Autodesk license agreement |
7 | provided at the time of installation or download, or which otherwise accompanies |
8 | this software in either electronic or hard copy form. |
9 | |
10 | ****************************************************************************************/ |
11 | |
12 | //! \file fbxstring.h |
13 | #ifndef _FBXSDK_CORE_BASE_STRING_H_ |
14 | #define _FBXSDK_CORE_BASE_STRING_H_ |
15 | |
16 | #include <fbxsdk/fbxsdk_def.h> |
17 | |
18 | #include <fbxsdk/fbxsdk_nsbegin.h> |
19 | |
20 | /** Convert string from UTF8 to wide-char |
21 | * \param pInUTF8 Input string |
22 | * \param pOutWideChar output string |
23 | * \param pOutWideCharSize size of the allocated output string buffer |
24 | * \remark Output buffer should be release by caller */ |
25 | FBXSDK_DLL void FbxUTF8ToWC(const char* pInUTF8, wchar_t*& pOutWideChar, size_t* pOutWideCharSize=NULL); |
26 | |
27 | /** Convert string from wide-char to UTF8 |
28 | * \param pInWideChar input string |
29 | * \param pOutUTF8 output string |
30 | * \param pOutUTF8Size size of the allocated output string buffer |
31 | * \remark Output buffer should be release by caller */ |
32 | FBXSDK_DLL void FbxWCToUTF8(const wchar_t* pInWideChar, char*& pOutUTF8, size_t* pOutUTF8Size=NULL); |
33 | |
34 | #if defined(FBXSDK_ENV_WIN) |
35 | /** Convert string from wide-char to ANSI |
36 | * \param pInWideChar input string |
37 | * \param pOutANSI output string |
38 | * \param pOutANSISize size of the allocated output string buffer |
39 | * \remark Output buffer should be release by caller */ |
40 | FBXSDK_DLL void FbxWCToAnsi(const wchar_t* pInWideChar, char*& pOutANSI, size_t* pOutANSISize=NULL); |
41 | |
42 | /** Convert string from ANSI to wide-char |
43 | * \param pInANSI input string |
44 | * \param pOutWideChar output string |
45 | * \param pOutWideCharSize size of the allocated output string buffer |
46 | * \remark Output buffer should be release by caller */ |
47 | FBXSDK_DLL void FbxAnsiToWC(const char* pInANSI, wchar_t*& pOutWideChar, size_t* pOutWideCharSize=NULL); |
48 | |
49 | /** Convert string from ANSI to UTF8 |
50 | * \param pInANSI input string |
51 | * \param outUTF8 output string |
52 | * \param pOutUTF8Size size of the allocated output string buffer |
53 | * \remark Output buffer should be release by caller */ |
54 | FBXSDK_DLL void FbxAnsiToUTF8(const char* pInANSI, char*& pOutUTF8, size_t* pOutUTF8Size=NULL); |
55 | |
56 | /** Convert string from UTF8 to ANSI |
57 | * \param pInUTF8 input string |
58 | * \param pOutANSI output string |
59 | * \param pOutANSISize size of the allocated output string buffer |
60 | * \remark Output buffer should be release by caller */ |
61 | FBXSDK_DLL void FbxUTF8ToAnsi(const char* pInUTF8, char*& pOutANSI, size_t* pOutANSISize=NULL); |
62 | #endif |
63 | |
64 | /** Utility class to manipulate strings. |
65 | * \nosubgrouping */ |
66 | class FBXSDK_DLL FbxString |
67 | { |
68 | public: |
69 | /** |
70 | * \name Constructors and Destructor |
71 | */ |
72 | //@{ |
73 | //! Default constructor. |
74 | FbxString(); |
75 | |
76 | /** Copy constructor. |
77 | * \param pString The FbxString to be copied. */ |
78 | FbxString(const FbxString& pString); |
79 | |
80 | /** String constructor. |
81 | * \param pString The string used to construct FbxString. */ |
82 | FbxString(const char* pString); |
83 | |
84 | /** Character constructor. |
85 | * \param pChar The character used to construct FbxString. |
86 | * \param pNbRepeat The number of times to repeat the character. Default value is 1 */ |
87 | FbxString(char pChar, size_t pNbRepeat=1); |
88 | |
89 | /** String constructor with maximum length. |
90 | * \param pCharPtr The string used to construct FbxString. |
91 | * \param pLength Maximum length. */ |
92 | FbxString(const char* pCharPtr, size_t pLength); |
93 | |
94 | /** Integer constructor. |
95 | * \param pValue The int value used to construct FbxString. */ |
96 | FbxString(const int pValue); |
97 | |
98 | /** Float constructor. |
99 | * \param pValue The float value used to construct FbxString. */ |
100 | FbxString(const float pValue); |
101 | |
102 | /** Double constructor. |
103 | * \param pValue The double value used to construct FbxString. */ |
104 | FbxString(const double pValue); |
105 | |
106 | //! Destructor. |
107 | ~FbxString(); |
108 | //@} |
109 | |
110 | /** |
111 | * \name Buffer Access and Validation |
112 | */ |
113 | //@{ |
114 | //! Get string length like "C" strlen(). |
115 | size_t GetLen() const; |
116 | |
117 | //! Get string length like "C" strlen(). |
118 | size_t Size() const; |
119 | |
120 | //! Return \c true if string length equal zero. |
121 | bool IsEmpty() const; |
122 | |
123 | //! Discard the content of the string. |
124 | FbxString& Clear(); |
125 | |
126 | /** Access by reference. |
127 | * \param pIndex The index. |
128 | * \return The reference of the char at pIndex. */ |
129 | char& operator[](int pIndex); |
130 | |
131 | /** Access by copy. |
132 | * \param pIndex The index. |
133 | * \return The char at pIndex. */ |
134 | char operator[](int pIndex) const; |
135 | |
136 | //! Non-const buffer access. |
137 | char* Buffer(); |
138 | |
139 | //! Const buffer access. |
140 | const char* Buffer()const; |
141 | //@} |
142 | |
143 | /** |
144 | * \name String Operations |
145 | */ |
146 | //@{ |
147 | /** FbxString assignment operator. |
148 | * \param pString The FbxString to be assigned. */ |
149 | const FbxString& operator=(const FbxString& pString); |
150 | |
151 | /** Character assignment operator. |
152 | * \param pChar The character to be assigned. */ |
153 | const FbxString& operator=(char pChar); |
154 | |
155 | /** String assignment operator. |
156 | * \param pString The string to be assigned. */ |
157 | const FbxString& operator=(const char* pString); |
158 | |
159 | /** Int assignment operator. |
160 | * \param pValue The int value to be assigned. */ |
161 | const FbxString& operator=(int pValue); |
162 | |
163 | /** Float assignment operator. |
164 | * \param pValue The float value to be assigned. */ |
165 | const FbxString& operator=(float pValue); |
166 | |
167 | /** Double assignment operator. |
168 | * \param pValue The double value to be assigned. */ |
169 | const FbxString& operator=(double pValue); |
170 | |
171 | /** FbxString append. |
172 | * \param pString The FbxString to be appended. */ |
173 | const FbxString& operator+=(const FbxString& pString); |
174 | |
175 | /** Character append. |
176 | * \param pChar The character to be appended. */ |
177 | const FbxString& operator+=(char pChar); |
178 | |
179 | /** String append. |
180 | * \param pString The string to be appended. */ |
181 | const FbxString& operator+=(const char* pString); |
182 | |
183 | /** Integer append. |
184 | * \param pValue The int value to be appended. */ |
185 | const FbxString& operator+=(int pValue); |
186 | |
187 | /** Float append. |
188 | * \param pValue The float value to be appended. */ |
189 | const FbxString& operator+=(float pValue); |
190 | |
191 | /** Double append. |
192 | * \param pValue The double value to be appended. */ |
193 | const FbxString& operator+=(double pValue); |
194 | |
195 | /** Equality operator. |
196 | * \param pString The FbxString to be compared. */ |
197 | bool operator== (const FbxString& pString) const; |
198 | |
199 | /** Inequality operator. |
200 | * \param pString The FbxString to be compared. */ |
201 | bool operator!= (const FbxString& pString) const; |
202 | |
203 | /** Inferior to operator. |
204 | * \param pString The FbxString to be compared. */ |
205 | bool operator< (const FbxString& pString) const; |
206 | |
207 | /** Inferior or equal to operator. |
208 | * \param pString The FbxString to be compared. */ |
209 | bool operator<= (const FbxString& pString) const; |
210 | |
211 | /** Superior or equal to operator. |
212 | * \param pString The FbxString to be compared. */ |
213 | bool operator>= (const FbxString& pString) const; |
214 | |
215 | /** Superior to operator. |
216 | * \param pString The FbxString to be compared. */ |
217 | bool operator> (const FbxString& pString) const; |
218 | |
219 | /** Equality operator. |
220 | * \param pString The string to be compared. */ |
221 | bool operator== (const char* pString) const; |
222 | |
223 | /** Inequality operator. |
224 | * \param pString The string to be compared. */ |
225 | bool operator!= (const char* pString) const; |
226 | |
227 | /** Inferior to operator. |
228 | * \param pString The string to be compared. */ |
229 | bool operator< (const char* pString) const; |
230 | |
231 | /** Inferior or equal to operator. |
232 | * \param pString The string to be compared. */ |
233 | bool operator<= (const char* pString) const; |
234 | |
235 | /** Superior or equal to operator. |
236 | * \param pString The string to be compared. */ |
237 | bool operator>= (const char* pString) const; |
238 | |
239 | /** Superior to operator. |
240 | * \param pString The string to be compared. */ |
241 | bool operator> (const char* pString) const; |
242 | |
243 | /** FbxString concatenation. |
244 | * \param pString1 FbxString 1 to be concatenated to FbxString 2. |
245 | * \param pString2 FbxString 2 to be concatenated to FbxString 1 */ |
246 | friend FBXSDK_DLL FbxString operator+(const FbxString& pString1, const FbxString& pString2); |
247 | |
248 | /** Character concatenation. |
249 | * \param pString FbxString to be concatenated to Character. |
250 | * \param pChar Character to be concatenated to FbxString */ |
251 | friend FBXSDK_DLL FbxString operator+(const FbxString& pString, char pChar); |
252 | |
253 | /** Character concatenation. |
254 | * \param pChar Character to be concatenated to FbxString |
255 | * \param pString FbxString to be concatenated to Character. */ |
256 | friend FBXSDK_DLL FbxString operator+(char pChar, const FbxString& pString); |
257 | |
258 | /** String concatenation. |
259 | * \param pString1 FbxString to be concatenated to String. |
260 | * \param pString2 String to be concatenated to FbxString */ |
261 | friend FBXSDK_DLL FbxString operator+(const FbxString& pString1, const char* pString2); |
262 | |
263 | /** String concatenation. |
264 | * \param pString1 String to be concatenated to FbxString |
265 | * \param pString2 FbxString to be concatenated to String. */ |
266 | friend FBXSDK_DLL FbxString operator+(const char* pString1, const FbxString& pString2); |
267 | |
268 | /** Integer concatenation. |
269 | * \param pString FbxString to be concatenated to Integer. |
270 | * \param pValue Integer to be concatenated to FbxString */ |
271 | friend FBXSDK_DLL FbxString operator+(const FbxString& pString, int pValue); |
272 | |
273 | /** Integer concatenation. |
274 | * \param pValue Integer to be concatenated to FbxString |
275 | * \param pString FbxString to be concatenated to Integer. */ |
276 | friend FBXSDK_DLL FbxString operator+(int pValue, const FbxString& pString); |
277 | |
278 | /** Float concatenation. |
279 | * \param pString FbxString to be concatenated to Float. |
280 | * \param pValue Float to be concatenated to FbxString */ |
281 | friend FBXSDK_DLL FbxString operator+(const FbxString& pString, float pValue); |
282 | |
283 | /** Float concatenation. |
284 | * \param pValue Float to be concatenated to FbxString |
285 | * \param pString FbxString to be concatenated to Float. */ |
286 | friend FBXSDK_DLL FbxString operator+( float pValue, const FbxString& pString); |
287 | |
288 | /** Double concatenation. |
289 | * \param pString FbxString to be concatenated to Double. |
290 | * \param pValue Double to be concatenated to FbxString */ |
291 | friend FBXSDK_DLL FbxString operator+(const FbxString& pString, double pValue); |
292 | |
293 | //! Cast operator. |
294 | operator const char*() const; |
295 | |
296 | /** String assignment function with maximum length. |
297 | * \param pString The string to be assigned. |
298 | * \param pLength The maximum length of string to be assigned. */ |
299 | const FbxString& Copy(const char* pString, size_t pLength); |
300 | |
301 | /** Append as "C" strncat(). |
302 | * \param pString The string to be appended. |
303 | * \param pLength The length of chars to be appended. */ |
304 | const FbxString& Append(const char* pString, size_t pLength); |
305 | |
306 | /** Compare as "C" strcmp(). |
307 | * \param pString The string to be compared. */ |
308 | int Compare(const char* pString) const; |
309 | |
310 | /** Compare as "C" stricmp(). |
311 | * \param pString The string to be compared. */ |
312 | int CompareNoCase(const char* pString) const; |
313 | |
314 | /** Swap the contents of two strings. |
315 | * \param pString The FbxString to be swapped. */ |
316 | void Swap(FbxString& pString); |
317 | |
318 | //! Uppercase conversion. |
319 | FbxString Upper() const; |
320 | |
321 | //! Lowercase conversion. |
322 | FbxString Lower() const; |
323 | //@} |
324 | |
325 | /** |
326 | * \name Substring Extraction |
327 | */ |
328 | //@{ |
329 | /** Extract middle string for a given length. |
330 | * \param pFirst The start index of FbxString to be extracted. |
331 | * \param pCount The length of sub-string to be extracted. */ |
332 | FbxString Mid(size_t pFirst, size_t pCount) const; |
333 | |
334 | /** Extract middle string up to the end. |
335 | * \param pFirst The start index of FbxString to be extracted. */ |
336 | FbxString Mid(size_t pFirst) const; |
337 | |
338 | /** Extract left string. |
339 | * \param pCount The length of sub-string to be extracted. */ |
340 | FbxString Left(size_t pCount) const; |
341 | |
342 | /** Extract right string. |
343 | * \param pCount The length of sub-string to be extracted. */ |
344 | FbxString Right(size_t pCount) const; |
345 | //@} |
346 | |
347 | /** |
348 | * \name Padding |
349 | */ |
350 | //@{ |
351 | /** \enum EPaddingType Padding types. |
352 | * - \e eRight |
353 | * - \e eLeft |
354 | * - \e eBoth */ |
355 | enum EPaddingType {eRight, eLeft, eBoth}; |
356 | |
357 | /** Add padding characters. |
358 | * \param pPadding The padding type. |
359 | * \param pLen The length limit of FbxString after padding. |
360 | * \param pCar The character to be padded. */ |
361 | FbxString Pad(EPaddingType pPadding, size_t pLen, char pCar=' ') const; |
362 | |
363 | /** Remove padding characters. |
364 | * \param pPadding The padding type. |
365 | * \param pCar The character to be padded. |
366 | * \remark If pCar == '\0' the function will remove all the characters that are tested by isspace(). */ |
367 | FbxString UnPad(EPaddingType pPadding, char pCar='\0') const; |
368 | //@} |
369 | |
370 | /** |
371 | * \name Search |
372 | */ |
373 | //@{ |
374 | /** Look for a single character match, like "C" strchr(). |
375 | * \param pChar The character to look for. |
376 | * \param pStartPosition Start position to look for. |
377 | * \return Index or -1 if not found. */ |
378 | int Find(char pChar, size_t pStartPosition=0) const; |
379 | |
380 | /** Look for a substring match, like "C" strstr(). |
381 | * \param pStrSub The substring to look for. |
382 | * \param pStartPosition Start position to look for. |
383 | * \return Starting index or -1 if not found. */ |
384 | int Find(const char* pStrSub, size_t pStartPosition=0) const; |
385 | |
386 | /** Look for the last occurrence of character in string, like "C" strrchr(). |
387 | * \param pChar The character to look for. |
388 | * \return Index or -1 if not found. */ |
389 | int ReverseFind(char pChar) const; |
390 | |
391 | /** Look for a single character match, like "C" strpbrk(). |
392 | * \param pStrCharSet The character set. |
393 | * \param pStartPosition The start position. |
394 | * \return Index or -1 if not found. */ |
395 | int FindOneOf(const char* pStrCharSet, size_t pStartPosition=0) const; |
396 | |
397 | /** Replace a substring. |
398 | * \param pFind The substring to look for. |
399 | * \param pReplaceBy The string to replace by. |
400 | * \param pStartPosition The start position. |
401 | * \return \c true if substring found and replaced. */ |
402 | bool FindAndReplace(const char* pFind, const char* pReplaceBy, size_t pStartPosition=0); |
403 | |
404 | /** Replace all occurrence of a substring. |
405 | * \param pFind The substring to look for. |
406 | * \param pReplaceBy The string to replace by. |
407 | * \return \c true if something got replaced. */ |
408 | bool ReplaceAll(const char* pFind, const char* pReplaceBy); |
409 | |
410 | /** Replace all occurrence of character to find by replacement character. |
411 | * \param pFind The character to look for. |
412 | * \param pReplaceBy The character to replace by. |
413 | * \return \c true if character found and replaced. */ |
414 | bool ReplaceAll(char pFind, char pReplaceBy); |
415 | //@} |
416 | |
417 | /** |
418 | * \name Token Extraction |
419 | */ |
420 | //@{ |
421 | /** Get number of tokens. |
422 | * \param pSpans The span |
423 | * \return The number of tokens. */ |
424 | int GetTokenCount(const char* pSpans) const; |
425 | |
426 | /** Get token at given index. |
427 | * \param pTokenIndex The token index. |
428 | * \param pSpans The span */ |
429 | FbxString GetToken(int pTokenIndex, const char* pSpans) const; |
430 | //@} |
431 | |
432 | private: |
433 | // Lengths/sizes in characters. |
434 | // Note: an extra character is always allocated. |
435 | char* mData; // Actual string (zero terminated). |
436 | |
437 | FbxString(size_t pSrc1Len, const char* pSrc1Data, size_t pSrc2Len, const char* pSrc2Data); // Previously ConcatCopy |
438 | void Init(); |
439 | |
440 | //! Invalidate string. |
441 | void Invalidate(); |
442 | |
443 | void FreeBuffer(); |
444 | void FreeBuffer(char *&pOldData); |
445 | |
446 | bool AllocCopy(FbxString& pDest, size_t pCopyLen, size_t pCopyIndex) const; |
447 | bool AllocBuffer(size_t pLen); |
448 | bool AllocBuffer(size_t pLen, char*& pOldData); |
449 | |
450 | bool AssignCopy(size_t pSrcLen, const char* pSrcData); |
451 | bool ConcatInPlace(size_t pSrcLen, const char* pSrcData); |
452 | |
453 | bool IsIn(char pChar, const char* pString) const; |
454 | bool InternalFindAndReplace(const char* pFind, const char* pReplaceBy, size_t& pStartPosition); |
455 | }; |
456 | |
457 | FBXSDK_INCOMPATIBLE_WITH_ARRAY(FbxString); |
458 | |
459 | //! FbxString concatenation. |
460 | FBXSDK_DLL FbxString operator+(const FbxString& pString1, const FbxString& pString2); |
461 | |
462 | //! Character concatenation. |
463 | FBXSDK_DLL FbxString operator+(const FbxString& pString, char pChar); |
464 | |
465 | //! String concatenation. |
466 | FBXSDK_DLL FbxString operator+(const FbxString& pString1, const char* pString2); |
467 | |
468 | //! Integer concatenation. |
469 | FBXSDK_DLL FbxString operator+(const FbxString& pString, int pValue); |
470 | |
471 | //! Float concatenation. |
472 | FBXSDK_DLL FbxString operator+(const FbxString& pString, float pValue); |
473 | |
474 | //! Double concatenation. |
475 | FBXSDK_DLL FbxString operator+(const FbxString& pString, double pValue); |
476 | |
477 | //! Functor to compare FbxString |
478 | struct FbxStringCompare { inline int operator()(const FbxString& pKeyA, const FbxString& pKeyB) const { return pKeyA.Compare(pKeyB); } }; |
479 | |
480 | //! Functor to compare FbxString without case sensitivity |
481 | struct FbxStringCompareNoCase { inline int operator()(const FbxString& pKeyA, const FbxString& pKeyB) const { return pKeyA.CompareNoCase(pKeyB); } }; |
482 | |
483 | //! Functor to compare "C" strings |
484 | struct FbxCharPtrCompare { inline int operator()(const char* pKeyA, const char* pKeyB) const { return strcmp(pKeyA, pKeyB); } }; |
485 | |
486 | //! Functor to compare "C" strings without case sensitivity |
487 | struct FbxCharPtrCompareNoCase { inline int operator()(const char* pKeyA, const char* pKeyB) const { return FBXSDK_stricmp(pKeyA, pKeyB); } }; |
488 | |
489 | /** Remove the given char in the given string. |
490 | * \param pString The given string. |
491 | * \param pToRemove The given char that ought to be removed. |
492 | * \remarks Strings used in this function are case-sensitive. */ |
493 | inline void FbxRemoveChar(FbxString& pString, char pToRemove) |
494 | { |
495 | int lPos = pString.ReverseFind(pToRemove); |
496 | while( lPos >= 0 ) |
497 | { |
498 | pString = pString.Left(lPos) + pString.Mid(lPos + 1); |
499 | lPos = pString.ReverseFind(pToRemove); |
500 | } |
501 | } |
502 | |
503 | #include <fbxsdk/fbxsdk_nsend.h> |
504 | |
505 | #endif /* _FBXSDK_CORE_BASE_STRING_H_ */ |
506 | |