Advertisement
Anders

NSIS ANSI UTF8 NLF and LangStringInNSH - Part1

Oct 18th, 2011
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.94 KB | None | 0 0
  1. --- Source/build.cpp    Tue Oct 18 16:30:26 2011
  2. +++ Source/build.cpp    Tue Oct 18 18:25:31 2011
  3. @@ -121,6 +121,9 @@
  4.    multiple_entries_instruction=0;
  5.  
  6.    build_include_depth=0;
  7. +#ifndef _UNICODE
  8. +  build_include_isutf8=false;
  9. +#endif
  10.  
  11.    has_called_write_output=false;
  12.  
  13. --- Source/build.h  Tue Oct 18 16:30:26 2011
  14. +++ Source/build.h  Tue Oct 18 19:18:29 2011
  15. @@ -328,6 +328,9 @@
  16.       * this will return a PS_WARNING.
  17.       */
  18.      int SetLangString(TCHAR *name, LANGID lang, const TCHAR *str, BOOL unicode);
  19. +#ifndef _UNICODE
  20. +    int SetUTF8LangString(TCHAR *name, LANGID lang, const char* stru8);
  21. +#endif
  22.  
  23.      /**
  24.       * Sets the user string to the specific NLF_STRINGS id.
  25. @@ -424,6 +427,9 @@
  26.      TCHAR build_output_filename[1024];
  27.  
  28.      int build_include_depth;
  29. +#ifndef _UNICODE
  30. +    bool build_include_isutf8; // UTF-8 LangString in .nsh hack for ANSI builds
  31. +#endif
  32.  
  33.      // Added by ramon 6 jun 2003
  34.  #ifdef NSIS_SUPPORT_VERSION_INFO
  35. --- Source/lang.cpp Tue Oct 18 16:30:26 2011
  36. +++ Source/lang.cpp Tue Oct 18 23:12:13 2011
  37. @@ -26,6 +26,7 @@
  38.  #include "exehead/resource.h"
  39.  #include <nsis-version.h>
  40.  #include "tstring.h"
  41. +#include "utf.h"
  42.  
  43.  using namespace std;
  44.  
  45. @@ -502,6 +503,21 @@
  46.    return PS_OK;
  47.  }
  48.  
  49. +#ifndef _UNICODE
  50. +int CEXEBuild::SetUTF8LangString(TCHAR *name, LANGID lang, const char* stru8)
  51. +{
  52. +  LanguageTable *table = GetLangTable(lang);
  53. +  if (!table) return PS_ERROR;
  54. +  if (!Platform_SupportsUTF8Conversion()) return PS_ERROR;
  55. +
  56. +  WINTCHAR_T* bufWinTStr = UTF8ToWinTStr(stru8, table->nlf.m_uCodePage);
  57. +  if (!bufWinTStr) return PS_ERROR;
  58. +  const int ret = SetLangString(name, lang, bufWinTStr, sizeof(TCHAR)>1);
  59. +  WinTStrFree(bufWinTStr);
  60. +  return ret;
  61. +}
  62. +#endif
  63. +
  64.  // Sets the user string to the specific NLF_STRINGS id.
  65.  //
  66.  // @return If the id is invalid or the string is not valid, it will return a
  67. @@ -925,6 +941,12 @@
  68.      return 0;
  69.    }
  70.  
  71. +#ifndef _UNICODE
  72. +  // 0 = ansi, 8 = utf-8 (16/17 for uft-16le/be not supported)
  73. +  char fencoding = 0;
  74. +  if (IsUTF8BOM(f)) fencoding = 8;
  75. +#endif
  76. +
  77.    // Check header
  78.    TCHAR buf[NSIS_MAX_STRLEN];
  79.    buf[0] = SkipComments(f);
  80. @@ -1096,8 +1118,33 @@
  81.      buf[0] = SkipComments(f);
  82.  
  83.      _fgetts(buf+1, NSIS_MAX_STRLEN, f);
  84. +
  85. +#ifndef _UNICODE
  86. +    if (8 == fencoding)
  87. +    {
  88. +      if (!Platform_SupportsUTF8Conversion()) {
  89. +        ERROR_MSG(_T("Error: UTF-8 language files not supported on this OS!\n"));
  90. +        return 0;
  91. +      }
  92. +      WINTCHAR_T* bufConv = UTF8ToWinTStr(buf, nlf->m_uCodePage);
  93. +      if (!bufConv) {
  94. +        ERROR_MSG(_T("Error: Invalid UTF-8? (string #%d - \"%s\")\n"), i, NLFStrings[i].szLangStringName);
  95. +        return 0;
  96. +      }
  97. +      else {
  98. +        //NOTE: All this string handling code needs to use WinTStr* and not _t* under UNICODE+POSIX
  99. +        UINT cch = _tcslen(bufConv);
  100. +        _tcsnccpy(buf, bufConv, NSIS_MAX_STRLEN);
  101. +        if (cch >= NSIS_MAX_STRLEN-1) {
  102. +          buf[NSIS_MAX_STRLEN-1] = _T('\0'); // Make sure we fail the "String too long" check
  103. +        }
  104. +      }
  105. +      WinTStrFree(bufConv);
  106. +    }
  107. +#endif
  108. +
  109.      if (_tcslen(buf) == NSIS_MAX_STRLEN-1) {
  110. -      ERROR_MSG(_T("Error: String too long (string #%d - \"%s\")"), i, NLFStrings[i].szLangStringName);
  111. +      ERROR_MSG(_T("Error: String too long (string #%d - \"%s\")\n"), i, NLFStrings[i].szLangStringName);
  112.        return 0;
  113.      }
  114.      temp=_tcslen(buf);
  115. --- Source/SConscript   Tue Oct 18 16:30:26 2011
  116. +++ Source/SConscript   Tue Oct 18 18:59:47 2011
  117. @@ -24,6 +24,7 @@
  118.     strlist.cpp
  119.     tokens.cpp
  120.     tstring.cpp
  121. +   utf.cpp
  122.     util.cpp
  123.     validateunicode.cpp
  124.     winchar.cpp
  125. --- Source/script.cpp   Tue Oct 18 16:30:26 2011
  126. +++ Source/script.cpp   Tue Oct 18 22:39:14 2011
  127. @@ -36,6 +36,7 @@
  128.  #include "tstring.h"
  129.  #include <algorithm>
  130.  #include "boost/scoped_ptr.hpp"
  131. +#include "utf.h"
  132.  
  133.  using namespace std;
  134.  
  135. @@ -787,6 +788,10 @@
  136.      return PS_ERROR;
  137.    }
  138.    build_include_depth++;
  139. +#ifndef _UNICODE
  140. +  const bool org_build_include_isutf8 = build_include_isutf8;
  141. +  build_include_isutf8 = IsUTF8BOM(incfp);
  142. +#endif
  143.  
  144.    int last_linecnt=linecnt;
  145.    linecnt=0;
  146. @@ -811,6 +816,10 @@
  147.    restore_timestamp_predefine(oldtimestamp);
  148.  #endif
  149.  
  150. +#ifndef _UNICODE
  151. +  build_include_isutf8 = org_build_include_isutf8;
  152. +#endif
  153. +
  154.    int errlinecnt=linecnt;
  155.  
  156.    linecnt=last_linecnt;
  157. @@ -1620,13 +1629,21 @@
  158.        TCHAR *name = line.gettoken_str(1);
  159.        LANGID lang = line.gettoken_int(2);
  160.        TCHAR *str = line.gettoken_str(3);
  161. -      int ret = SetLangString(name, lang, str, curfile_unicode);
  162. +      int ret;
  163. +#ifndef _UNICODE
  164. +        if (build_include_isutf8)
  165. +          ret = SetUTF8LangString(name, lang, str);
  166. +        else
  167. +#endif
  168. +          ret = SetLangString(name, lang, str, curfile_unicode);
  169. +      // BUGBUG: TODO: Warn if langstring is > NSIS_MAX_STRLEN ?
  170.        if (ret == PS_WARNING)
  171.          warning_fl(_T("LangString \"%s\" set multiple times for %d, wasting space"), name, lang);
  172.        else if (ret == PS_ERROR) {
  173.          ERROR_MSG(_T("Error: can't set LangString \"%s\"!\n"), name);
  174.          return PS_ERROR;
  175.        }
  176. +      //BUGBUG: Does not display UTF-8 properly.
  177.        SCRIPT_MSG(_T("LangString: \"%s\" %d \"%s\"\n"), name, lang, str);
  178.      }
  179.      return PS_OK;
  180. --- Source/util.cpp Tue Oct 18 16:30:26 2011
  181. +++ Source/util.cpp Tue Oct 18 21:10:08 2011
  182. @@ -209,7 +209,7 @@
  183.  int WideCharToMultiByte(UINT CodePage, DWORD dwFlags, LPCWSTR lpWideCharStr,
  184.      int cchWideChar, LPSTR lpMultiByteStr, int cbMultiByte, LPCSTR lpDefaultChar,
  185.      LPBOOL lpUsedDefaultChar) {
  186. -  static char buffer[4096];
  187. +  static char buffer[4096]; // BUGBUG: Should this be 4*NSIS_MAX_STRLEN for large string build?
  188.  
  189.    char cp[128];
  190.    create_code_page_string(cp, sizeof(cp), CodePage);
  191. @@ -245,7 +245,7 @@
  192.  
  193.  int MultiByteToWideChar(UINT CodePage, DWORD dwFlags, LPCSTR lpMultiByteStr,
  194.      int cbMultiByte, LPWSTR lpWideCharStr, int cchWideChar) {
  195. -  static WCHAR buffer[4096];
  196. +  static WCHAR buffer[4096]; // BUGBUG: Should this be 4*NSIS_MAX_STRLEN for large string build?
  197.  
  198.    char cp[128];
  199.    create_code_page_string(cp, sizeof(cp), CodePage);
  200. @@ -847,3 +847,15 @@
  201.  
  202.    return false;
  203.  }
  204. +
  205. +#ifdef _WIN32
  206. +BOOL Platform_IsWin95()
  207. +{
  208. +  OSVERSIONINFO ovi;
  209. +  ovi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  210. +  GetVersionEx(&ovi);
  211. +  return VER_PLATFORM_WIN32_WINDOWS == ovi.dwPlatformId &&
  212. +    4 == ovi.dwMajorVersion &&
  213. +    ovi.dwMinorVersion < 10;
  214. +}
  215. +#endif
  216. \ No newline at end of file
  217. --- Source/util.h   Tue Oct 18 16:30:26 2011
  218. +++ Source/util.h   Tue Oct 18 21:13:24 2011
  219. @@ -159,4 +159,16 @@
  220.  #  define PATH_CONVERT(x)
  221.  #endif
  222.  
  223. +// Platform detection
  224. +#ifndef _WIN32
  225. +#  define Platform_IsWin95() (FALSE)
  226. +#else
  227. +#  ifndef _UNICODE
  228. +     BOOL Platform_IsWin95();
  229. +#  else
  230. +#    define Platform_IsWin95() (FALSE)
  231. +#  endif
  232. +#endif
  233. +#define Platform_SupportsUTF8Conversion() ( !Platform_IsWin95() ) // TODO: MSLU supports UTF-8 on Win95
  234. +
  235.  #endif //_UTIL_H_
  236.  
  237.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement