Re: [AD] Allegro unable to deal with non ascii filenames

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


On Sun, 2006-05-28 at 07:23 -0700, Chris wrote:
> 
> All it should take is to read the LC_* vars or LANG, figure out the encoding 
> (UTF-8, or some codepage), and tell Allegro to use that for filename 
> conversions. Since Allegro uses UTF-8 internally by default, on UTF-8 
> systems, it'll not need any conversion. But on systems that have a codepage 
> set, it'll properly convert from that codepage (assuming Allegro has it) to 
> UTF-8 and it'll display the proper characters (assuming also Allegro has a 
> proper font). It'll also convert from UTF-8 to the proper codepage before 
> sending it to the system.
> 

Patch attached. This patch is extends the last one with a function to
auto-detect the encoding. With this, Grzegorz's testcase works for me. I
couldn't figure out how U_ASCII_CP would be any useful here - so all
this patch does is, either use UTF8, or do no conversion at all.

set/get_file_encoding stay public - that way users could define their
own codepage and make Allegro convert files to it, if they do know what
libc uses.

-- 
Elias Pschernig
Index: src/linux/lsystem.c
===================================================================
--- src/linux/lsystem.c	(revision 5814)
+++ src/linux/lsystem.c	(working copy)
@@ -164,6 +164,8 @@
 	_unix_read_os_type();
 	if (os_type != OSTYPE_LINUX) return -1; /* FWIW */
 
+   _unix_guess_file_encoding();
+
 	/* This is the only bit that needs root privileges.  First
 	 * we attempt to set our euid to 0, in case this is the
 	 * second time we've been called. */
Index: src/unix/ufile.c
===================================================================
--- src/unix/ufile.c	(revision 5814)
+++ src/unix/ufile.c	(working copy)
@@ -57,7 +57,9 @@
    #endif
 #endif
 
+#define PREFIX_I "al-unix INFO: "
 
+
 /* _al_file_isok:
  *  Helper function to check if it is safe to access a file on a floppy
  *  drive.
@@ -500,3 +502,24 @@
    ASSERT(ff_data);
    return ff_data->size;
 }
+
+
+
+void _unix_guess_file_encoding(void)
+{
+   char const *encoding = "unknown";
+   char *locale = getenv("LC_ALL");
+   if (ustrstr(locale, "utf8") ||
+      ustrstr(locale, "UTF-8") ||
+      ustrstr(locale, "utf-8") ||
+      ustrstr(locale, "UTF8")) {
+      set_file_encoding(U_UTF8);
+      encoding = "UTF8";
+   }
+   else {
+      set_file_encoding(U_CURRENT);
+   }
+
+   TRACE(PREFIX_I "Assumed libc encoding is %s.\n", encoding);
+
+}
Index: src/qnx/qsystem.c
===================================================================
--- src/qnx/qsystem.c	(revision 5814)
+++ src/qnx/qsystem.c	(working copy)
@@ -337,7 +337,9 @@
    struct sched_param sparam;
    int spolicy;
    char tmp[WINDOW_TITLE_SIZE];
-   
+
+   _unix_guess_file_encoding();
+
    /* install emergency-exit signal handlers */
    old_sig_abrt = signal(SIGABRT, qnx_signal_handler);
    old_sig_fpe  = signal(SIGFPE,  qnx_signal_handler);
Index: src/macosx/system.m
===================================================================
--- src/macosx/system.m	(revision 5814)
+++ src/macosx/system.m	(working copy)
@@ -410,7 +410,9 @@
    if (!osx_bootstrap_ok()) {
       return -1;
    }
-   
+
+   _unix_guess_file_encoding();
+
    /* Install emergency-exit signal handlers */
    old_sig_abrt = signal(SIGABRT, osx_signal_handler);
    old_sig_fpe  = signal(SIGFPE,  osx_signal_handler);
Index: src/file.c
===================================================================
--- src/file.c	(revision 5814)
+++ src/file.c	(working copy)
@@ -74,6 +74,7 @@
 
 static PACKFILE *pack_fopen_special_file(AL_CONST char *filename, AL_CONST char *mode);
 
+static int file_encoding = U_UTF8;
 
 
 #define FA_DAT_FLAGS  (FA_RDONLY | FA_ARCH)
@@ -671,6 +672,26 @@
  ***************************************************/
 
 
