/ Check-in [345860c9]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Revise and enhance the Win32 string conversion routines.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | altShellFix
Files: files | file ages | folders
SHA1: 345860c92195544aad44ea9b0d14c9ebbd50adf2
User & Date: mistachkin 2016-04-03 22:44:16
Context
2016-04-04
02:05
More refactoring and cleanup work on the Win32 string conversion and utility routines. check-in: 02ccb444 user: mistachkin tags: altShellFix
2016-04-03
22:44
Revise and enhance the Win32 string conversion routines. check-in: 345860c9 user: mistachkin tags: altShellFix
20:50
Replace the new fprintf() calls. check-in: f76c3a0c user: mistachkin tags: altShellFix
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/os_win.c.

  1631   1631   #endif /* SQLITE_WIN32_MALLOC */
  1632   1632   
  1633   1633   /*
  1634   1634   ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
  1635   1635   **
  1636   1636   ** Space to hold the returned string is obtained from malloc.
  1637   1637   */
  1638         -static LPWSTR winUtf8ToUnicode(const char *zFilename){
         1638  +static LPWSTR winUtf8ToUnicode(const char *zText){
  1639   1639     int nChar;
  1640         -  LPWSTR zWideFilename;
         1640  +  LPWSTR zWideText;
  1641   1641   
  1642         -  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
         1642  +  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, NULL, 0);
  1643   1643     if( nChar==0 ){
  1644   1644       return 0;
  1645   1645     }
  1646         -  zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
  1647         -  if( zWideFilename==0 ){
         1646  +  zWideText = sqlite3MallocZero( nChar*sizeof(WCHAR) );
         1647  +  if( zWideText==0 ){
  1648   1648       return 0;
  1649   1649     }
  1650         -  nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
         1650  +  nChar = osMultiByteToWideChar(CP_UTF8, 0, zText, -1, zWideText,
  1651   1651                                   nChar);
  1652   1652     if( nChar==0 ){
  1653         -    sqlite3_free(zWideFilename);
  1654         -    zWideFilename = 0;
         1653  +    sqlite3_free(zWideText);
         1654  +    zWideText = 0;
  1655   1655     }
  1656         -  return zWideFilename;
         1656  +  return zWideText;
  1657   1657   }
  1658   1658   
  1659   1659   /*
  1660   1660   ** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
  1661   1661   ** obtained from sqlite3_malloc().
  1662   1662   */
  1663         -static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
         1663  +static char *winUnicodeToUtf8(LPCWSTR zWideText){
  1664   1664     int nByte;
  1665         -  char *zFilename;
         1665  +  char *zText;
  1666   1666   
  1667         -  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
         1667  +  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, 0, 0, 0, 0);
  1668   1668     if( nByte == 0 ){
  1669   1669       return 0;
  1670   1670     }
  1671         -  zFilename = sqlite3MallocZero( nByte );
  1672         -  if( zFilename==0 ){
         1671  +  zText = sqlite3MallocZero( nByte );
         1672  +  if( zText==0 ){
  1673   1673       return 0;
  1674   1674     }
  1675         -  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
         1675  +  nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideText, -1, zText, nByte,
  1676   1676                                   0, 0);
  1677   1677     if( nByte == 0 ){
  1678         -    sqlite3_free(zFilename);
  1679         -    zFilename = 0;
         1678  +    sqlite3_free(zText);
         1679  +    zText = 0;
  1680   1680     }
  1681         -  return zFilename;
         1681  +  return zText;
  1682   1682   }
  1683   1683   
  1684   1684   /*
  1685   1685   ** Convert an ANSI string to Microsoft Unicode, based on the
  1686   1686   ** current codepage settings for file apis.
  1687   1687   **
  1688   1688   ** Space to hold the returned string is obtained
  1689   1689   ** from sqlite3_malloc.
  1690   1690   */
  1691         -static LPWSTR winMbcsToUnicode(const char *zFilename){
         1691  +static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
  1692   1692     int nByte;
  1693   1693     LPWSTR zMbcsFilename;
  1694         -  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
         1694  +  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
  1695   1695   
  1696         -  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
         1696  +  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
  1697   1697                                   0)*sizeof(WCHAR);
  1698   1698     if( nByte==0 ){
  1699   1699       return 0;
  1700   1700     }
  1701   1701     zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
  1702   1702     if( zMbcsFilename==0 ){
  1703   1703       return 0;
  1704   1704     }
  1705         -  nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
         1705  +  nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsFilename,
  1706   1706                                   nByte);
  1707   1707     if( nByte==0 ){
  1708   1708       sqlite3_free(zMbcsFilename);
  1709   1709       zMbcsFilename = 0;
  1710   1710     }
  1711   1711     return zMbcsFilename;
  1712   1712   }
