add_languages (string_list_ty *languages, string_list_ty *desired_languages,
const char *line, size_t length)
{
- char *start;
+ const char *start;
/* Split the line by whitespace and build the languages list. */
- for (start = (char *) line; start - line < length; )
+ for (start = line; start - line < length; )
{
- char *p;
+ const char *p;
/* Skip whitespace before the string. */
while (*start == ' ' || *start == '\t')
while (*p != '\0' && *p != ' ' && *p != '\t')
p++;
- *p = '\0';
if (desired_languages == NULL
- || string_list_member (desired_languages, start))
- string_list_append_unique (languages, start);
+ || string_list_member_desc (desired_languages, start, p - start))
+ string_list_append_unique_desc (languages, start, p - start);
start = p + 1;
}
}
{
size_t j;
- /* Do not if the string is already in the list. */
+ /* Do nothing if the string is already in the list. */
for (j = 0; j < slp->nitems; ++j)
if (strcmp (slp->item[j], s) == 0)
return;
slp->item[slp->nitems++] = xstrdup (s);
}
+/* Likewise with a string descriptor as argument. */
+void
+string_list_append_unique_desc (string_list_ty *slp,
+ const char *s, size_t s_len)
+{
+ size_t j;
+
+ /* Do nothing if the string is already in the list. */
+ for (j = 0; j < slp->nitems; ++j)
+ if (strlen (slp->item[j]) == s_len && memcmp (slp->item[j], s, s_len) == 0)
+ return;
+
+ /* Grow the list. */
+ if (slp->nitems >= slp->nitems_max)
+ {
+ slp->nitems_max = slp->nitems_max * 2 + 4;
+ slp->item = (const char **) xrealloc (slp->item,
+ slp->nitems_max
+ * sizeof (slp->item[0]));
+ }
+
+ /* Add a copy of the string to the end of the list. */
+ {
+ char *copy = XNMALLOC (s_len + 1, char);
+ memcpy (copy, s, s_len);
+ copy[s_len] = '\0';
+
+ slp->item[slp->nitems++] = copy;
+ }
+}
+
/* Destroy a list of strings. */
void
return false;
}
+/* Likewise with a string descriptor as argument. */
+bool
+string_list_member_desc (const string_list_ty *slp, const char *s, size_t s_len)
+{
+ size_t j;
+
+ for (j = 0; j < slp->nitems; ++j)
+ if (strlen (slp->item[j]) == s_len && memcmp (slp->item[j], s, s_len) == 0)
+ return true;
+ return false;
+}
+
/* Remove s from the list of strings. Return the removed string or NULL. */
const char *
/* Append a single string to the end of a list of strings, unless it is
already contained in the list. */
extern void string_list_append_unique (string_list_ty *slp, const char *s);
+/* Likewise with a string descriptor as argument. */
+extern void string_list_append_unique_desc (string_list_ty *slp,
+ const char *s, size_t s_len);
/* Destroy a list of strings. */
extern void string_list_destroy (string_list_ty *slp);
/* Return 1 if s is contained in the list of strings, 0 otherwise. */
extern bool string_list_member (const string_list_ty *slp, const char *s);
+/* Likewise with a string descriptor as argument. */
+extern bool string_list_member_desc (const string_list_ty *slp,
+ const char *s, size_t s_len);
/* Remove s from the list of strings. Return the removed string or NULL. */
extern const char * string_list_remove (string_list_ty *slp, const char *s);