Hi to all.I'm trying to rewrite Allegro 4 code to Allegro 5 and I miss function - for_each_file - in A5. I found some routines on the net but they are not working. I have only little free time for programing so I can't study those routines for errors.Could someone tell me where can I find functional routine or some really simple way to replace function for_each_file in A5. I need to find all files in directory for example ".jpg" files and store their file names in a field of strings.Thank you very much.
Basically you al_create_fs_entry a filesystem entry for your desired directory, then you al_open_directory the directory, al_read_directory it until there are no entries left, and then al_close_directory it.
Here is an example program that lists all the files in a given directory. You can specify the directory as a command line argument or else it will default to the current directory.
Hmm, seeing that the code to replicate for_each_file is relatively long, I'm in favor of adding an al_for_each_file() function.
beoran +1
He could also use the ALLEGRO_PATH api to handle paths and files. Would cut out a bit of that code.
He could also use the ALLEGRO_PATH api to handle paths and files. Would cut out a bit of that code.
Replace the loop with this then:
I also added directory detection, but it's still kinda longish. The proposed change would look something like:
void callback(char const *filename, void *user) { printf("%s\n", filename); } al_for_each_file(read_dir, callback, int flags, user);
Which is much more simple It also handles common errors someone would make on implementing it on their own, like not ignoring the "." and ".." entries we have on some platforms and dealing with directories where al_get_path_filename fails.
Possible flags could be:
FULL_PATH - pass the full path to the callback not just the filename
RECURSIVE - also recurse into any directories found
INCLUDE_DIRECTORIES - also call the callback with directory names not just files
Actually I already made a patch, with two functions, documentation and a test. One function uses a callback with char * file names and the other with ALLEGRO_FS_ENTRY.
The flags parameter is a good idea though, I'll modify my patch to use that. I'll probably add the ALLEGRO_EACH_FILE_RECURSE, ALLEGRO_EACH_FILE_DIRECTORIES, ALLEGRO_EACH_FILE_DOTFILES and ALLEGRO_EACH_FILE_SHORT_PATH flags. The default call does not recurse, does not return directories, does return not dotfiles, and does return full file names, not partial ones, since full names are normally most useful.