1370b324cSopenharmony_ci// Windows/ResourceString.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#ifndef _UNICODE
6370b324cSopenharmony_ci#include "../Common/StringConvert.h"
7370b324cSopenharmony_ci#endif
8370b324cSopenharmony_ci
9370b324cSopenharmony_ci#include "ResourceString.h"
10370b324cSopenharmony_ci
11370b324cSopenharmony_ciextern HINSTANCE g_hInstance;
12370b324cSopenharmony_ci#ifndef _UNICODE
13370b324cSopenharmony_ciextern bool g_IsNT;
14370b324cSopenharmony_ci#endif
15370b324cSopenharmony_ci
16370b324cSopenharmony_cinamespace NWindows {
17370b324cSopenharmony_ci
18370b324cSopenharmony_ci#ifndef _UNICODE
19370b324cSopenharmony_ci
20370b324cSopenharmony_cistatic CSysString MyLoadStringA(HINSTANCE hInstance, UINT resourceID)
21370b324cSopenharmony_ci{
22370b324cSopenharmony_ci  CSysString s;
23370b324cSopenharmony_ci  int size = 128;
24370b324cSopenharmony_ci  int len;
25370b324cSopenharmony_ci  do
26370b324cSopenharmony_ci  {
27370b324cSopenharmony_ci    size <<= 1;
28370b324cSopenharmony_ci    len = ::LoadString(hInstance, resourceID, s.GetBuf((unsigned)size - 1), size);
29370b324cSopenharmony_ci  }
30370b324cSopenharmony_ci  while (size - len <= 1);
31370b324cSopenharmony_ci  s.ReleaseBuf_CalcLen((unsigned)len);
32370b324cSopenharmony_ci  return s;
33370b324cSopenharmony_ci}
34370b324cSopenharmony_ci
35370b324cSopenharmony_ci#endif
36370b324cSopenharmony_ci
37370b324cSopenharmony_cistatic const int kStartSize = 256;
38370b324cSopenharmony_ci
39370b324cSopenharmony_cistatic void MyLoadString2(HINSTANCE hInstance, UINT resourceID, UString &s)
40370b324cSopenharmony_ci{
41370b324cSopenharmony_ci  int size = kStartSize;
42370b324cSopenharmony_ci  int len;
43370b324cSopenharmony_ci  do
44370b324cSopenharmony_ci  {
45370b324cSopenharmony_ci    size <<= 1;
46370b324cSopenharmony_ci    len = ::LoadStringW(hInstance, resourceID, s.GetBuf((unsigned)size - 1), size);
47370b324cSopenharmony_ci  }
48370b324cSopenharmony_ci  while (size - len <= 1);
49370b324cSopenharmony_ci  s.ReleaseBuf_CalcLen((unsigned)len);
50370b324cSopenharmony_ci}
51370b324cSopenharmony_ci
52370b324cSopenharmony_ci// NT4 doesn't support LoadStringW(,,, 0) to get pointer to resource string. So we don't use it.
53370b324cSopenharmony_ci
54370b324cSopenharmony_ciUString MyLoadString(UINT resourceID)
55370b324cSopenharmony_ci{
56370b324cSopenharmony_ci  #ifndef _UNICODE
57370b324cSopenharmony_ci  if (!g_IsNT)
58370b324cSopenharmony_ci    return GetUnicodeString(MyLoadStringA(g_hInstance, resourceID));
59370b324cSopenharmony_ci  else
60370b324cSopenharmony_ci  #endif
61370b324cSopenharmony_ci  {
62370b324cSopenharmony_ci    {
63370b324cSopenharmony_ci      wchar_t s[kStartSize];
64370b324cSopenharmony_ci      s[0] = 0;
65370b324cSopenharmony_ci      int len = ::LoadStringW(g_hInstance, resourceID, s, kStartSize);
66370b324cSopenharmony_ci      if (kStartSize - len > 1)
67370b324cSopenharmony_ci        return s;
68370b324cSopenharmony_ci    }
69370b324cSopenharmony_ci    UString dest;
70370b324cSopenharmony_ci    MyLoadString2(g_hInstance, resourceID, dest);
71370b324cSopenharmony_ci    return dest;
72370b324cSopenharmony_ci  }
73370b324cSopenharmony_ci}
74370b324cSopenharmony_ci
75370b324cSopenharmony_civoid MyLoadString(HINSTANCE hInstance, UINT resourceID, UString &dest)
76370b324cSopenharmony_ci{
77370b324cSopenharmony_ci  dest.Empty();
78370b324cSopenharmony_ci  #ifndef _UNICODE
79370b324cSopenharmony_ci  if (!g_IsNT)
80370b324cSopenharmony_ci    MultiByteToUnicodeString2(dest, MyLoadStringA(hInstance, resourceID));
81370b324cSopenharmony_ci  else
82370b324cSopenharmony_ci  #endif
83370b324cSopenharmony_ci  {
84370b324cSopenharmony_ci    {
85370b324cSopenharmony_ci      wchar_t s[kStartSize];
86370b324cSopenharmony_ci      s[0] = 0;
87370b324cSopenharmony_ci      int len = ::LoadStringW(hInstance, resourceID, s, kStartSize);
88370b324cSopenharmony_ci      if (kStartSize - len > 1)
89370b324cSopenharmony_ci      {
90370b324cSopenharmony_ci        dest = s;
91370b324cSopenharmony_ci        return;
92370b324cSopenharmony_ci      }
93370b324cSopenharmony_ci    }
94370b324cSopenharmony_ci    MyLoadString2(hInstance, resourceID, dest);
95370b324cSopenharmony_ci  }
96370b324cSopenharmony_ci}
97370b324cSopenharmony_ci
98370b324cSopenharmony_civoid MyLoadString(UINT resourceID, UString &dest)
99370b324cSopenharmony_ci{
100370b324cSopenharmony_ci  MyLoadString(g_hInstance, resourceID, dest);
101370b324cSopenharmony_ci}
102370b324cSopenharmony_ci
103370b324cSopenharmony_ci}
104