2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2005
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 build a null terminated list of strings from a input string and a
25 separator list. The separator list must contain characters less than
26 or equal to 0x2f for this to work correctly on multi-byte strings
28 const char **str_list_make(TALLOC_CTX
*mem_ctx
, const char *string
, const char *sep
)
31 const char **ret
= NULL
;
37 ret
= talloc_array(mem_ctx
, const char *, 1);
42 while (string
&& *string
) {
43 size_t len
= strcspn(string
, sep
);
47 string
+= strspn(string
, sep
);
51 ret2
= talloc_realloc(mem_ctx
, ret
, const char *, num_elements
+2);
58 ret
[num_elements
] = talloc_strndup(ret
, string
, len
);
59 if (ret
[num_elements
] == NULL
) {
68 ret
[num_elements
] = NULL
;
73 /* join a list back to one string */
74 char *str_list_join(TALLOC_CTX
*mem_ctx
, const char **list
, char seperator
)
80 return talloc_strdup(mem_ctx
, "");
82 ret
= talloc_strdup(mem_ctx
, list
[0]);
84 for (i
= 1; list
[i
]; i
++) {
85 ret
= talloc_asprintf_append(ret
, "%c%s", seperator
, list
[i
]);
93 return the number of elements in a string list
95 size_t str_list_length(const char **list
)
98 for (ret
=0;list
&& list
[ret
];ret
++) /* noop */ ;
106 const char **str_list_copy(TALLOC_CTX
*mem_ctx
, const char **list
)
109 const char **ret
= talloc_array(mem_ctx
, const char *, str_list_length(list
)+1);
110 if (ret
== NULL
) return NULL
;
112 for (i
=0;list
&& list
[i
];i
++) {
113 ret
[i
] = talloc_strdup(ret
, list
[i
]);
114 if (ret
[i
] == NULL
) {
124 Return true if all the elements of the list match exactly.
126 BOOL
str_list_equal(const char **list1
, const char **list2
)
130 if (list1
== NULL
|| list2
== NULL
) {
131 return (list1
== list2
);
134 for (i
=0;list1
[i
] && list2
[i
];i
++) {
135 if (strcmp(list1
[i
], list2
[i
]) != 0) {
139 if (list1
[i
] || list2
[i
]) {
147 add an entry to a string list
149 const char **str_list_add(const char **list
, const char *s
)
151 size_t len
= str_list_length(list
);
154 ret
= talloc_realloc(NULL
, list
, const char *, len
+2);
155 if (ret
== NULL
) return NULL
;
157 ret
[len
] = talloc_strdup(ret
, s
);
158 if (ret
[len
] == NULL
) return NULL
;
166 remove an entry from a string list
168 void str_list_remove(const char **list
, const char *s
)
172 for (i
=0;list
[i
];i
++) {
173 if (strcmp(list
[i
], s
) == 0) break;
175 if (!list
[i
]) return;
184 return True if a string is in a list
186 BOOL
str_list_check(const char **list
, const char *s
)
190 for (i
=0;list
[i
];i
++) {
191 if (strcmp(list
[i
], s
) == 0) return True
;
197 return True if a string is in a list, case insensitively
199 BOOL
str_list_check_ci(const char **list
, const char *s
)
203 for (i
=0;list
[i
];i
++) {
204 if (strcasecmp(list
[i
], s
) == 0) return True
;