for_each_file_ex(3alleg4)

✨ for_each_file_ex ✨

okay bestie, welcome to the Allegro manual, section 3alleg4 πŸ’– β€” this one's literally just "do a thing for every file that matches a wildcard", and it ate, no cap.

🌸 NAME 🌸

for_each_file_ex β€” runs callback() for each file that matches a wildcard. It's giving Allegro game programming library realness πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <allegro.h>

int for_each_file_ex(const char *name, int in_attrib, int out_attrib,
int (*callback)(const char *filename, int attrib, void *param), void
*param);

πŸ’… DESCRIPTION πŸ’…

So the way this works: it finds all the files on disk that match the wildcard spec you give it and the file attributes you asked for, then it fires callback() once for each one. Periodt. πŸ’«

Basically? It's a convenient lil wrapper around al_findfirst(), al_findnext() and al_findclose() β€” the trio. Think of it as the group chat version of those three, where you don't have to @ each one individually 🫢

Now the tea on the two attribute masks:

  • in_attrib β€” a bitmask saying which attributes the files must carry (vibe check: they gotta have these).
  • out_attrib β€” a bitmask saying which attributes the files must not carry (nope, not these).
  • Attributes that show up in neither bitmask are simply not taken into account when deciding whether callback() gets invoked or not. They're just there, unbothered, minding their business πŸ™ˆ

πŸ’œ the callback πŸ’œ

Your callback function gets passed three arguments:

  1. A string with the completed filename β€” which is exactly the same string you passed to for_each_file_ex(), but with meta characters. Yes bestie, same string, meta characters included, fr fr.
  2. The actual attributes of the file.
  3. A void pointer which is just a copy of param β€” use it for literally whatever you want, it's yours πŸ‘‘
And the rule for the callback: it must return zero to let the enumeration keep going, or any non-zero value to stop it. That's the whole vibe check. βœ‹

Also important: if an error happens, the error code gets stored in errno, but the enumeration won't stop. She keeps going, iconic behaviour honestly 😭

Example, because you know we love an example:

int show_name(const char *filename, int attrib, void *param)
{
   allegro_message("Caught `%s', attribs %d\n",
                   filename, attrib);
   return 0;
}
...
   count = for_each_file_ex("data/level*", FA_DIREC,
                            0, show_name, 0);
   allegro_message("%d game directories\n", count);

look at her go πŸ₯Ή collecting all those data/level* directories like PokΓ©mon.


⭐ RETURN VALUE ⭐

Returns the number of successful calls made to callback() β€” that is, the number of times callback() was called and returned 0. So if your callback bails early with a non-zero, that one doesn't count toward the number. Math is math πŸ’€


πŸ‘‘ SEE ALSO πŸ‘‘

the whole squad, go read them next πŸ’–


and that's the page, bestie β€” brought to you by Allegro, version 4.4.3, dated right there with for_each_file_ex(3alleg4) πŸ’…βœ¨ mwah, go enumerate some files, you're doing amazing sweetie πŸŽ€πŸ¦‹πŸ’œ