]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
search-path.c: Improve code style.
authorBruno Haible <bruno@clisp.org>
Sat, 27 Apr 2019 02:40:13 +0000 (04:40 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 27 Apr 2019 02:40:44 +0000 (04:40 +0200)
* gettext-tools/src/search-path.h (get_search_path): Clarify memory allocation.
* gettext-tools/src/search-path.c (struct path_array_ty): Add comment.
(foreach_elements): Fix comment.
(get_search_path): Improve comments. Perform each getenv() only once. Reduce
scope of variables. Fix gcc warning.

gettext-tools/src/search-path.c
gettext-tools/src/search-path.h

index 46e2b90a09573fd43f6b9ab098bf65b068867a8b..0f1bc44469f44847bafbcff25545f4e07f646cb4 100644 (file)
@@ -1,5 +1,5 @@
 /* Routines for locating data files
-   Copyright (C) 2016 Free Software Foundation, Inc.
+   Copyright (C) 2016, 2019 Free Software Foundation, Inc.
 
    This file was written by Daiki Ueno <ueno@gnu.org>, 2016.
 
@@ -37,6 +37,7 @@ typedef void (* foreach_function_ty) (const char *dir, size_t len, void *data);
 struct path_array_ty {
   char **ptr;
   size_t len;
+  /* Transient argument for fill().  */
   const char *sub;
 };
 
