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.

92 lines
2.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: strconv.h
  8. //
  9. //--------------------------------------------------------------------------
  10. // This header contains macros and functions for performing various types of
  11. // string conversions, in most cases, these functions are duplicate of others
  12. // found in both MFC, and ATL. Unfortunately, when both MFC and ATL are
  13. // included within the same translation unit, all of the conversion functions
  14. // available in ATL are made unavailable. This file should work in all settings!
  15. // The functions, though, in many cases to functions elsewhere have been renamed
  16. // to avoid name colisions. The naming convention is similar to the naming
  17. // convention used in the C++ standard library. ANSI strings are abbreviated to
  18. // str, while wide character strings are abbreviated to wcs.
  19. #ifndef STRCONV_H
  20. #define STRCONV_H
  21. #ifndef __wtypes_h__
  22. #include <wtypes.h>
  23. #endif
  24. #ifndef _INC_MALLOC
  25. #include <malloc.h>
  26. #endif
  27. namespace microsoft {
  28. namespace string_conversion {
  29. inline BSTR wcs2BSTR(const wchar_t* ws) throw()
  30. // Converts a wide character string to a BSTR. The callee is responsible
  31. // for releasing the returned BSTR. A BSTR is always returned, even if ws
  32. // is NULL. When ws is NULL, an empty BSTR is returned. This is done so
  33. // that the behaviour of this function is similar to those found below.
  34. {
  35. return ws ? SysAllocString(ws) : SysAllocString(L"");
  36. }
  37. inline BSTR str2BSTR(const char* s) throw()
  38. // Converts the provided narrow string to a BSTR. The BSTR must be released
  39. // by the callee.
  40. {
  41. if (!s)
  42. return wcs2BSTR(NULL);
  43. const int len = lstrlenA(s)+1;
  44. wchar_t* ws = reinterpret_cast<wchar_t*>(alloca((len)*2));
  45. *ws=0;
  46. MultiByteToWideChar(CP_ACP, 0, s, -1, ws, len);
  47. return wcs2BSTR(ws);
  48. }
  49. inline char* wcs2str(char* s, const wchar_t* ws) throw()
  50. // Converts the provided wcs to a narrrow string, which will be stored in
  51. // the provided space. No overrun checking is done! The narrow string
  52. // is returned.
  53. {
  54. *s=0;
  55. WideCharToMultiByte(CP_ACP, 0, ws, -1, s, lstrlenW(ws)+1, NULL, NULL);
  56. return s;
  57. }
  58. inline char* wcs2str(char* s, const wchar_t* ws, int wslen) throw()
  59. // Converts the provided wcs to a narrow string, which will be stored
  60. // in the provided space. Overrun checking is performed.
  61. {
  62. *s=0;
  63. WideCharToMultiByte(CP_ACP, 0, ws, -1, s, wslen, NULL, NULL);
  64. return s;
  65. }
  66. #define USES_STRCONV int _strconv_length;
  67. #define WCS2STR(ws) \
  68. (ws ? (_strconv_length = lstrlenW(ws)+1,\
  69. wcs2str(reinterpret_cast<char*>(alloca(_strconv_length)),\
  70. ws, _strconv_length)) : NULL)
  71. } // namespace string_conversion
  72. } // namespace microsoft
  73. #ifndef MICROSOFT_NAMESPACE_ON
  74. using namespace microsoft;
  75. #ifndef STRING_CONVERSION_NAMESPACE_ON
  76. using namespace string_conversion;
  77. #endif // STRING_CONVERSION_NAMESPACE_ON
  78. #endif // MICROSOFT_NAMESPACE_ON
  79. #endif // STRCONV_H