unicode_win32.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/unicode.h>
  20. #include <pj/assert.h>
  21. #include <pj/string.h>
  22. #include <windows.h>
  23. PJ_DEF(wchar_t*) pj_ansi_to_unicode(const char *s, int len,
  24. wchar_t *buf, int buf_count)
  25. {
  26. PJ_ASSERT_RETURN(s && buf, NULL);
  27. len = MultiByteToWideChar(CP_ACP, 0, s, len,
  28. buf, buf_count);
  29. if (buf_count) {
  30. if (len < buf_count)
  31. buf[len] = 0;
  32. else
  33. buf[len-1] = 0;
  34. }
  35. return buf;
  36. }
  37. PJ_DEF(char*) pj_unicode_to_ansi( const wchar_t *wstr, pj_ssize_t len,
  38. char *buf, int buf_size)
  39. {
  40. PJ_ASSERT_RETURN(wstr && buf, NULL);
  41. len = WideCharToMultiByte(CP_ACP, 0, wstr, (int)len, buf, buf_size,
  42. NULL, NULL);
  43. if (buf_size) {
  44. if (len < buf_size)
  45. buf[len] = '\0';
  46. else
  47. buf[len-1] = '\0';
  48. }
  49. return buf;
  50. }