@@ -45,7 +46,7 @@ foreach_elements (const char *dirs, foreach_function_ty function, void *data)
 {
   const char *start = dirs;
 
-  /* Count the number of valid elements in GETTEXTDATADIRS.  */
+  /* Count the number of valid elements in DIRS.  */
   while (*start != '\0')
     {
       char *end = strchrnul (start, ':');
@@ -86,8 +87,12 @@ fill (const char *dir, size_t len, void *data)
   array->ptr[array->len++] = name;
 }
 
-/* Find the standard search path for data files.  Returns a NULL
-   terminated list of strings.  The order in the path is as follows:
+/* Find the standard search path for data files.  If SUB is not NULL, append it
+   to each directory.
+   Returns a freshly allocated NULL terminated list of freshly allocated
+   strings.
+
+   The order in the path is as follows:
 
    1. $GETTEXTDATADIR or GETTEXTDATADIR
    2. $GETTEXTDATADIRS
@@ -98,62 +103,80 @@ get_search_path (const char *sub)
 {
   const char *gettextdatadir;
   const char *gettextdatadirs;
+  const char *xdgdatadirs;
   struct path_array_ty array;
-  char *base, *name;
+
+  /* Count how many array elements are needed.  */
   size_t count = 2;
 
   gettextdatadirs = getenv ("GETTEXTDATADIRS");
   if (gettextdatadirs != NULL)
     foreach_elements (gettextdatadirs, increment, &count);
 
-  gettextdatadirs = getenv ("XDG_DATA_DIRS");
-  if (gettextdatadirs != NULL)
-    foreach_elements (gettextdatadirs, increment, &count);
+  xdgdatadirs = getenv ("XDG_DATA_DIRS");
+  if (xdgdatadirs != NULL)
+    foreach_elements (xdgdatadirs, increment, &count);
 
+  /* Allocate the array.  */
   array.ptr = XCALLOC (count + 1, char *);
   array.len = 0;
 
-  gettextdatadir = getenv ("GETTEXTDATADIR");
-  if (gettextdatadir == NULL || gettextdatadir[0] == '\0')
-    /* Make it possible to override the locator file location.  This
-       is necessary for running the testsuite before "make
-       install".  */
-    gettextdatadir = relocate (GETTEXTDATADIR);
+  /* Fill the array.  */
+  {
+    gettextdatadir = getenv ("GETTEXTDATADIR");
+    if (gettextdatadir == NULL || gettextdatadir[0] == '\0')
+      /* Make it possible to override the locator file location.  This
+         is necessary for running the testsuite before "make
+         install".  */
+      gettextdatadir = relocate (GETTEXTDATADIR);
 
-  /* Append element from GETTEXTDATADIR.  */
-  if (sub == NULL)
-    name = xstrdup (gettextdatadir);
-  else
-    name = xconcatenated_filename (gettextdatadir, sub, NULL);
-  array.ptr[array.len++] = name;
-
-  /* Append elements from GETTEXTDATADIRS.  */
-  array.sub = sub;
-  gettextdatadirs = getenv ("GETTEXTDATADIRS");
-  if (gettextdatadirs != NULL)
-    foreach_elements (gettextdatadirs, fill, &array);
-
-  /* Append elements from XDG_DATA_DIRS.  Note that each element needs
-     to have "gettext" suffix.  */
-  if (sub == NULL)
-    array.sub = xstrdup ("gettext");
-  else
-    array.sub = xconcatenated_filename ("gettext", sub, NULL);
-  gettextdatadirs = getenv ("XDG_DATA_DIRS");
-  if (gettextdatadirs != NULL)
-    foreach_elements (gettextdatadirs, fill, &array);
-  free (array.sub);
+    /* Append element from GETTEXTDATADIR.  */
+    {
+      char *name;
+      if (sub == NULL)
+        name = xstrdup (gettextdatadir);
+      else
+        name = xconcatenated_filename (gettextdatadir, sub, NULL);
+      array.ptr[array.len++] = name;
+    }
 
-  /* Append version specific directory.  */
-  base = xasprintf ("%s%s", gettextdatadir, PACKAGE_SUFFIX);
-  if (sub == NULL)
-    name = base;
-  else
+    /* Append elements from GETTEXTDATADIRS.  */
+    if (gettextdatadirs != NULL)
+      {
+        array.sub = sub;
+        foreach_elements (gettextdatadirs, fill, &array);
+      }
+
+    /* Append elements from XDG_DATA_DIRS.  Note that each element needs
+       to have "gettext" suffix.  */
+    if (xdgdatadirs != NULL)
+      {
+        char *combined_sub;
+        if (sub == NULL)
+          combined_sub = xstrdup ("gettext");
+        else
+          combined_sub = xconcatenated_filename ("gettext", sub, NULL);
+
+        array.sub = combined_sub;
+        foreach_elements (xdgdatadirs, fill, &array);
+
+        free (combined_sub);
+      }
+
+    /* Append version specific directory.  */
     {
-      name = xconcatenated_filename (base, sub, NULL);
-      free (base);
+      char *base = xasprintf ("%s%s", gettextdatadir, PACKAGE_SUFFIX);
+      char *name;
+      if (sub == NULL)
+        name = base;
+      else
+        {
+          name = xconcatenated_filename (base, sub, NULL);
+          free (base);
+        }
+      array.ptr[array.len++] = name;
     }
-  array.ptr[array.len++] = name;
+  }
 
   return array.ptr;
 }
index 301143d37c114c1f4b5f3b8e8ef156be43aa9217..39fe88515380b49a62246df3cb2484f7d50377ec 100644 (file)
@@ -1,5 +1,5 @@
 /* Routines for locating data files
-   Copyright (C) 2016 Free Software Foundation, Inc.
+   Copyright (C) 2016, 2019 Free Software Foundation, Inc.
 
    This file was written by Daiki Ueno <ueno@gnu.org>, 2016.
 
@@ -24,9 +24,10 @@ extern "C" {
 #endif
 
 
-/* Find the standard search path for data files.  Returns a NULL
-   terminated list of strings.  If SUB is not NULL, append it to each
-   directory.  */
+/* Find the standard search path for data files.  If SUB is not NULL, append it
+   to each directory.
+   Returns a freshly allocated NULL terminated list of freshly allocated
+   strings.  */
 extern char **get_search_path (const char *sub);