Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » open directories with special characters

This thread is locked; no one can reply to it. rss feed Print
open directories with special characters
alex glez
Member #16,757
October 2017

Hello !. I have a problem opening directories with special cards. The only thing that works for me is directories with no special characters. Thank you.

.......................

dialogo1 = al_create_native_file_dialog("", "", "", ALLEGRO_FILECHOOSER_FOLDER);
al_show_native_file_dialog(ventana, dialogo1);
DIRECTORIO = al_create_fs_entry(al_get_native_file_dialog_path(dialogo1, 0));
if (!(al_open_directory(DIRECTORIO))) {
al_show_native_message_box(ventana, "Mensaje", "Error", "IMPOSIBLE LEER DIRECTORIO ORIGEN", NULL, ALLEGRO_MESSAGEBOX_ERROR); }

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

You can use <code>code goes here...</code> tags for code please.

Can you produce a simple example program that does this? What do you mean by special characters? Where? In the source code? Or on your OS? Or?

Please be as specific as possible and help us reproduce this. What compiler and what version of allegro are you using?

EDIT

Do you mean specifically unicode characters? Non ascii? What is your OS and what language are you using?

alex glez
Member #16,757
October 2017

I use the Spanish language. Windows 10 system. Visual studio.
The version of allegro is 5.
With the names of folders with special characters
( canción , フォルダの例 ... )
the previous program fails.
It only reads folders with ascii characters from 0 to 127.

Chris Katko
Member #1,881
January 2002
avatar

dialogo1 = al_create_native_file_dialog("", "", "", ALLEGRO_FILECHOOSER_FOLDER);

al_show_native_file_dialog(ventana, dialogo1);

DIRECTORIO = al_create_fs_entry(al_get_native_file_dialog_path(dialogo1, 0));

if (!(al_open_directory(DIRECTORIO))) 
  {
  al_show_native_message_box(ventana, "Mensaje", "Error", 
  "IMPOSIBLE LEER DIRECTORIO ORIGEN", NULL, ALLEGRO_MESSAGEBOX_ERROR); 
  }

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

bamccaig
Member #7,536
July 2006
avatar

I'm new to this end of the API, but from the looks of it calling al_get_native_file_dialog_path when the user hasn't selected a file will just return NULL. Unfortunately, you're not checking return values. To help you understand where your program goes wrong you should be checking return values and printing error messages when something unexpected happens (like if al_get_native_file_dialog_path returns NULL, since your program is currently assuming that won't happen). Don't pass the result of functions that can possibly fail to other functions. Store the result, check it, and then pass it on. It will save you a lot of trouble.

I don't know who created this API, but it seems pretty insane. Apparently you need to query al_get_native_file_dialog_count until it returns a value > 0 and that indicates that you can then get the path(s) from the dialog. This seems very racy and insane. You'd think with Allegro's event system that the dialog would signal selected paths with an event, but I digress... I guess that means to get the path that you need to do something like:

#SelectExpand
1// Create dialog.. 2// Show dialog... 3 4const char * path = NULL; 5 6while (1) { 7 path = al_get_native_file_dialog_path(dialogo1); 8 9 if (path) { 10 break; 11 } 12 13 al_rest(0.2); // h4x: Yield some CPU because this API suxx0rs. 14} 15 16// Yay, if you ever escaped from that loop you should now have a path selected. 17 18al_destroy_native_file_dialog(dialogo1);

It's bizarre that there isn't an event when the user actually clicks "OK" or something in the dialog. Assuming that is a thing. I don't know if I've ever seen the dialog that Allegro produces.

A timeout probably doesn't make sense for a file selection dialog... I cannot imagine how you're supposed to make a user-friendly program from this. Am I missing something crucial to this API, or is it just not really meant to be used?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Peter Hull
Member #1,136
March 2001

