diff options
Diffstat (limited to 'Src/linklist.c')
-rw-r--r-- | Src/linklist.c | 68 |
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; +} |