1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: list.c 769 2007-10-24 00:15:40Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2006-2007 University of Washington
8 * Copyright 2013-2016 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 /*======================================================================
24 #include "../pith/headers.h"
25 #include "../pith/list.h"
26 #include "../pith/string.h"
35 * parse_list - takes a comma delimited list of "count" elements and
36 * returns an array of pointers to each element neatly
37 * malloc'd in its own array. Any errors are returned
38 * in the string pointed to by "error"
40 * If remove_surrounding_double_quotes is set, then double quotes around
41 * each element of the list are removed. We can't do this for all list
42 * variables. For example, incoming folders look like
44 * in the config file. Each of those may be quoted separately.
46 * NOTE: only recognizes escaped quotes
49 parse_list(char *list
, int count
, int flags
, char **error
)
51 char **lvalue
, *p2
, *p3
, *p4
;
53 int remove_surrounding_double_quotes
;
54 int commas_may_be_escaped
;
56 remove_surrounding_double_quotes
= (flags
& PL_REMSURRQUOT
);
57 commas_may_be_escaped
= (flags
& PL_COMMAQUOTE
);
59 lvalue
= (char **) fs_get((count
+1) * sizeof(char *));
61 while(*list
){ /* pick elements from list */
62 p2
= list
; /* find end of element */
64 if(*p2
== '"') /* ignore ',' if quoted */
65 was_quoted
= (was_quoted
) ? 0 : 1 ;
67 if(*p2
== '\\' && *(p2
+1) == '"')
68 p2
++; /* preserve escaped quotes, too */
70 if((*p2
== ',' && !was_quoted
) || *p2
== '\0')
73 if(commas_may_be_escaped
&& *p2
== '\\' && *(p2
+1) == ',')
79 if(was_quoted
){ /* unbalanced quotes! */
81 *error
= "Unbalanced quotes";
87 * if element found, eliminate trailing
88 * white space and tie into variable list
91 for(p3
= p2
- 1; isspace((unsigned char) *p3
) && list
< p3
; p3
--)
94 p4
= fs_get(((p3
- list
) + 2) * sizeof(char));
101 if(remove_surrounding_double_quotes
)
102 removing_double_quotes(lvalue
[count
]);
107 if(*(list
= p2
) != '\0'){ /* move to beginning of next val */
108 while(*list
== ',' || isspace((unsigned char)*list
))
113 lvalue
[count
] = NULL
; /* tie off pointer list */
119 * Free array of string pointers and associated strings
121 * Args: list -- array of char *'s
124 free_list_array(char ***list
)
129 for(p
= *list
; *p
; p
++)
130 fs_give((void **) p
);
132 fs_give((void **) list
);
138 * Copy array of string pointers and associated strings
140 * Args: list -- array of char *'s
142 * Returns: Allocated array of string pointers and allocated copies of strings.
143 * Caller should free the list array when done.
146 copy_list_array(char **list
)
149 char **p
, **ret_list
= NULL
;
155 p
= ret_list
= (char **)fs_get((cnt
+1) * sizeof(char *));
156 memset((void *) ret_list
, 0, (cnt
+1) * sizeof(char *));
158 for(i
=0; list
[i
]; i
++, p
++)
159 *p
= cpystr(list
[i
]);
170 equal_list_arrays(char **list1
, char **list2
)
176 if(!*list2
|| strcmp(*list1
, *list2
) != 0)
183 if(*list1
== NULL
&& *list2
== NULL
)