Source code of Windows XP (NT5)
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.

227 lines
3.5 KiB

  1. /*++
  2. Copyright(c) 1995 Microsoft Corporation
  3. MODULE NAME
  4. tstr.c
  5. ABSTRACT
  6. String conversion routines
  7. AUTHOR
  8. Anthony Discolo (adiscolo) 19-Dec-1996
  9. REVISION HISTORY
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <tapi.h>
  15. CHAR *
  16. strdupA(
  17. IN LPCSTR psz
  18. )
  19. {
  20. INT cb = strlen(psz);
  21. CHAR *pszNew = NULL;
  22. if (cb) {
  23. pszNew = malloc(cb + 1);
  24. if (pszNew == NULL) {
  25. DbgPrint("strdupA: LocalAlloc failed\n");
  26. return NULL;
  27. }
  28. strcpy(pszNew, psz);
  29. }
  30. return pszNew;
  31. }
  32. WCHAR *
  33. strdupW(
  34. IN LPCWSTR psz
  35. )
  36. {
  37. INT cb = wcslen(psz);
  38. WCHAR *pszNew = NULL;
  39. if (cb) {
  40. pszNew = malloc((cb + 1) * sizeof (WCHAR));
  41. if (pszNew == NULL) {
  42. DbgPrint("strdupW: LocalAlloc failed\n");
  43. return NULL;
  44. }
  45. wcscpy(pszNew, psz);
  46. }
  47. return pszNew;
  48. }
  49. CHAR *
  50. StrdupWtoA(
  51. IN LPCWSTR psz,
  52. IN DWORD dwCp
  53. )
  54. {
  55. CHAR* pszNew = NULL;
  56. if (psz != NULL) {
  57. DWORD cb;
  58. cb = WideCharToMultiByte(dwCp, 0, psz, -1, NULL, 0, NULL, NULL);
  59. ASSERT(cb);
  60. pszNew = (CHAR*)malloc(cb);
  61. if (pszNew == NULL) {
  62. return NULL;
  63. }
  64. cb = WideCharToMultiByte(dwCp, 0, psz, -1, pszNew, cb, NULL, NULL);
  65. if (!cb) {
  66. free(pszNew);
  67. return NULL;
  68. }
  69. }
  70. return pszNew;
  71. }
  72. WCHAR *
  73. StrdupAtoW(
  74. IN LPCSTR psz,
  75. IN DWORD dwCp
  76. )
  77. {
  78. WCHAR* pszNew = NULL;
  79. if (psz != NULL) {
  80. DWORD cb;
  81. cb = MultiByteToWideChar(dwCp, 0, psz, -1, NULL, 0);
  82. ASSERT(cb);
  83. pszNew = malloc(cb * sizeof(WCHAR));
  84. if (pszNew == NULL) {
  85. return NULL;
  86. }
  87. cb = MultiByteToWideChar(dwCp, 0, psz, -1, pszNew, cb);
  88. if (!cb) {
  89. free(pszNew);
  90. return NULL;
  91. }
  92. }
  93. return pszNew;
  94. }
  95. VOID
  96. StrcpyWtoA(
  97. OUT CHAR *pszDst,
  98. IN LPCWSTR pszSrc,
  99. IN DWORD dwCp
  100. )
  101. {
  102. *pszDst = '\0';
  103. if (pszSrc != NULL) {
  104. DWORD cb;
  105. cb = WideCharToMultiByte(dwCp, 0, pszSrc, -1, NULL, 0, NULL, NULL);
  106. ASSERT(cb);
  107. cb = WideCharToMultiByte(dwCp, 0, pszSrc, -1, pszDst, cb, NULL, NULL);
  108. }
  109. }
  110. VOID
  111. StrcpyAtoW(
  112. OUT WCHAR *pszDst,
  113. IN LPCSTR pszSrc,
  114. IN DWORD dwCp
  115. )
  116. {
  117. *pszDst = L'\0';
  118. if (pszSrc != NULL) {
  119. DWORD cb;
  120. cb = MultiByteToWideChar(dwCp, 0, pszSrc, -1, NULL, 0);
  121. ASSERT(cb);
  122. cb = MultiByteToWideChar(dwCp, 0, pszSrc, -1, pszDst, cb);
  123. }
  124. }
  125. VOID
  126. StrncpyWtoA(
  127. OUT CHAR *pszDst,
  128. IN LPCWSTR pszSrc,
  129. INT cb,
  130. DWORD dwCp
  131. )
  132. {
  133. *pszDst = '\0';
  134. if (pszSrc != NULL) {
  135. cb = WideCharToMultiByte(dwCp, 0, pszSrc, -1, pszDst, cb, NULL, NULL);
  136. }
  137. }
  138. VOID
  139. StrncpyAtoW(
  140. OUT WCHAR *pszDst,
  141. IN LPCSTR pszSrc,
  142. INT cb,
  143. DWORD dwCp
  144. )
  145. {
  146. *pszDst = L'\0';
  147. if (pszSrc != NULL) {
  148. cb = MultiByteToWideChar(dwCp, 0, pszSrc, -1, pszDst, cb);
  149. }
  150. }
  151. size_t
  152. wcslenU(
  153. IN const WCHAR UNALIGNED *pszU
  154. )
  155. {
  156. size_t len = 0;
  157. if (pszU == NULL)
  158. return 0;
  159. while (*pszU != L'\0') {
  160. pszU++;
  161. len++;
  162. }
  163. return len;
  164. }
  165. WCHAR *
  166. strdupWU(
  167. IN const WCHAR UNALIGNED *pszU
  168. )
  169. {
  170. WCHAR *psz;
  171. DWORD dwcb;
  172. if (pszU == NULL)
  173. return NULL;
  174. dwcb = (wcslenU(pszU) + 1) * sizeof (WCHAR);
  175. psz = malloc(dwcb);
  176. if (psz == NULL)
  177. return NULL;
  178. RtlCopyMemory(psz, pszU, dwcb);
  179. return psz;
  180. }