................................................................................
  1714   1714   /*
  1715   1715   ** Convert Microsoft Unicode to multi-byte character string, based on the
  1716   1716   ** user's ANSI codepage.
  1717   1717   **
  1718   1718   ** Space to hold the returned string is obtained from
  1719   1719   ** sqlite3_malloc().
  1720   1720   */
  1721         -static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
         1721  +static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
  1722   1722     int nByte;
  1723         -  char *zFilename;
  1724         -  int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
         1723  +  char *zText;
         1724  +  int codepage = useAnsi ? CP_ACP : CP_OEMCP;
  1725   1725   
  1726         -  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
         1726  +  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, 0, 0, 0, 0);
  1727   1727     if( nByte == 0 ){
  1728   1728       return 0;
  1729   1729     }
  1730         -  zFilename = sqlite3MallocZero( nByte );
  1731         -  if( zFilename==0 ){
         1730  +  zText = sqlite3MallocZero( nByte );
         1731  +  if( zText==0 ){
  1732   1732       return 0;
  1733   1733     }
  1734         -  nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
         1734  +  nByte = osWideCharToMultiByte(codepage, 0, zWideText, -1, zText,
  1735   1735                                   nByte, 0, 0);
  1736   1736     if( nByte == 0 ){
  1737         -    sqlite3_free(zFilename);
  1738         -    zFilename = 0;
         1737  +    sqlite3_free(zText);
         1738  +    zText = 0;
  1739   1739     }
  1740         -  return zFilename;
         1740  +  return zText;
  1741   1741   }
  1742   1742   
  1743   1743   /*
  1744   1744   ** Convert multibyte character string to UTF-8.  Space to hold the
  1745   1745   ** returned string is obtained from sqlite3_malloc().
  1746   1746   */
  1747         -char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
  1748         -  char *zFilenameUtf8;
         1747  +char *sqlite3_win32_mbcs_to_utf8(const char *zText){
         1748  +  char *zTextUtf8;
         1749  +  LPWSTR zTmpWide;
         1750  +
         1751  +  zTmpWide = winMbcsToUnicode(zText, osAreFileApisANSI());
         1752  +  if( zTmpWide==0 ){
         1753  +    return 0;
         1754  +  }
         1755  +  zTextUtf8 = winUnicodeToUtf8(zTmpWide);
         1756  +  sqlite3_free(zTmpWide);
         1757  +  return zTextUtf8;
         1758  +}
         1759  +
         1760  +/*
         1761  +** Convert multibyte character string to UTF-8 using the ANSI codepage.
         1762  +** Space to hold the returned string is obtained from sqlite3_malloc().
         1763  +*/
         1764  +char *sqlite3_win32_mbcs_to_utf8_via_ansi(const char *zText){
         1765  +  char *zTextUtf8;
         1766  +  LPWSTR zTmpWide;
         1767  +
         1768  +  zTmpWide = winMbcsToUnicode(zText, 1);
         1769  +  if( zTmpWide==0 ){
         1770  +    return 0;
         1771  +  }
         1772  +  zTextUtf8 = winUnicodeToUtf8(zTmpWide);
         1773  +  sqlite3_free(zTmpWide);
         1774  +  return zTextUtf8;
         1775  +}
         1776  +
         1777  +/*
         1778  +** Convert multibyte character string to UTF-8 using the OEM codepage.
         1779  +** Space to hold the returned string is obtained from sqlite3_malloc().
         1780  +*/
         1781  +char *sqlite3_win32_mbcs_to_utf8_via_oem(const char *zText){
         1782  +  char *zTextUtf8;
  1749   1783     LPWSTR zTmpWide;
  1750   1784   
  1751         -  zTmpWide = winMbcsToUnicode(zFilename);
         1785  +  zTmpWide = winMbcsToUnicode(zText, 0);
  1752   1786     if( zTmpWide==0 ){
  1753   1787       return 0;
  1754   1788     }
  1755         -  zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
         1789  +  zTextUtf8 = winUnicodeToUtf8(zTmpWide);
  1756   1790     sqlite3_free(zTmpWide);
  1757         -  return zFilenameUtf8;
         1791  +  return zTextUtf8;
  1758   1792   }
  1759   1793   
  1760   1794   /*
  1761   1795   ** Convert UTF-8 to multibyte character string.  Space to hold the
  1762   1796   ** returned string is obtained from sqlite3_malloc().
  1763   1797   */
  1764         -char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
  1765         -  char *zFilenameMbcs;
         1798  +char *sqlite3_win32_utf8_to_mbcs(const char *zText){
         1799  +  char *zTextMbcs;
         1800  +  LPWSTR zTmpWide;
         1801  +
         1802  +  zTmpWide = winUtf8ToUnicode(zText);
         1803  +  if( zTmpWide==0 ){
         1804  +    return 0;
         1805  +  }
         1806  +  zTextMbcs = winUnicodeToMbcs(zTmpWide, osAreFileApisANSI());
         1807  +  sqlite3_free(zTmpWide);
         1808  +  return zTextMbcs;
         1809  +}
         1810  +
         1811  +/*
         1812  +** Convert UTF-8 to multibyte character string using the ANSI codepage.
         1813  +** Space to hold the returned string is obtained from sqlite3_malloc().
         1814  +*/
         1815  +char *sqlite3_win32_utf8_to_mbcs_via_ansi(const char *zText){
         1816  +  char *zTextMbcs;
         1817  +  LPWSTR zTmpWide;
         1818  +
         1819  +  zTmpWide = winUtf8ToUnicode(zText);
         1820  +  if( zTmpWide==0 ){
         1821  +    return 0;
         1822  +  }
         1823  +  zTextMbcs = winUnicodeToMbcs(zTmpWide, 1);
         1824  +  sqlite3_free(zTmpWide);
         1825  +  return zTextMbcs;
         1826  +}
         1827  +
         1828  +/*
         1829  +** Convert UTF-8 to multibyte character string using the OEM codepage.
         1830  +** Space to hold the returned string is obtained from sqlite3_malloc().
         1831  +*/
         1832  +char *sqlite3_win32_utf8_to_mbcs_via_oem(const char *zText){
         1833  +  char *zTextMbcs;
  1766   1834     LPWSTR zTmpWide;
  1767   1835   
  1768         -  zTmpWide = winUtf8ToUnicode(zFilename);
         1836  +  zTmpWide = winUtf8ToUnicode(zText);
  1769   1837     if( zTmpWide==0 ){
  1770   1838       return 0;
  1771   1839     }
  1772         -  zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
         1840  +  zTextMbcs = winUnicodeToMbcs(zTmpWide, 0);
  1773   1841     sqlite3_free(zTmpWide);
  1774         -  return zFilenameMbcs;
         1842  +  return zTextMbcs;
  1775   1843   }
  1776   1844   
  1777   1845   /*
  1778   1846   ** This function sets the data directory or the temporary directory based on
  1779   1847   ** the provided arguments.  The type argument must be 1 in order to set the
  1780   1848   ** data directory or 2 in order to set the temporary directory.  The zValue
  1781   1849   ** argument is the name of the directory to use.  The return value will be