ALLEGRO_FILE_INTERFACE(3alleg5)

πŸ“¦
version 5.2.11.3 Β· 4 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ ALLEGRO_FILE_INTERFACE ✨

okay bestie, welcome to the Allegro 5 API manual, section 3alleg5, and this one is genuinely a little iconic πŸ’… it's all about ALLEGRO_FILE_INTERFACE.


πŸŽ€ NAME πŸŽ€

ALLEGRO_FILE_INTERFACE β€” Allegro 5 API. that's it, that's the name, she's a whole API entry 🫢


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <allegro5/allegro.h>

typedef struct ALLEGRO_FILE_INTERFACE

literally just include the header and boom, the struct exists ✨


πŸ’– DESCRIPTION πŸ’–

so bestie, this is a struct full of function pointers that handles one type of "file" β€” could be a real file, could be a virtual one, we don't judge 🌈 it's giving pluggable file backend energy.

if you want the whole deep-dive discussion, go read al_set_new_file_interface(3alleg5) β€” that's where the lore lives πŸ‘‘

the fields are these, and yes you get all thirteen, no skips:

void*         (*fi_fopen)(const char *path, const char *mode);
bool          (*fi_fclose)(ALLEGRO_FILE *f);
size_t        (*fi_fread)(ALLEGRO_FILE *f, void *ptr, size_t size);
size_t        (*fi_fwrite)(ALLEGRO_FILE *f, const void *ptr, size_t size);
bool          (*fi_fflush)(ALLEGRO_FILE *f);
int64_t       (*fi_ftell)(ALLEGRO_FILE *f);
bool          (*fi_fseek)(ALLEGRO_FILE *f, int64_t offset, int whence);
bool          (*fi_feof)(ALLEGRO_FILE *f);
int           (*fi_ferror)(ALLEGRO_FILE *f);
const char *  (*fi_ferrmsg)(ALLEGRO_FILE *f);
void          (*fi_fclearerr)(ALLEGRO_FILE *f);
int           (*fi_fungetc)(ALLEGRO_FILE *f, int c);
off_t         (*fi_fsize)(ALLEGRO_FILE *f);

now the fi_open function β€” okay so here's the tea 🍡 β€” it must allocate memory for whatever userdata structure it needs. that pointer to that memory has to get returned, and then it gets associated with the file. the other functions can reach that data by calling al_get_file_userdata(3alleg5) on the file handle, periodt 🫢

and if fi_open returns NULL? then al_fopen(3alleg5) will also return NULL. matching energy, no cap πŸ’€

heads up bestie β€” the fi_fclose function has to clean up and free the userdata, but Allegro itself will free the ALLEGRO_FILE(3alleg5) handle. so don't you dare double-free, i'm serious 😭

one more lil thing 🌸 β€” if fi_fungetc is NULL, then Allegro's default implementation kicks in, and that default is a 16 char long buffer. free real estate, you don't gotta write it yourself ✨


and that's the whole page, bestie, you ate it πŸ’… this was the Allegro reference manual's take on ALLEGRO_FILE_INTERFACE(3alleg5) β€” go forth and implement your file interface, you main character πŸ¦‹πŸ‘‘πŸŽ€

πŸ“¦ where this page comes from