CTString.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #ifndef SE_INCL_CTSTRING_H
  13. #define SE_INCL_CTSTRING_H
  14. #ifdef PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #include <Engine/Base/Types.h>
  18. /*
  19. * Main string class
  20. */
  21. class ENGINE_API CTString {
  22. public:
  23. char *str_String; // pointer to memory holding the character string
  24. public:
  25. /* Default constructor. */
  26. inline CTString(void);
  27. /* Copy constructor. */
  28. inline CTString(const CTString &strOriginal);
  29. /* Constructor from character string. */
  30. inline CTString(const char *pString);
  31. /* Constructor with formatting. */
  32. inline CTString(INDEX iDummy, const char *strFormat, ...);
  33. /* Destructor. */
  34. inline ~CTString();
  35. /* Clear the object. */
  36. inline void Clear(void);
  37. /* Conversion into character string. */
  38. inline operator const char*() const;
  39. /* Assignment. */
  40. inline CTString &operator=(const char *strCharString);
  41. inline CTString &operator=(const CTString &strOther);
  42. /* Check if string data is valid. */
  43. BOOL IsValid(void) const;
  44. // return length of the string
  45. inline INDEX Length(void) const { return strlen(str_String); };
  46. INDEX LengthNaked(void) const;
  47. // strip decorations from the string
  48. CTString Undecorated(void) const;
  49. /* Find index of a substring in a string (returns -1 if not found). */
  50. INDEX FindSubstr(const CTString &strSub);
  51. /* Replace a substring in a string. */
  52. BOOL ReplaceSubstr(const CTString &strSub, const CTString &strNewSub);
  53. /* Check if has given prefix */
  54. BOOL HasPrefix( const CTString &strPrefix) const;
  55. /* Remove given prefix string from this string */
  56. BOOL RemovePrefix( const CTString &strPrefix);
  57. /* Trim the string to contain at most given number of characters. */
  58. INDEX TrimLeft( INDEX ctCharacters);
  59. INDEX TrimRight( INDEX ctCharacters);
  60. /* Trim the string from spaces. */
  61. INDEX TrimSpacesLeft(void);
  62. INDEX TrimSpacesRight(void);
  63. /* Calcuate hashing value for the string. */
  64. ULONG GetHash(void) const;
  65. // retain only first line of the string
  66. void OnlyFirstLine(void);
  67. /* Equality comparison. */
  68. BOOL operator==(const CTString &strOther) const;
  69. BOOL operator==(const char *strOther) const;
  70. ENGINE_API friend BOOL operator==(const char *strThis, const CTString &strOther);
  71. /* Inequality comparison. */
  72. BOOL operator!=(const CTString &strOther) const;
  73. BOOL operator!=(const char *strOther) const;
  74. ENGINE_API friend BOOL operator!=(const char *strThis, const CTString &strOther);
  75. // wild card comparison (other string may contain wildcards)
  76. BOOL Matches(const CTString &strOther) const;
  77. BOOL Matches(const char *strOther) const;
  78. /* String concatenation. */
  79. CTString operator+(const CTString &strSecond) const;
  80. CTString &operator+=(const CTString &strSecond);
  81. ENGINE_API friend CTString operator+(const char *strFirst, const CTString &strSecond);
  82. // split string in two strings at specified position (char AT splitting position goes to str2)
  83. void Split( INDEX iPos, CTString &str1, CTString &str2);
  84. void InsertChar( INDEX iPos, char cChr); // insert char at position
  85. void DeleteChar( INDEX iPos); // delete char at position
  86. /* Throw exception */
  87. void Throw_t(void);
  88. /* Read from stream. */
  89. ENGINE_API friend CTStream &operator>>(CTStream &strmStream, CTString &strString);
  90. void ReadFromText_t(CTStream &strmStream, const CTString &strKeyword); // throw char *
  91. /* Write to stream. */
  92. ENGINE_API friend CTStream &operator<<(CTStream &strmStream, const CTString &strString);
  93. /* Load an entire text file into a string. */
  94. void Load_t(const class CTFileName &fnmFile); // throw char *
  95. void LoadKeepCRLF_t(const class CTFileName &fnmFile); // throw char *
  96. void ReadUntilEOF_t(CTStream &strmStream); // throw char *
  97. /* Save an entire string into a text file. */
  98. void Save_t(const class CTFileName &fnmFile); // throw char *
  99. void SaveKeepCRLF_t(const class CTFileName &fnmFile); // throw char *
  100. // Print formatted to a string
  101. INDEX PrintF(const char *strFormat, ...);
  102. INDEX VPrintF(const char *strFormat, va_list arg);
  103. // Scan formatted from a string
  104. INDEX ScanF(const char *strFormat, ...);
  105. // variable management functions
  106. void LoadVar(const CTFileName &fnmFile);
  107. void SaveVar(const CTFileName &fnmFile);
  108. };
  109. // general variable functions
  110. ENGINE_API void LoadStringVar( const CTFileName &fnmVar, CTString &strVar);
  111. ENGINE_API void SaveStringVar( const CTFileName &fnmVar, CTString &strVar);
  112. ENGINE_API void LoadIntVar( const CTFileName &fnmVar, INDEX &iVar);
  113. ENGINE_API void SaveIntVar( const CTFileName &fnmVar, INDEX &iVar);
  114. ENGINE_API CTString RemoveSpecialCodes( const CTString &str);
  115. #include <Engine/Base/CTString.inl>
  116. #endif /* include-once check. */