Use babl utility to iterate over directory contents
authorLuca Bacci <luca.bacci982@gmail.com>
Thu, 29 Dec 2022 17:37:44 +0000 (18:37 +0100)
committerØyvind "pippin" Kolås <pippin@gimp.org>
Tue, 3 Jan 2023 10:33:37 +0000 (10:33 +0000)
babl/babl-extension.c

index 8f06fcdab247b4c18a2ecb75943576f62fb5c95b..0d36eeff57006401a3cbdf7fc2cbc5db6846fc36 100644 (file)
@@ -260,45 +260,53 @@ babl_extension_load (const char *path)
     }
 }
 
+struct dir_foreach_ctx
+{
+  const char **exclusion_patterns;
+};
+
 static void
-babl_extension_load_dir (const char *base_path,
-                         const char **exclusion_patterns)
+dir_foreach (const char *base_path,
+             const char *entry,
+             void       *user_data)
 {
-  DIR *dir;
+  struct dir_foreach_ctx *ctx = (struct dir_foreach_ctx*) user_data;
 
-  if ((dir = opendir (base_path)))
+  if (entry[0] != '.')
     {
-      struct  dirent *dentry;
+      char       *path = NULL;
+      char       *extension;
 
-      while ((dentry = readdir (dir)) != NULL)
+      path = babl_strcat (path, base_path);
+      path = babl_strcat (path, BABL_DIR_SEPARATOR);
+      path = babl_strcat (path, entry);
+
+      if ((extension = strrchr (entry, '.')) != NULL &&
+          !strcmp (extension, SHREXT))
         {
-          if (dentry->d_name[0] != '.')
-            {
-              char       *path = NULL;
-              char       *extension;
-
-              path = babl_strcat (path, base_path);
-              path = babl_strcat (path, BABL_DIR_SEPARATOR);
-              path = babl_strcat (path, dentry->d_name);
-
-              if ((extension = strrchr (dentry->d_name, '.')) != NULL &&
-                  !strcmp (extension, SHREXT))
-                {
-                  int excluded = 0;
-                  for (int i = 0; exclusion_patterns[i]; i++)
-                    if (strstr (path, exclusion_patterns[i]))
-                      excluded = 1;
-                  if (!excluded)
-                    babl_extension_load (path);
-                }
-
-              babl_free (path);
-            }
+          int excluded = 0;
+          for (int i = 0; ctx->exclusion_patterns[i]; i++)
+            if (strstr (path, ctx->exclusion_patterns[i]))
+              excluded = 1;
+          if (!excluded)
+            babl_extension_load (path);
         }
-      closedir (dir);
+
+      babl_free (path);
     }
 }
 
+static void
+babl_extension_load_dir (const char *base_path,
+                         const char **exclusion_patterns)
+{
+  struct dir_foreach_ctx ctx;
+
+  ctx.exclusion_patterns = exclusion_patterns;
+
+  _babl_dir_foreach (base_path, dir_foreach, &ctx);
+}
+
 static char *
 expand_path (char *path)
 {