1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| typedef struct s_options
{
/*...*/
} t_options;
typedef void (*fct)(t_options const *arg);
typedef struct s_fct
{
char const *nom;
fct const fonction;
} t_fct;
/*Fonction de comparaison*/
int compare_cle_fct(const void *pcvCle, const void *pcvFct)
{
char const *cle = pcvCle;
t_cft const *fct = pcvFct;
return strcmp(cle, fct->nom);
}
/*Fonction de recherche dans un tableau de fct*/
t_fct const *bsearch_fct_c(
char const *nom,
t_fct const *fonctions,
size_t nbFonctions
)
{
return bsearch(nom, fonctions, nbFonctions, sizeof *fonctions, compare_cle_fct);
}
/*
Variables globales et fonctions les utilisant
*/
/*Attention, ce tableau doit être trié*/
static t_fct const g_mesFct[] = {{"fct1", &fct1}, {"fct2", &fct2}};
static size_t const g_nbFonctions = sizeof g_mesFct / sizeof g_mesFct[0];
/*Fonction de recherche dans le tableau global de fct*/
t_fct const * GetFonction(char const *nom)
{
return bsearch_fct_c(nom, g_mesFct, g_nbFonctions);
} |
Partager