File: characterEncodingConvertWin32.cc

package info (click to toggle)
libosl 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 273,976 kB
  • sloc: cpp: 129,625; ansic: 7,145; ruby: 1,290; makefile: 558; perl: 413; sh: 35
file content (45 lines) | stat: -rw-r--r-- 1,295 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "osl/misc/characterEncodingConvertWin32.h"
#ifdef _WIN32
#include <windows.h>
#include <cassert>

#define CP_EUCJP 20932
//#define CP_EUCJP 51932 not supported by MultiByteToWideChar
#define CP_SJIS  932

std::string osl::misc::
eucToLang(const std::string& src) {
  const int wlen = MultiByteToWideChar(CP_EUCJP, 0,
                                       src.c_str(), src.size(),
                                       NULL, 0);
  assert(wlen>0);
  wchar_t wbuf[wlen];
  const int wret = MultiByteToWideChar(CP_EUCJP, 0,
                                       src.c_str(), src.size(),
                                       wbuf, wlen);
  if (!wret || wlen != wret) {
    return "";
  }

  const int len = WideCharToMultiByte(CP_SJIS, 0,
                                      wbuf, wret,
                                      NULL, 0,
                                      NULL, NULL);
  assert(len>0);
  char buf[len];
  const int ret = WideCharToMultiByte(CP_SJIS, 0,
                                      wbuf, wret,
                                      buf, len,
                                      NULL, NULL);
  if (!ret || len != ret) {
    return "";
  }

  return std::string(buf, ret);
}

#endif
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: