Re: [AD] Windows unicode filename support

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


Oops, sorry, I've send bad file. I'm really sorry for that.

--
Regards,
    Michal

ICQ# 175762750
--- wfile.c.newest	2006-05-03 21:31:08.000000000 +0200
+++ wfile.c	2006-05-03 22:27:58.000000000 +0200
@@ -30,6 +30,36 @@
 #endif
 
 
+/* convert_to_windows_codepage:
+ *  Converts Allegro string into the Windows codepage.
+ *  Don't forget to _AL_FREE the returned string.
+ */
+static char* convert_to_windows_codepage(AL_CONST char *str)
+{
+   int len = ustrlen(str);
+   int wchar_len = sizeof(WCHAR) * (len + 1);
+   WCHAR* buffer = (WCHAR*) _AL_MALLOC(wchar_len);
+   char* win_str = (char*) _AL_MALLOC(len + 1);
+   WCHAR* wchar_str = (WCHAR*) uconvert(str, U_CURRENT, (char*)buffer, 
+                                        U_UNICODE, wchar_len);
+   WideCharToMultiByte(CP_ACP, 0, wchar_str, len, win_str, len + 1, 0, 0);
+   _AL_FREE(buffer);
+   return win_str;
+}
+
+/* convert_from_windows_codepage:
+ *  Converts string from the Windows codepage to Allegro's current.
+ */
+static void convert_from_windows_codepage(AL_CONST char *str, char* buffer,
+                                          int buffer_size)
+{
+   int len = MultiByteToWideChar(CP_ACP, 0, str, -1, 0, 0);
+   int wchar_len = sizeof(WCHAR) * (len + 1);
+   WCHAR* wchar_str = (WCHAR*) _AL_MALLOC(wchar_len);
+   MultiByteToWideChar(CP_ACP, 0, str, -1, wchar_str, wchar_len);
+   do_uconvert((char*)wchar_str, U_UNICODE, buffer, U_CURRENT, buffer_size);
+   _AL_FREE(wchar_str);
+}
 
 /* _al_file_isok:
  *  Helper function to check if it is safe to access a file on a floppy
@@ -49,13 +79,14 @@
 long _al_file_size(AL_CONST char *filename)
 {
    struct _stat s;
-   char tmp[1024];
+   char *win_filename = convert_to_windows_codepage(filename);
 
-   if (_wstat((wchar_t*)uconvert(filename, U_CURRENT, tmp, U_UNICODE,
-              sizeof(tmp)), &s) != 0) {
+   if (_stat(win_filename, &s) != 0) {
+      _AL_FREE(win_filename);
       *allegro_errno = errno;
       return 0;
    }
+   _AL_FREE(win_filename);
 
    return s.st_size;
 }
@@ -68,13 +99,14 @@
 time_t _al_file_time(AL_CONST char *filename)
 {
    struct _stat s;
-   char tmp[1024];
+   char *win_filename = convert_to_windows_codepage(filename);
 
-   if (_wstat((wchar_t*)uconvert(filename, U_CURRENT, tmp, U_UNICODE,
-              sizeof(tmp)), &s) != 0) {
+   if (_stat(win_filename, &s) != 0) {
+      _AL_FREE(win_filename);
       *allegro_errno = errno;
       return 0;
    }
+   _AL_FREE(win_filename);
 
    return s.st_mtime;
 }
@@ -84,7 +116,7 @@
 /* structure for use by the directory scanning routines */
 struct FF_DATA
 {
-   struct _wfinddata_t data;
+   struct _finddata_t data;
    long handle;
    int attrib;
 };
@@ -102,8 +134,8 @@
    info->time = ff_data->data.time_write;
    info->size = ff_data->data.size;
 
-   do_uconvert((const char*)ff_data->data.name, U_UNICODE, info->name, U_CURRENT,
-               sizeof(info->name));
+   convert_from_windows_codepage(ff_data->data.name, info->name, 
+                                 sizeof(info->name));
 }
 
 
@@ -114,7 +146,7 @@
 int al_findfirst(AL_CONST char *pattern, struct al_ffblk *info, int attrib)
 {
    struct FF_DATA *ff_data;
-   char tmp[1024];
+   char* win_pattern = convert_to_windows_codepage(pattern);
 
    /* allocate ff_data structure */
    ff_data = _AL_MALLOC(sizeof(struct FF_DATA));
@@ -144,8 +176,8 @@
    /* start the search */
    errno = *allegro_errno = 0;
 
-   ff_data->handle = _wfindfirst((wchar_t*)uconvert(pattern, U_CURRENT, tmp,
-                                 U_UNICODE, sizeof(tmp)), &ff_data->data);
+   ff_data->handle = _findfirst(win_pattern, &ff_data->data);
+   _AL_FREE(win_pattern);
 
    if (ff_data->handle < 0) {
       *allegro_errno = errno;
@@ -177,7 +209,7 @@
    struct FF_DATA *ff_data = (struct FF_DATA *) info->ff_data;
 
    do {
-      if (_wfindnext(ff_data->handle, &ff_data->data) != 0) {
+      if (_findnext(ff_data->handle, &ff_data->data) != 0) {
          *allegro_errno = errno;
          return -1;
       }
@@ -231,10 +263,10 @@
  */
 void _al_getdcwd(int drive, char *buf, int size)
 {
-   wchar_t tmp[1024];
+   char tmp[1024];
 
-   if (_wgetdcwd(drive+1, tmp, sizeof(tmp)/sizeof(tmp[0])))
-      do_uconvert((const char*)tmp, U_UNICODE, buf, U_CURRENT, size);
+   if (_getdcwd(drive+1, tmp, sizeof(tmp)))
+      convert_from_windows_codepage(tmp, buf, size);
    else
       usetc(buf, 0);
 }


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