[AD] Re: UTF-8 in ufile.c?

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


Updated the patch. According to Wikipedia, using the libc str functions (as is 
done in this file) and doing per-byte character matching is valid on UTF-8 
strings (which was the main thing I was worried about with my last patch, 
that this would somehow break things). Luckilly, it seems it won't. This 
patch lets uconvert use the internal static buffer for converting filename 
stuff where possible.

I'll follow this up with a patch that increases uconvert's internal buffer to 
1024 from 512, and change the note in the docs that the function uses a 
buffer that could hold less than 1024 characters.
Index: src/unix/ufile.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/unix/ufile.c,v
retrieving revision 1.4
diff -u -p -r1.4 ufile.c
--- src/unix/ufile.c	2 Jul 2004 16:25:42 -0000	1.4
+++ src/unix/ufile.c	12 Sep 2005 23:54:15 -0000
@@ -72,9 +72,8 @@ int _al_file_isok(AL_CONST char *filenam
 long _al_file_size(AL_CONST char *filename)
 {
    struct stat s;
-   char tmp[1024];
 
-   if (stat(uconvert_toascii(filename, tmp), &s) != 0) {
+   if (stat(uconvert(filename, U_CURRENT, NULL, U_UTF8, 0), &s) != 0) {
       *allegro_errno = errno;
       return 0;
    }
@@ -90,9 +89,8 @@ long _al_file_size(AL_CONST char *filena
 time_t _al_file_time(AL_CONST char *filename)
 {
    struct stat s;
-   char tmp[1024];
 
-   if (stat(uconvert_toascii(filename, tmp), &s) != 0) {
+   if (stat(uconvert(filename, U_CURRENT, NULL, U_UTF8, 0), &s) != 0) {
       *allegro_errno = errno;
       return 0;
    }
@@ -312,19 +310,18 @@ int al_findfirst(AL_CONST char *pattern,
    struct FF_DATA *ff_data;
    struct stat s;
    int actual_attrib;
-   char tmp[1024];
    char *p;
 
    /* if the pattern contains no wildcard, we use stat() */
-   if (!ustrpbrk(pattern, uconvert_ascii("?*", tmp))) {
+   if (!ustrpbrk(pattern, uconvert("?*", U_ASCII, NULL, U_UTF8, 0))) {
       info->ff_data = NULL;
 
       /* start the search */
       errno = *allegro_errno = 0;
 
-      if (stat(uconvert_toascii(pattern, tmp), &s) == 0) {
+      if (stat(uconvert(pattern, U_CURRENT, NULL, U_UTF8, 0), &s) == 0) {
          /* get file attributes */
-         actual_attrib = ff_get_attrib(ff_get_filename(uconvert_toascii(pattern, tmp)), &s);
+         actual_attrib = ff_get_attrib(ff_get_filename(uconvert(pattern, U_CURRENT, NULL, U_UTF8, 0)), &s);
 
          /* does it match ? */
          if ((actual_attrib & ~attrib) == 0) {
@@ -354,7 +351,7 @@ int al_findfirst(AL_CONST char *pattern,
    /* initialize it */
    ff_data->attrib = attrib;
 
-   do_uconvert(pattern, U_CURRENT, ff_data->dirname, U_ASCII, sizeof(ff_data->dirname));
+   do_uconvert(pattern, U_CURRENT, ff_data->dirname, U_UTF8, sizeof(ff_data->dirname));
    p = ff_get_filename(ff_data->dirname);
    _al_sane_strncpy(ff_data->pattern, p, sizeof(ff_data->pattern));
    if (p == ff_data->dirname)
@@ -443,7 +440,7 @@ int al_findnext(struct al_ffblk *info)
    info->time = s.st_mtime;
    info->size = s.st_size;
 
-   do_uconvert(tempname, U_ASCII, info->name, U_CURRENT, sizeof(info->name));
+   do_uconvert(tempname, U_UTF8, info->name, U_CURRENT, sizeof(info->name));
 
    return 0;
 }
@@ -477,7 +474,7 @@ void _al_getdcwd(int drive, char *buf, i
    char tmp[1024];
 
    if (getcwd(tmp, sizeof(tmp)))
-      do_uconvert(tmp, U_ASCII, buf, U_CURRENT, size);
+      do_uconvert(tmp, U_UTF8, buf, U_CURRENT, size);
    else
       usetc(buf, 0);
 }


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