Diff
Not logged in

Differences From:

File src/db.c part of check-in [1d280a85aa] - Take care that the root directory name does not have extra "/" characters. Ticket 9c378ed139. by drh on 2008-10-31 13:33:36. [view]

To:

File src/db.c part of check-in [dad7731f0e] - Fix for ticket 30f7206b2b: Added routines to convert filenames from microsoft character sets into UTF-8. by petr on 2008-11-03 22:50:28. [view]

@@ -37,8 +37,11 @@
 #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>
@@ -497,8 +500,77 @@
   db_finalize(&s);
   return z;
 }
 
+
+/*
+** 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;
+}
+
+
+
 /*
 ** Initialize a new database file with the given schema.  If anything
 ** goes wrong, call db_err() to exit.
 */
@@ -511,9 +583,9 @@
   int rc;
   const char *zSql;
   va_list ap;
 
-  rc = sqlite3_open(zFileName, &db);
+rc = sqlite3_open(mbcsToUtf8(zFileName), &db);
   if( rc!=SQLITE_OK ){
     db_err(sqlite3_errmsg(db));
   }
   sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
@@ -539,15 +611,15 @@
 ** already open, then attach zDbName using the name zLabel.
 */
 void db_open_or_attach(const char *zDbName, const char *zLabel){
   if( !g.db ){
-    int rc = sqlite3_open(zDbName, &g.db);
+    int rc = sqlite3_open(mbcsToUtf8(zDbName), &g.db);
     if( rc!=SQLITE_OK ){
       db_err(sqlite3_errmsg(g.db));
     }
     db_connection_init();
   }else{
-    db_multi_exec("ATTACH DATABASE %Q AS %s", zDbName, zLabel);
+    db_multi_exec("ATTACH DATABASE %Q AS %s", mbcsToUtf8(zDbName), zLabel);
   }
 }
 
 /*