Check-in [dad7731f0e]
Not logged in
Overview

SHA1 Hash:dad7731f0ec79596fd90b986c0fe2e647c4b909a
Date: 2008-11-03 22:50:28
User: petr
Edited Comment:Fix for ticket 30f7206b2b: Added routines to convert filenames from microsoft character sets into UTF-8.
Original Comment:Fixed ticket30f7206b2b:sqlite3 functions requires UTF-8 encoded path
Timelines: ancestors | descendants | both | trunk
Other Links: files | ZIP archive | manifest

Tags And Properties
Changes
[hide diffs]

Modified src/db.c from [ed66011978] to [bb2296ea5e].

@@ -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,79 @@
   }
   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.
 */
 void db_init_database(
@@ -510,11 +582,11 @@
   sqlite3 *db;
   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);
   rc = sqlite3_exec(db, zSchema, 0, 0, 0);
@@ -538,17 +610,17 @@
 ** 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){
   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);
   }
 }
 
 /*
 ** Open the user database in "~/.fossil".  Create the database anew if