+/* set_file_encoding:
+ *  Sets the encoding to use for filenames. By default, UTF8 is assumed.
+ */
+void set_file_encoding(int encoding)
+{
+    file_encoding = encoding;
+}
+
+
+
+/* get_file_encoding:
+ *  Returns the encoding currently assumed for filenames.
+ */
+int get_file_encoding(void)
+{
+    return file_encoding ;
+}
+
+
+
 /* file_exists:
  *  Checks whether a file matching the given name and attributes exists,
  *  returning non zero if it does. The file attribute may contain any of
@@ -788,7 +809,7 @@
    if (!_al_file_isok(filename))
       return -1;
 
-   if (unlink(uconvert_toascii(filename, tmp)) != 0) {
+   if (unlink(uconvert_tofilename(filename, tmp)) != 0) {
       *allegro_errno = errno;
       return -1;
    }
@@ -1197,7 +1218,7 @@
 
    /* try any extra environment variable that the parameters say to use */
    if (envvar) {
-      s = getenv(uconvert_toascii(envvar, tmp));
+      s = getenv(uconvert_tofilename(envvar, tmp));
 
       if (s) {
 	 do_uconvert(s, U_ASCII, path, U_CURRENT, sizeof(path)-ucwidth(OTHER_PATH_SEPARATOR));
@@ -1792,14 +1813,14 @@
 
 #ifndef ALLEGRO_MPW
    if (strpbrk(mode, "wW"))  /* write mode? */
-      fd = open(uconvert_toascii(filename, tmp), O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, OPEN_PERMS);
+      fd = open(uconvert_tofilename(filename, tmp), O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, OPEN_PERMS);
    else
-      fd = open(uconvert_toascii(filename, tmp), O_RDONLY | O_BINARY, OPEN_PERMS);
+      fd = open(uconvert_tofilename(filename, tmp), O_RDONLY | O_BINARY, OPEN_PERMS);
 #else
    if (strpbrk(mode, "wW"))  /* write mode? */
-      fd = _al_open(uconvert_toascii(filename, tmp), O_WRONLY | O_BINARY | O_CREAT | O_TRUNC);
+      fd = _al_open(uconvert_tofilename(filename, tmp), O_WRONLY | O_BINARY | O_CREAT | O_TRUNC);
    else
-      fd = _al_open(uconvert_toascii(filename, tmp), O_RDONLY | O_BINARY);
+      fd = _al_open(uconvert_tofilename(filename, tmp), O_RDONLY | O_BINARY);
 #endif
 
    if (fd < 0) {
Index: src/x/xsystem.c
===================================================================
--- src/x/xsystem.c	(revision 5814)
+++ src/x/xsystem.c	(working copy)
@@ -167,6 +167,8 @@
 
    _unix_read_os_type();
 
+   _unix_guess_file_encoding();
+
    /* install emergency-exit signal handlers */
    old_sig_abrt = signal(SIGABRT, _xwin_signal_handler);
    old_sig_fpe  = signal(SIGFPE,  _xwin_signal_handler);
Index: include/allegro/platform/aintunix.h
===================================================================
--- include/allegro/platform/aintunix.h	(revision 5814)
+++ include/allegro/platform/aintunix.h	(working copy)
@@ -75,6 +75,9 @@
    AL_FUNC(void, _unix_register_midi_driver, (int id, MIDI_DRIVER *driver, int autodetect, int priority));
 
 
+   /* File system helpers */
+   AL_FUNC(void, _unix_guess_file_encoding, (void));
+
 #ifdef ALLEGRO_WITH_XWINDOWS
    AL_FUNCPTR(void, _xwin_keyboard_interrupt, (int pressed, int code));
    AL_FUNCPTR(void, _xwin_keyboard_focused, (int focused, int state));
Index: include/allegro/file.h
===================================================================
--- include/allegro/file.h	(revision 5814)
+++ include/allegro/file.h	(working copy)
@@ -138,6 +138,11 @@
 };
 
 
+#define uconvert_tofilename(s, buf)      uconvert(s, U_CURRENT, buf, get_file_encoding(), sizeof(buf))
+
+AL_FUNC(void, set_file_encoding, (int encoding));
+AL_FUNC(int, get_file_encoding, (void));
+
 AL_FUNC(void, packfile_password, (AL_CONST char *password));
 AL_FUNC(PACKFILE *, pack_fopen, (AL_CONST char *filename, AL_CONST char *mode));
 AL_FUNC(PACKFILE *, pack_fopen_vtable, (AL_CONST PACKFILE_VTABLE *vtable, void *userdata));


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/