What would take to port this C-lib to the API : http://troydhanson.github.io/uthash/index.html
Are there any Hash librarys in the API already?
Never mind, it’s just a header file so you just need to copy and paste the “uthash.h” from https://github.com/troydhanson/uthash/blob/master/src/uthash.h
So, if you in case need to do a simple HASH you can do it like this:
struct funcoes { // Create a struct to map an id to a function
int id;
void (*func)(void);
UT_hash_handle hh; // needed to hash
};
struct funcoes * FUNCOES = NULL; // Create the reference that will hold all the functions
void add_func(struct funcoes *f) { // Standard method to add a hash
HASH_ADD_INT( FUNCOES, id, f );
}
struct funcoes *find_func(int func_id) { // Standard method to find a hash
struct funcoes *f;
HASH_FIND_INT( FUNCOES, &func_id, f );
return f;
}
void function_AJUDA(void) { // Function Example
terminal.println("\nAJUDA:");
terminal.flush();
}
struct funcoes FUNCAOAJUDA = {1, function_AJUDA}; // Put function in the struct with an ID value (Arbitrary)
So we just need to add this to our Hash, and we can access the function via it’s ID value like this:
add_func(&FUNCAOAJUDA);
And:
find_func(1)->func();
The output is:
terminal: AJUDA:
1 Like