Odd. Tried it on Linux (debian/gnome) and it seemed to be OK with unicode filenames but there was no way to choose a folder - if I selected one and pressed Open it would always just open the folder and show its contents.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_native_dialog.h> 3 4int main(int, char *[]) { 5 al_init(); 6 al_init_native_dialog_addon(); 7 ALLEGRO_DISPLAY *ventana = NULL; 8 ALLEGRO_FILECHOOSER *dialogo1 = al_create_native_file_dialog( 9 NULL, "Title", "*", ALLEGRO_FILECHOOSER_FOLDER); 10 11 bool ans = al_show_native_file_dialog(ventana, dialogo1); 12 if (ans && al_get_native_file_dialog_count(dialogo1) > 0) { 13 const char *cc = al_get_native_file_dialog_path(dialogo1, 0); 14 ALLEGRO_FS_ENTRY *DIRECTORIO = al_create_fs_entry(cc); 15 16 if (!(al_open_directory(DIRECTORIO))) { 17 al_show_native_message_box(ventana, "Mensaje", "Error", 18 "IMPOSIBLE LEER DIRECTORIO ORIGEN", NULL, 19 ALLEGRO_MESSAGEBOX_ERROR); 20 } else { 21 al_show_native_message_box(ventana, "Mensaje", "Info", 22 "YES YOU ARE A CAPTAIN", NULL, 0); 23 } 24 } 25 return 0; 26}

Edit: Actually it's been fixed - Debian installs 5.2.2 but when I built Allegro myself (5.2.5) it worked.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Peter, it's still broken on Windows.

I had to fix a null pointer crash for your code to work (in allegro) and then it ran okay. But I tried making a directory called 'ù' and you can't select it with the dialog.

This is with Allegro 5.2.X from GIT.

The null pointer crash is in win_dialog.c on line

#SelectExpand
71static bool select_folder(ALLEGRO_DISPLAY_WIN *win_display, 72 ALLEGRO_NATIVE_DIALOG *fd) 73{ 74 BROWSEINFO folderinfo; 75 LPCITEMIDLIST pidl; 76 char buf[MAX_PATH] = ""; 77 char dbuf[MAX_PATH] = ""; 78
79 folderinfo.hwndOwner = win_display->window;
80 folderinfo.pidlRoot = NULL; 81 folderinfo.pszDisplayName = dbuf; 82 folderinfo.lpszTitle = al_cstr(fd->title); 83 folderinfo.ulFlags = 0; 84 folderinfo.lpfn = NULL; 85 86 pidl = SHBrowseForFolder(&folderinfo); 87 if (pidl) { 88 SHGetPathFromIDList(pidl, buf); 89 fd->fc_path_count = 1; 90 fd->fc_paths = al_malloc(sizeof(void *)); 91 fd->fc_paths[0] = al_create_path(buf); 92 return true; 93 } 94 return false; 95}

al_show_native_file_dialog specifically says you can pass null for the display, but select_folder doesn't handle it. Assigning hwndOwner to NULL works fine to fix it though.

alex glez
Member #16,757
October 2017

I interpret then that you can not open folders in this way that contain characters that are not ascii (from 0 to 127)?

With directory names (ascii from 0 to 127) it works perfect ..

Peter Hull
Member #1,136
March 2001

OK,
when I said "it's been fixed" I meant not being able to select directories on Linux.
The problem with Windows seems to be that it was written specifically to use the ANSI API. (As I'm sure you know) a lot of Windows functions have two variants, one ending in A for ANSI the other ending in W for wide characters, i.e. unicode. (example)
Edgar, your null pointer crash is also a bug, well spotted - the docs say it's OK to pass NULL for the display.
Pete

alex glez
Member #16,757
October 2017

I understand, thanks to everyone for the help. My knowledge is very limited, anyway I have 95% completed the program (some retouching is missing) and although I would like to solve the problem, it is not vital for its correct functioning. Maybe for later ...

Doctor Cop
Member #16,833
April 2018
avatar

So guys how are we going to fix it?

please tell me technicalities, I want to work on it.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

There are several problems. One, as Peter mentioned, the ascii version of windows dialog functions are being used, instead of the wide character version. This means no unicode support.

Second, the old dialogs have been superseded by the Common Item dialog starting with Windows Vista, so there are new Open and Save dialogs that should be used instead.

Common Item Dialog

Peter Hull
Member #1,136
March 2001

1. Fork https://github.com/liballeg/allegro5
2. Fix
3. Test
4. Make a pull request

and you're done!

Go to: