about summary refs log tree commit diff
path: root/Src/linklist.c
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2007-06-27 13:56:10 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2007-06-27 13:56:10 +0000
commit4d52b7ebe6d424e46cfddf2c6aa6ba704470f6ac (patch)
treed84ef47a22b39fb8370647c3e9974ea9a7e5b3ec /Src/linklist.c
parent4be5febd94a453c4ed67eb00e43d2107453e6563 (diff)
downloadzsh-4d52b7ebe6d424e46cfddf2c6aa6ba704470f6ac.tar.gz
zsh-4d52b7ebe6d424e46cfddf2c6aa6ba704470f6ac.tar.xz
zsh-4d52b7ebe6d424e46cfddf2c6aa6ba704470f6ac.zip
23670: rationalise some linked list functions
Diffstat (limited to 'Src/linklist.c')
-rw-r--r--Src/linklist.c68
1 files changed, 62 insertions, 6 deletions
diff --git a/Src/linklist.c b/Src/linklist.c
index 26b2c5e0e..b51d88161 100644
--- a/Src/linklist.c
+++ b/Src/linklist.c
@@ -273,28 +273,84 @@ newsizedlist(int size)
     return list;
 }
 
+/*
+ * Return the node whose data is the pointer "dat", else NULL.
+ * Can be used as a boolean test.
+ */
+
 /**/
-mod_export int
-listcontains(LinkList list, void *dat)
+mod_export LinkNode
+linknodebydatum(LinkList list, void *dat)
 {
     LinkNode node;
 
     for (node = firstnode(list); node; incnode(node))
 	if (getdata(node) == dat)
-	    return 1;
+	    return node;
 
-    return 0;
+    return NULL;
 }
 
+/*
+ * Return the node whose data matches the string "dat", else NULL.
+ */
+
 /**/
 mod_export LinkNode
-linknodebydatum(LinkList list, void *dat)
+linknodebystring(LinkList list, char *dat)
 {
     LinkNode node;
 
     for (node = firstnode(list); node; incnode(node))
-	if (getdata(node) == dat)
+	if (!strcmp((char *)getdata(node), dat))
 	    return node;
 
     return NULL;
 }
+
+/*
+ * Convert a linked list whose data elements are strings to
+ * an array.  Memory is off the heap and the elements of the
+ * array are the same elements as the linked list data if copy is
+ * 0, else copied onto the heap.
+ */
+
+/**/
+mod_export char **
+hlinklist2array(LinkList list, int copy)
+{
+    int l = countlinknodes(list);
+    char **ret = (char **) zhalloc((l + 1) * sizeof(char *)), **p;
+    LinkNode n;
+
+    for (n = firstnode(list), p = ret; n; incnode(n), p++) {
+	*p = (char *) getdata(n);
+	if (copy)
+	    *p = dupstring(*p);
+    }
+    *p = NULL;
+
+    return ret;
+}
+
+/*
+ * Convert a linked list whose data elements are strings to
+ * an array.  The result is a permanently allocated, freearrayable
+ * array.
+ */
+
+/**/
+mod_export char **
+zlinklist2array(LinkList list)
+{
+    int l = countlinknodes(list);
+    char **ret = (char **) zalloc((l + 1) * sizeof(char *)), **p;
+    LinkNode n;
+
+    for (n = firstnode(list), p = ret; n; incnode(n), p++) {
+	*p = ztrdup((char *) getdata(n));
+    }
+    *p = NULL;
+
+    return ret;
+}