Check-in [c6a9e4ed41]
Not logged in
Overview

SHA1 Hash:c6a9e4ed41e5132ba53404c373c64f62c9b253fb
Date: 2008-11-04 12:13:09
User: drh
Comment:Convert the changes that support microsoft character sets so that they work (so that they are #ifdef-ed out) on other platforms.
Timelines: ancestors | descendants | both | trunk
Other Links: files | ZIP archive | manifest

Tags And Properties
Changes
[hide diffs]

Modified src/db.c from [ed66011978] to [3dffe034aa].

@@ -36,10 +36,13 @@
 */
 #include "config.h"
 #ifndef __MINGW32__
 #  include <pwd.h>
 #endif
+#ifdef __MINGW32__
+#  include <windows.h>
+#endif
 #include <sqlite3.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include "db.h"
@@ -496,10 +499,86 @@
   }
   db_finalize(&s);
   return z;
 }
 
+#ifdef __MINGW32__
+/*
+** These routines (copied out of the os_win.c driver for SQLite) convert
+** character strings in various microsoft multi-byte character formats
+** into UTF-8.  Fossil and SQLite always use only UTF-8 internally.  These
+** routines are needed in order to convert from the default character set
+** currently in use by windows into UTF-8 when strings are imported from
+** the outside world.
+*/
+/*
+** Convert microsoft unicode to UTF-8.  Space to hold the returned string is
+** obtained from malloc().
+** Copied from sqlite3.c as is (petr)
+*/
+static char *unicodeToUtf8(const WCHAR *zWideFilename){
+  int nByte;
+  char *zFilename;
+
+  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
+  zFilename = malloc( nByte );
+  if( zFilename==0 ){
+    return 0;
+  }
+  nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
+                              0, 0);
+  if( nByte == 0 ){
+    free(zFilename);
+    zFilename = 0;
+  }
+  return zFilename;
+}
+/*
+** Convert an ansi string to microsoft unicode, based on the
+** current codepage settings for file apis.
+**
+** Space to hold the returned string is obtained
+** from malloc.
+*/
+static WCHAR *mbcsToUnicode(const char *zFilename){
+  int nByte;
+  WCHAR *zMbcsFilename;
+  int codepage = CP_ACP;
+
+  nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
+  zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
+  if( zMbcsFilename==0 ){
+    return 0;
+  }
+
+  nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
+  if( nByte==0 ){
+    free(zMbcsFilename);
+    zMbcsFilename = 0;
+  }
+  return zMbcsFilename;
+}
+/*
+** Convert multibyte character string to UTF-8.  Space to hold the
+** returned string is obtained from malloc().
+*/
+static char *mbcsToUtf8(const char *zFilename){
+  char *zFilenameUtf8;
+  WCHAR *zTmpWide;
+
+  zTmpWide = mbcsToUnicode(zFilename);
+  if( zTmpWide==0 ){
+    return 0;
+  }
+
+  zFilenameUtf8 = unicodeToUtf8(zTmpWide);
+  free(zTmpWide);
+  return zFilenameUtf8;
+}
+#endif /* __MINGW32__ */
+
+
 /*
 ** Initialize a new database file with the given schema.  If anything
 ** goes wrong, call db_err() to exit.
 */
 void db_init_database(
@@ -510,10 +589,13 @@
   sqlite3 *db;
   int rc;
   const char *zSql;
   va_list ap;
 
+#ifdef __MINGW32__
+  zFileName = mbcsToUtf8(zFileName);
+#endif
   rc = sqlite3_open(zFileName, &db);
   if( rc!=SQLITE_OK ){
     db_err(sqlite3_errmsg(db));
   }
   sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
@@ -537,10 +619,13 @@
 ** zDbName is the name of a database file.  If no other database
 ** file is open, then open this one.  If another database file is
 ** already open, then attach zDbName using the name zLabel.
 */
 void db_open_or_attach(const char *zDbName, const char *zLabel){
+#ifdef __MINGW32__
+  zDbName = mbcsToUtf8(zDbName);
+#endif
   if( !g.db ){
     int rc = sqlite3_open(zDbName, &g.db);
     if( rc!=SQLITE_OK ){
       db_err(sqlite3_errmsg(g.db));
     }