changeset 34627:3482045da618

Revise listRepl(). Improve doxygen comments, move len definition where variable is used and use correct data type, check for NULL replacement, replace strncmp() by gstrncmp() which can handle NULL, replace gstrdup() by strdup() because argument won't be NULL and check whether (re)allocation succeeded.
author ib
date Mon, 13 Feb 2012 16:01:55 +0000
parents fb13b4e8eeb4
children ee78c9c66508
files gui/util/list.c
diffstat 1 files changed, 22 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/gui/util/list.c	Mon Feb 13 15:48:12 2012 +0000
+++ b/gui/util/list.c	Mon Feb 13 16:01:55 2012 +0000
@@ -219,19 +219,29 @@
 }
 
 /**
- * \brief This replaces a string starting with search by replace.
- * If not found, replace is appended.
+ * @brief Replace the first element in list that starts with @a search.
+ *
+ * @note If no such element is found, @a replace will be appended.
+ *
+ * @param list pointer to the char pointer list
+ * @param search element to search
+ * @param replace replacement element
  */
 void listRepl(char ***list, const char *search, const char *replace)
 {
-    int i   = 0;
-    int len = (search ? strlen(search) : 0);
+    int i      = 0;
+    char **org = *list;
+
+    if (!replace)
+        return;
 
     if (*list) {
+        size_t len = (search ? strlen(search) : 0);
+
         for (i = 0; (*list)[i]; i++) {
-            if (search && (strncmp((*list)[i], search, len) == 0)) {
+            if (gstrncmp((*list)[i], search, len) == 0) {
                 free((*list)[i]);
-                (*list)[i] = gstrdup(replace);
+                (*list)[i] = strdup(replace);
                 return;
             }
         }
@@ -240,6 +250,11 @@
     } else
         *list = malloc(2 * sizeof(char *));
 
-    (*list)[i]     = gstrdup(replace);
+    if (!*list) {
+        *list = org;
+        return;
+    }
+
+    (*list)[i]     = strdup(replace);
     (*list)[i + 1] = NULL;
 }