mirror of
https://gittea.dev/nova/th.git
synced 2025-10-21 10:20:15 -04:00
slight speed optimization
This commit is contained in:
63
dir.c
63
dir.c
@@ -34,19 +34,21 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
unsigned long get_dir_size(char *path){
|
||||
DIR *dir = opendir(path);
|
||||
unsigned long entry_count = 0;
|
||||
if (dir) {
|
||||
struct dirent *entry;
|
||||
struct dirent *entry;
|
||||
if (dir && file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
|
||||
while ((entry=readdir(dir))) {
|
||||
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
entry_count++;
|
||||
}
|
||||
/* removes files "." and ".." */
|
||||
entry_count -= 2;
|
||||
} else if (dir) {
|
||||
while ((entry=readdir(dir))) {
|
||||
if (entry->d_name[0] != '.') {
|
||||
entry_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
|
||||
/* removes files "." and ".." */
|
||||
entry_count -= 2;
|
||||
}
|
||||
closedir(dir);
|
||||
return entry_count;
|
||||
|
||||
}
|
||||
@@ -65,24 +67,20 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
|
||||
} else {
|
||||
dir_content[i].file_name = malloc(strlen(entry[i]->d_name)+1);
|
||||
strcpy(dir_content[i].file_name, entry[i]->d_name);
|
||||
dir_content[i].file_name[strlen(entry[i]->d_name)] = '\0';
|
||||
|
||||
|
||||
struct stat *file;
|
||||
file = malloc(sizeof(struct stat));
|
||||
memset(file, ' ', sizeof(struct stat));
|
||||
|
||||
/* using the full path allows using the same function for all windows */
|
||||
unsigned long path_len = strlen(path);
|
||||
char *full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/"));
|
||||
memcpy(full_path, path, strlen(path));
|
||||
memcpy(full_path + path_len, "/", sizeof("/"));
|
||||
memcpy(full_path + path_len + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
|
||||
memcpy(full_path + strlen(path) + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
|
||||
full_path[strlen(path)] = '/';
|
||||
|
||||
lstat(full_path, file);
|
||||
|
||||
dir_content[i].file_size = file->st_size;
|
||||
dir_content[i].permissions = 1;
|
||||
dir_content[i].permissions = file->st_mode;
|
||||
|
||||
if (S_ISDIR(file->st_mode)) {
|
||||
@@ -102,6 +100,18 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
|
||||
} else if (file->st_mode & S_IXUSR) {
|
||||
dir_content[i].file_type = FILE_TYPE_EXEC;
|
||||
dir_content[i].color_pair = COLOR_EXEC;
|
||||
} else if (S_ISREG(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_REGULAR;
|
||||
dir_content[i].color_pair = COLOR_REGULAR;
|
||||
unsigned long j = 0;
|
||||
char *extension = strrchr(entry[i]->d_name, '.');
|
||||
if (extension) {
|
||||
for (j = 0; j < color_count; j++) {
|
||||
if (!strncmp(colors[j].file_extension, extension, strlen(colors[j].file_extension))) {
|
||||
dir_content[i].color_pair = colors[j].color_pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (S_ISBLK(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_BLOCK;
|
||||
dir_content[i].color_pair = COLOR_BLOCK;
|
||||
@@ -113,19 +123,6 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
|
||||
} else if (S_ISSOCK(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_SOCK;
|
||||
dir_content[i].color_pair = COLOR_SOCK;
|
||||
} else if (S_ISREG(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_REGULAR;
|
||||
dir_content[i].color_pair = COLOR_REGULAR;
|
||||
unsigned long j = 0;
|
||||
char *extension = strrchr(entry[i]->d_name, '.');
|
||||
if (extension) {
|
||||
for (j = 0; j < color_count; j++) {
|
||||
if (!strcmp(colors[j].file_extension, extension)) {
|
||||
dir_content[i].color_pair = colors[j].color_pair;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
}
|
||||
} else {
|
||||
dir_content[i].file_type = COLOR_REGULAR;
|
||||
dir_content[i].color_pair = COLOR_REGULAR;
|
||||
@@ -133,7 +130,7 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
|
||||
char *extension = strrchr(entry[i]->d_name, '.');
|
||||
if (extension) {
|
||||
for (j = 0; j < color_count; j++) {
|
||||
if (!strcmp(colors[j].file_extension, extension)) {
|
||||
if (!strncmp(colors[j].file_extension, extension, strlen(colors[j].file_extension))) {
|
||||
dir_content[i].color_pair = colors[j].color_pair;
|
||||
}
|
||||
}
|
||||
@@ -204,10 +201,10 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
* if no extension is found, the name will truncate */
|
||||
char *file_name;
|
||||
unsigned long file_name_width = strlen(dir_content[i].file_name);
|
||||
if ((file_name_width + offset_front + is_selected) > offset_back - 1) {
|
||||
if ((file_name_width + offset_front + is_selected) > line_width - 3) {
|
||||
char *extension = strrchr(dir_content[i].file_name, '.');
|
||||
if (extension) {
|
||||
int char_offset = (file_name_width + offset_front + is_selected) - (offset_back - 1) ;
|
||||
int char_offset = (file_name_width + offset_front + is_selected) - (line_width - 3) ;
|
||||
if ((file_name_width - char_offset - strlen(extension) - 1) > 1) {
|
||||
file_name = malloc(file_name_width - char_offset + 1);
|
||||
memcpy(file_name, dir_content[i].file_name, file_name_width - char_offset);
|
||||
@@ -221,9 +218,9 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
|
||||
file_name[strlen(extension)] = '\0';
|
||||
}
|
||||
} else {
|
||||
file_name = malloc(file_name_width+1);
|
||||
memcpy(file_name, dir_content[i].file_name, file_name_width);
|
||||
file_name[file_name_width] = '\0';
|
||||
file_name = malloc(file_name_width+1);
|
||||
memcpy(file_name, dir_content[i].file_name, file_name_width);
|
||||
file_name[file_name_width] = '\0';
|
||||
}
|
||||
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user