mirror of
https://gittea.dev/nova/th.git
synced 2026-05-01 05:55:15 -04:00
no longer mallocs new strings on dir print
This commit is contained in:
@@ -25,11 +25,6 @@ int (*order_func)() = sort_natural;
|
||||
linked_dir *visited_dirs;
|
||||
linked_dir *current_dir;
|
||||
|
||||
unsigned long get_dir_size(char *path);
|
||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
|
||||
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);
|
||||
|
||||
|
||||
|
||||
unsigned long get_dir_size(char *path){
|
||||
DIR *dir = opendir(path);
|
||||
@@ -236,47 +231,6 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* shortens the printed file name if it is too long
|
||||
* example input: aaaaaaaa.txt
|
||||
* example output: aaa~.txt
|
||||
* example input: aaaaaaaa.longextension
|
||||
* example output: ~.longe~
|
||||
* if no extension is found, the name will truncate */
|
||||
char *file_name = NULL;
|
||||
unsigned long printable_size = offset_back - (is_selected + offset_front);
|
||||
if (strlen(dir_content[i].file_name) >= printable_size) {
|
||||
char *extension = strrchr(dir_content[i].file_name, '.');
|
||||
if (extension && extension != dir_content[i].file_name) {
|
||||
file_name = malloc(printable_size+1);
|
||||
|
||||
/*if the extension is bigger than the filename this is false */
|
||||
if ((&file_name[printable_size - strlen(extension)] - 2) > file_name) {
|
||||
memcpy(file_name, dir_content[i].file_name, (printable_size - strlen(extension)));
|
||||
memcpy(file_name + (printable_size - strlen(extension))-1, extension, strlen(extension)+1);
|
||||
file_name[printable_size - strlen(extension) - 2] = '~';
|
||||
file_name[printable_size] = '\0';
|
||||
} else {
|
||||
memcpy(file_name + 1, extension, printable_size - 1);
|
||||
file_name[0] = '~';
|
||||
file_name[printable_size-2] = '~';
|
||||
file_name[printable_size-1] = '\0';
|
||||
}
|
||||
} else {
|
||||
file_name = malloc(printable_size);
|
||||
memcpy(file_name, dir_content[i].file_name, printable_size);
|
||||
file_name[printable_size-2] = '~';
|
||||
file_name[printable_size-1] = '\0';
|
||||
file_name[printable_size] = '\0';
|
||||
}
|
||||
} else {
|
||||
file_name = malloc(strlen(dir_content[i].file_name)+1);
|
||||
memcpy(file_name, dir_content[i].file_name, strlen(dir_content[i].file_name));
|
||||
file_name[strlen(dir_content[i].file_name)] = '\0';
|
||||
}
|
||||
|
||||
|
||||
|
||||
mvwaddstr(win, i-offset_vertical, 0, bg);
|
||||
if(print_info) {
|
||||
#if SETTINGS_LINE_NUMBERS == 2
|
||||
@@ -309,7 +263,6 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
mvwprintw(win, i-offset_vertical, offset_front-offset_index, "%ld", i);
|
||||
#endif
|
||||
|
||||
mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, file_name, line_width-offset_front-is_selected-2);
|
||||
|
||||
if (dir_content[i].file_type &= FILE_TYPE_DIR) {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir_content[i].file_size);
|
||||
@@ -318,15 +271,24 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
} else {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%0.2lf %c", printed_size, size_char);
|
||||
}
|
||||
} else {
|
||||
mvwaddnstr(win, i-offset_vertical, 0, file_name, line_width);
|
||||
}
|
||||
char *extension = strrchr(dir_content[i].file_name, '.');
|
||||
|
||||
size_t printable_size = offset_back-offset_front-is_selected-2;
|
||||
|
||||
mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, dir_content[i].file_name, printable_size);
|
||||
if (extension && printable_size <= strlen(dir_content[i].file_name)) {
|
||||
mvwaddnstr(win, i-offset_vertical, offset_back-strlen(extension)-1, extension, strlen(extension));
|
||||
mvwaddnstr(win, i-offset_vertical, offset_back-strlen(extension)-2, "~", 1);
|
||||
}
|
||||
|
||||
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
wattroff(win, COLOR_PAIR(8));
|
||||
} else {
|
||||
wattroff(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
}
|
||||
free(file_name);
|
||||
/*free(file_name);*/
|
||||
|
||||
}
|
||||
free(bg);
|
||||
|
||||
Reference in New Issue
Block a user