diff --git a/Makefile b/Makefile index 846144a..159ccd4 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES)) CC = gcc -CFLAGS = -Wall -Werror -Wextra -g2 +CFLAGS = -Wall -Werror -Wextra -g3 LIB_NAME = libft diff --git a/src/nm.c b/src/nm.c index b33fcce..b5b9a93 100644 --- a/src/nm.c +++ b/src/nm.c @@ -139,24 +139,79 @@ char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec) { return ("n"); } -int strcmp_nm(t_list *a, t_list *b) { +int strcmp_nm(void *a, void *b) { + t_entry *aa = (t_entry *)a; + t_entry *bb = (t_entry *)b; + char *s1 = aa->symbol; + char *s2 = bb->symbol; + size_t a_i = 0; + size_t b_i = 0; + while (s1[a_i] && s2[b_i]) { + bool is_okay = true; + char c = s1[a_i] & 0x11101111; + if (!(c >= 'a' && c <= 'z')) { + ++a_i; + is_okay = false; + } + c = s2[b_i] & 0x11101111; + if (!(c >= 'a' && c <= 'z')) { + is_okay = false; + ++b_i; + } + if (is_okay) { + printf("comparing %c and %c\n", s1[a_i], s2[b_i]); + if ((s1[a_i] & 0x11101111) != (s2[b_i] & 0x11101111)) { + return s1[a_i] - s2[b_i]; + } else { + a_i++; + b_i++; + } + } + } + return s1[a_i] - s2[b_i]; +} + +int reverse(void *a, void *b) { + return strcmp_nm(b, a); +} + +int nosort(void *a, void *b) { (void)a; (void)b; return 1; } -int reverse(t_list *a, t_list *b) { - return strcmp_nm(b, a); -} - -int nosort(t_list *a, t_list *b) { - (void)a; - (void)b; - return -1; +void put_entry(void *data) { + t_node *node = (t_node *)data; + t_entry *entry = (t_entry *)node->data; +/* if (node->parent) { + ft_putstr("node has parent: "); + t_node *parent = (t_node *)node->parent; + t_entry *lol = (t_entry *)parent->data; + ft_putstr(lol->string); + } + if (node->left) { + ft_putstr("node has left: "); + t_node *left = (t_node *)node->left; + t_entry *lol = (t_entry *)left->data; + ft_putstr(lol->string); + } + if (node->right) { + ft_putstr("node has right: "); + t_node *right = (t_node *)node->right; + t_entry *lol = (t_entry *)right->data; + ft_putstr(lol->string); + } + if (node->color == RED) { + ft_putstr("RED NODE: "); + } else { + ft_putstr("BLACK NODE: "); + }*/ + ft_putstr(entry->string); } int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering) { - t_list *list = NULL; + t_root *tree = NULL; t_entry *entry = (t_entry *)malloc(sizeof(t_entry)); if (!entry) return FT_NM_FAILURE; @@ -251,13 +306,13 @@ int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin // ensuite un switch ordering avec un ft_lstinsert (pointeur sur fonction serait plus opti mais osef) switch (ordering) { case NOSORT: - ft_lstinsert(&list, ft_lstnew((void *)entry, sizeof(t_entry)), &nosort); + ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &nosort); break; case REVERSE: - ft_lstinsert(&list, ft_lstnew((void *)entry, sizeof(t_entry)), &reverse); + ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &reverse); break; case DEFAULT_ORDERING: - ft_lstinsert(&list, ft_lstnew((void *)entry, sizeof(t_entry)), &strcmp_nm); + ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &strcmp_nm); break; } // pas de bst oops :D @@ -269,15 +324,32 @@ int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin free(str); } // ici on affiche tout ou no symbols en cas de list vide +/* char buffer[16384]; + size_t buf_i = 0; + size_t entry_string_len = 0; if (list) { while (list) { t_entry *my_entry = (t_entry *)list->content; - ft_putstr(my_entry->string); + entry_string_len = ft_strlen(my_entry->string); + if (buf_i + entry_string_len < 16384) { + ft_memcpy(buffer + buf_i, my_entry->string, entry_string_len); + buf_i += entry_string_len; + } else { + buffer[buf_i] = '\0'; + ft_putstr(buffer); + buf_i = 0; + } + //ft_putstr(my_entry->string); list = list->next; } } else { ft_printf("sss", "nm: ", path, ": no symbols\n"); + }*/ + if (tree) { + ft_putrbt(tree, &put_entry); + } else { + ft_printf("sss", "nm: ", path, ": no symbols\n"); } return FT_NM_SUCCESS; }