[Bf-blender-cvs] [d3967ce27c0] master: Cleanup: use early return in bli_builddir, reduce right shift
Campbell Barton
noreply at git.blender.org
Wed Jan 25 00:51:33 CET 2023
Commit: d3967ce27c04b0e49a2c5daaa605c543b87266f3
Author: Campbell Barton
Date: Wed Jan 25 10:48:56 2023 +1100
Branches: master
https://developer.blender.org/rBd3967ce27c04b0e49a2c5daaa605c543b87266f3
Cleanup: use early return in bli_builddir, reduce right shift
===================================================================
M source/blender/blenlib/intern/BLI_filelist.c
===================================================================
diff --git a/source/blender/blenlib/intern/BLI_filelist.c b/source/blender/blenlib/intern/BLI_filelist.c
index 2f7662e6220..69d07e52b08 100644
--- a/source/blender/blenlib/intern/BLI_filelist.c
+++ b/source/blender/blenlib/intern/BLI_filelist.c
@@ -45,7 +45,7 @@
* Ordering function for sorting lists of files/directories. Returns -1 if
* entry1 belongs before entry2, 0 if they are equal, 1 if they should be swapped.
*/
-static int bli_compare(struct direntry *entry1, struct direntry *entry2)
+static int direntry_cmp(struct direntry *entry1, struct direntry *entry2)
{
/* type is equal to stat.st_mode */
@@ -107,122 +107,120 @@ struct BuildDirCtx {
*/
static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
{
+ DIR *dir = opendir(dirname);
+ if (UNLIKELY(dir == NULL)) {
+ printf("%s non-existent directory\n", dirname);
+ return;
+ }
+
struct ListBase dirbase = {NULL, NULL};
int newnum = 0;
- DIR *dir;
+ const struct dirent *fname;
+ bool has_current = false, has_parent = false;
- if ((dir = opendir(dirname)) != NULL) {
- const struct dirent *fname;
- bool has_current = false, has_parent = false;
+ char dirname_with_slash[FILE_MAXDIR + 1];
+ size_t dirname_with_slash_len = BLI_strncpy_rlen(
+ dirname_with_slash, dirname, sizeof(dirname_with_slash) - 1);
- char dirname_with_slash[FILE_MAXDIR + 1];
- size_t dirname_with_slash_len = BLI_strncpy_rlen(
- dirname_with_slash, dirname, sizeof(dirname_with_slash) - 1);
+ if ((dirname_with_slash_len > 0) &&
+ (BLI_path_slash_is_native_compat(dirname_with_slash_len - 1) == false)) {
+ dirname_with_slash[dirname_with_slash_len++] = SEP;
+ dirname_with_slash[dirname_with_slash_len] = '\0';
+ }
- if ((dirname_with_slash_len > 0) &&
- (BLI_path_slash_is_native_compat(dirname_with_slash_len - 1) == false)) {
- dirname_with_slash[dirname_with_slash_len++] = SEP;
- dirname_with_slash[dirname_with_slash_len] = '\0';
+ while ((fname = readdir(dir)) != NULL) {
+ struct dirlink *const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
+ if (dlink != NULL) {
+ dlink->name = BLI_strdup(fname->d_name);
+ if (FILENAME_IS_PARENT(dlink->name)) {
+ has_parent = true;
+ }
+ else if (FILENAME_IS_CURRENT(dlink->name)) {
+ has_current = true;
+ }
+ BLI_addhead(&dirbase, dlink);
+ newnum++;
}
+ }
+
+ if (!has_parent) {
+ char pardir[FILE_MAXDIR];
- while ((fname = readdir(dir)) != NULL) {
+ BLI_strncpy(pardir, dirname, sizeof(pardir));
+ if (BLI_path_parent_dir(pardir) && (BLI_access(pardir, R_OK) == 0)) {
struct dirlink *const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
if (dlink != NULL) {
- dlink->name = BLI_strdup(fname->d_name);
- if (FILENAME_IS_PARENT(dlink->name)) {
- has_parent = true;
- }
- else if (FILENAME_IS_CURRENT(dlink->name)) {
- has_current = true;
- }
+ dlink->name = BLI_strdup(FILENAME_PARENT);
BLI_addhead(&dirbase, dlink);
newnum++;
}
}
+ }
+ if (!has_current) {
+ struct dirlink *const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
+ if (dlink != NULL) {
+ dlink->name = BLI_strdup(FILENAME_CURRENT);
+ BLI_addhead(&dirbase, dlink);
+ newnum++;
+ }
+ }
- if (!has_parent) {
- char pardir[FILE_MAXDIR];
-
- BLI_strncpy(pardir, dirname, sizeof(pardir));
- if (BLI_path_parent_dir(pardir) && (BLI_access(pardir, R_OK) == 0)) {
- struct dirlink *const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
- if (dlink != NULL) {
- dlink->name = BLI_strdup(FILENAME_PARENT);
- BLI_addhead(&dirbase, dlink);
- newnum++;
- }
+ if (newnum) {
+ if (dir_ctx->files) {
+ void *const tmp = MEM_reallocN(dir_ctx->files,
+ (dir_ctx->files_num + newnum) * sizeof(struct direntry));
+ if (tmp) {
+ dir_ctx->files = (struct direntry *)tmp;
}
- }
- if (!has_current) {
- struct dirlink *const dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
- if (dlink != NULL) {
- dlink->name = BLI_strdup(FILENAME_CURRENT);
- BLI_addhead(&dirbase, dlink);
- newnum++;
+ else { /* realloc fail */
+ MEM_freeN(dir_ctx->files);
+ dir_ctx->files = NULL;
}
}
- if (newnum) {
- if (dir_ctx->files) {
- void *const tmp = MEM_reallocN(dir_ctx->files,
- (dir_ctx->files_num + newnum) * sizeof(struct direntry));
- if (tmp) {
- dir_ctx->files = (struct direntry *)tmp;
- }
- else { /* realloc fail */
- MEM_freeN(dir_ctx->files);
- dir_ctx->files = NULL;
- }
- }
+ if (dir_ctx->files == NULL) {
+ dir_ctx->files = (struct direntry *)MEM_mallocN(newnum * sizeof(struct direntry), __func__);
+ }
- if (dir_ctx->files == NULL) {
- dir_ctx->files = (struct direntry *)MEM_mallocN(newnum * sizeof(struct direntry),
- __func__);
- }
+ if (dir_ctx->files) {
+ struct dirlink *dlink = (struct dirlink *)dirbase.first;
+ struct direntry *file = &dir_ctx->files[dir_ctx->files_num];
- if (dir_ctx->files) {
- struct dirlink *dlink = (struct dirlink *)dirbase.first;
- struct direntry *file = &dir_ctx->files[dir_ctx->files_num];
-
- while (dlink) {
- memset(file, 0, sizeof(struct direntry));
- file->relname = dlink->name;
- file->path = BLI_string_joinN(dirname_with_slash, dlink->name);
- if (BLI_stat(file->path, &file->s) != -1) {
- file->type = file->s.st_mode;
- }
- else if (FILENAME_IS_CURRPAR(file->relname)) {
- /* Hack around for UNC paths on windows:
- * does not support stat on '\\SERVER\foo\..', sigh... */
- file->type |= S_IFDIR;
- }
- dir_ctx->files_num++;
- file++;
- dlink = dlink->next;
+ while (dlink) {
+ memset(file, 0, sizeof(struct direntry));
+ file->relname = dlink->name;
+ file->path = BLI_string_joinN(dirname_with_slash, dlink->name);
+ if (BLI_stat(file->path, &file->s) != -1) {
+ file->type = file->s.st_mode;
}
- }
- else {
- printf("Couldn't get memory for dir\n");
- exit(1);
- }
-
- BLI_freelist(&dirbase);
- if (dir_ctx->files) {
- qsort(dir_ctx->files,
- dir_ctx->files_num,
- sizeof(struct direntry),
- (int (*)(const void *, const void *))bli_compare);
+ else if (FILENAME_IS_CURRPAR(file->relname)) {
+ /* Hack around for UNC paths on windows:
+ * does not support stat on '\\SERVER\foo\..', sigh... */
+ file->type |= S_IFDIR;
+ }
+ dir_ctx->files_num++;
+ file++;
+ dlink = dlink->next;
}
}
else {
- printf("%s empty directory\n", dirname);
+ printf("Couldn't get memory for dir\n");
+ exit(1);
}
- closedir(dir);
+ BLI_freelist(&dirbase);
+ if (dir_ctx->files) {
+ qsort(dir_ctx->files,
+ dir_ctx->files_num,
+ sizeof(struct direntry),
+ (int (*)(const void *, const void *))direntry_cmp);
+ }
}
else {
- printf("%s non-existent directory\n", dirname);
+ printf("%s empty directory\n", dirname);
}
+
+ closedir(dir);
}
uint BLI_filelist_dir_contents(const char *dirname, struct direntry **r_filelist)
More information about the Bf-blender-cvs
mailing list