|
|
- #include "tstring.h"
- #include "string.h"
- #include "stdlib.h"
- #include "stdio.h"
-
- void generator(
- tstring* header_filename,
- tstring* prefix,
- tstring* bucket_count,
- tstring* K_size, tstring* K_comp_pred, tstring* K_hash_func,
- tstring* V_size
- ) {
- tstring* key_type = tstring_n_compose("tc",
- prefix, "_key_t"
- );
- tstring* key_type_decl = tstring_n_compose("ctctc",
- "struct ", key_type, "{\n"
- "char* data[",K_size,"];\n"
- "};\n"
- );
- tstring* value_type = tstring_n_compose("tc",
- prefix, "_value_t"
- );
- tstring* value_type_decl = tstring_n_compose("ctctc",
- "struct ", value_type, "{\n"
- "char* data[",V_size,"];\n"
- "};\n"
- );
- tstring* accessor_type = tstring_n_compose("tc",
- prefix, "_accessor_t"
- );
- tstring* accessor_type_decl = tstring_n_compose("ctc",
- "struct ", accessor_type, "{\n"
- "void* value;\n"
- "};\n"
- );
- tstring* hash_decl = tstring_n_compose("ctctctctc",
- "size_t ",K_hash_func,"(void*);\n"
- "struct ",prefix,"_hashtype{\n"
- "size_t operator()(",key_type," key) {\n"
- "return ", K_hash_func, "(key);\n"
- "}\n"
- "}\n"
- );
- tstring* hashmap_ptr_type = tstring_n_compose("tc",
- prefix,"_hashmap_ptr"
- );
- tstring* hashmap_decl = tstring_n_compose("ctc",
- "typedef void* ",hashmap_ptr_type,";\n"
- );
- tstring* create_hashmap_decl = tstring_n_compose("tctc",
- hashmap_ptr_type," ",prefix,"_hm_create();\n"
- );
- tstring* destroy_hashmap_decl = tstring_n_compose("ctctc",
- "void ", prefix, "_hm_destroy(",hashmap_ptr_type,");\n"
- );
- tstring* hashmap_push_decl = tstring_n_compose("ctctctc",
- "void ", prefix, "_hm_push(",key_type,"* k, ",value_type,"* v);\n"
- );
- tstring* hashmap_get_decl = tstring_n_compose("tctctc",
- accessor_type, " ", prefix, "_hm_get(",key_type,"* k);\n"
- );
- tstring* hashmap_endget_decl = tstring_n_compose("ctctc",
- "void ", prefix, "_hm_endget(",accessor_type," acc);\n"
- );
- tstring* hashmap_remove_decl = tstring_n_compose("ctctc",
- "void ", prefix, "_hm_remove(",key_type,"* k);\n"
- );
- tstring* top_guard = tstring_n_compose("ctctc",
- "#ifndef GUARD_", prefix,
- "\n#define GUARD_", prefix,"\n\n"
- );
- tstring* bottom_guard = tstring_n_compose("ctc",
- "\n#endif // GUARD_", prefix, "\n"
- );
- FILE* header;
- if(
- !(header = fopen(header_filename->data, "w"))
- ) {
- char* m = header_filename->data;
- fprintf(stderr, "Couldn't open header file: %s\n", header_filename->data);
- exit(EXIT_FAILURE);
- }
- if(
- tstring_n_write(
- header,12,
- top_guard,
- key_type_decl,
- value_type_decl,
- accessor_type_decl,
- hashmap_decl,
- create_hashmap_decl,
- destroy_hashmap_decl,
- hashmap_get_decl,
- hashmap_endget_decl,
- hashmap_push_decl,
- hashmap_remove_decl,
- bottom_guard
- ) != 0
- ) {
- fprintf(stderr, "Couldn't generate header file: error writing to file: %s\n", header_filename->data);
- exit(EXIT_FAILURE);
- }
- }
|