dup_mb_to_wc.c 936 B

12345678910111213141516171819202122232425262728293031
  1. /*
  2. * dup_mb_to_wc: memory-allocating wrapper on mb_to_wc.
  3. *
  4. * Also dup_mb_to_wc_c: same but you already know the length of the
  5. * string, and you get told the length of the returned wide string.
  6. * (But it's still NUL-terminated, for convenience.)
  7. */
  8. #include "putty.h"
  9. #include "misc.h"
  10. wchar_t *dup_mb_to_wc_c(int codepage, const char *string,
  11. size_t inlen, size_t *outlen_p)
  12. {
  13. strbuf *sb = strbuf_new();
  14. put_mb_to_wc(sb, codepage, string, inlen);
  15. if (outlen_p)
  16. *outlen_p = sb->len / sizeof(wchar_t);
  17. /* Append a trailing L'\0'. For this we only need to write one
  18. * byte _fewer_ than sizeof(wchar_t), because strbuf will append a
  19. * byte '\0' for us. */
  20. put_padding(sb, sizeof(wchar_t) - 1, 0);
  21. return (wchar_t *)strbuf_to_str(sb);
  22. }
  23. wchar_t *dup_mb_to_wc(int codepage, const char *string)
  24. {
  25. return dup_mb_to_wc_c(codepage, string, strlen(string), NULL);
  26. }