Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
6.3 KiB

  1. /**************************************************************************\
  2. *
  3. * Copyright (c) 1999 Microsoft Corporation
  4. *
  5. * Module Name:
  6. *
  7. * Unicode strings
  8. *
  9. * Abstract:
  10. *
  11. * Functions and classes which deal with Unicode strings
  12. *
  13. * Revision History:
  14. *
  15. * 02/22/1999 davidx
  16. * Created it.
  17. * 09/08/1999 agodfrey
  18. * Moved to Runtime\unicode.hpp
  19. *
  20. \**************************************************************************/
  21. #ifndef _UNICODE_HPP
  22. #define _UNICODE_HPP
  23. namespace GpRuntime
  24. {
  25. // These are replacements for some of the C runtime functions.
  26. size_t UnicodeStringLength(const WCHAR *str);
  27. WCHAR * UnicodeStringDuplicate(const WCHAR *strSource);
  28. INT UnicodeStringCompare(const WCHAR *str1, const WCHAR *str2);
  29. extern "C" INT UnicodeStringCompareCI(const WCHAR *str1, const WCHAR *str2);
  30. INT UnicodeStringCompareCount(const WCHAR* str1, const WCHAR* str2, size_t count);
  31. INT UnicodeStringCompareCICount(const WCHAR* str1, const WCHAR* str2, size_t count);
  32. void UnicodeStringCopy(WCHAR *dest, const WCHAR *src);
  33. void UnicodeStringCopyCount(WCHAR *dest, const WCHAR *src, size_t count);
  34. void UnicodeStringToUpper(WCHAR* dest, WCHAR* src);
  35. WCHAR * UnicodeStringConcat(WCHAR *dest, const WCHAR *src);
  36. WCHAR * UnicodeStringReverseSearch(const WCHAR* str, WCHAR ch);
  37. BOOL UnicodeStringIIsEqual(
  38. const WCHAR* str1,
  39. const WCHAR* str2u,
  40. const WCHAR* str2l
  41. );
  42. inline BOOL
  43. UnicodeToAnsiStr(
  44. const WCHAR* unicodeStr,
  45. CHAR* ansiStr,
  46. INT ansiSize
  47. )
  48. {
  49. return WideCharToMultiByte(
  50. CP_ACP,
  51. 0,
  52. unicodeStr,
  53. -1,
  54. ansiStr,
  55. ansiSize,
  56. NULL,
  57. NULL) > 0;
  58. }
  59. class AnsiStrFromUnicode
  60. {
  61. private:
  62. // We now use an ObjectTag to determine if the object is valid
  63. // instead of using a BOOL. This is much more robust and helps
  64. // with debugging. It also enables us to version our objects
  65. // more easily with a version number in the ObjectTag.
  66. ObjectTag Tag; // Keep this as the 1st value in the object!
  67. VOID SetValid(BOOL valid)
  68. {
  69. Tag = valid ? ObjectTagAnsiStrFromUnicode : ObjectTagInvalid;
  70. }
  71. public:
  72. AnsiStrFromUnicode(const WCHAR* unicodeStr)
  73. {
  74. if (unicodeStr == NULL)
  75. {
  76. SetValid(TRUE);
  77. ansiStr = NULL;
  78. }
  79. else
  80. {
  81. SetValid(UnicodeToAnsiStr(unicodeStr, buf, MAX_PATH));
  82. ansiStr = IsValid() ? buf : NULL;
  83. }
  84. }
  85. ~AnsiStrFromUnicode()
  86. {
  87. SetValid(FALSE); // so we don't use a deleted object
  88. }
  89. BOOL IsValid() const
  90. {
  91. ASSERT((Tag == ObjectTagAnsiStrFromUnicode) || (Tag == ObjectTagInvalid));
  92. #if DBG
  93. if (Tag == ObjectTagInvalid)
  94. {
  95. WARNING1("Invalid AnsiStrFromUnicode");
  96. }
  97. #endif
  98. return (Tag == ObjectTagAnsiStrFromUnicode);
  99. }
  100. operator CHAR*()
  101. {
  102. return ansiStr;
  103. }
  104. private:
  105. CHAR* ansiStr;
  106. CHAR buf[MAX_PATH];
  107. };
  108. inline BOOL
  109. AnsiToUnicodeStr(
  110. const CHAR* ansiStr,
  111. WCHAR* unicodeStr,
  112. INT unicodeSize
  113. )
  114. {
  115. return MultiByteToWideChar(
  116. CP_ACP,
  117. 0,
  118. ansiStr,
  119. -1,
  120. unicodeStr,
  121. unicodeSize) > 0;
  122. }
  123. class UnicodeStrFromAnsi
  124. {
  125. private:
  126. // We now use an ObjectTag to determine if the object is valid
  127. // instead of using a BOOL. This is much more robust and helps
  128. // with debugging. It also enables us to version our objects
  129. // more easily with a version number in the ObjectTag.
  130. ObjectTag Tag; // Keep this as the 1st value in the object!
  131. VOID SetValid(BOOL valid)
  132. {
  133. Tag = valid ? ObjectTagUnicodeStrFromAnsi : ObjectTagInvalid;
  134. }
  135. public:
  136. UnicodeStrFromAnsi(const CHAR* ansiStr)
  137. {
  138. if (ansiStr == NULL)
  139. {
  140. SetValid(TRUE);
  141. unicodeStr = NULL;
  142. }
  143. else
  144. {
  145. SetValid(AnsiToUnicodeStr(ansiStr, buf, MAX_PATH));
  146. unicodeStr = IsValid() ? buf : NULL;
  147. }
  148. }
  149. ~UnicodeStrFromAnsi()
  150. {
  151. SetValid(FALSE); // so we don't use a deleted object
  152. }
  153. BOOL IsValid() const
  154. {
  155. ASSERT((Tag == ObjectTagUnicodeStrFromAnsi) || (Tag == ObjectTagInvalid));
  156. #if DBG
  157. if (Tag == ObjectTagInvalid)
  158. {
  159. WARNING1("Invalid UnicodeStrFromAnsi");
  160. }
  161. #endif
  162. return (Tag == ObjectTagUnicodeStrFromAnsi);
  163. }
  164. operator WCHAR*()
  165. {
  166. return unicodeStr;
  167. }
  168. private:
  169. WCHAR* unicodeStr;
  170. WCHAR buf[MAX_PATH];
  171. };
  172. //--------------------------------------------------------------------------
  173. // Represent a simple immutable Unicode string object used internally
  174. // by GDI+ implementation. A string is represented by pieces of
  175. // information:
  176. // - pointer to the character buffer
  177. // - number of characters in the string
  178. //
  179. // [agodfrey] Ack! Yet another string class. I'm just moving it here
  180. // to be with its mates. It came from BaseTypes.hpp.
  181. //--------------------------------------------------------------------------
  182. class GpString
  183. {
  184. public:
  185. // NOTE:
  186. // We're not making a copy of the characters here. Instead,
  187. // we simply remember the character pointer. We assume the
  188. // caller will ensure the input pointer's lifetime is longer
  189. // than that of the newly constructed GpString object.
  190. GpString(const WCHAR* str, UINT len)
  191. {
  192. Buf = str;
  193. Len = len;
  194. }
  195. GpString(const WCHAR* str)
  196. {
  197. Buf = str;
  198. Len = UnicodeStringLength(str);
  199. }
  200. BOOL IsNull() const
  201. {
  202. return Buf == NULL;
  203. }
  204. const WCHAR* GetBuf() const
  205. {
  206. return Buf;
  207. }
  208. UINT GetLen() const
  209. {
  210. return Len;
  211. }
  212. // Return a copy of the string as a NUL-terminated C string.
  213. // Caller should call GpFree on the returned pointer after
  214. // it finishes using the C string.
  215. WCHAR* GetCString() const;
  216. protected:
  217. const WCHAR* Buf;
  218. UINT Len;
  219. };
  220. }
  221. #endif // !_UNICODE_HPP