Add list_add_strdup() etc

This commit is contained in:
matt335672
2023-03-06 13:54:22 +00:00
parent a6995f5e58
commit c3d697de16
9 changed files with 159 additions and 53 deletions
+5 -5
View File
@@ -100,7 +100,7 @@ l_file_read_sections(int fd, int max_file_size, struct list *names)
{
if (line_lookup_for_section_name(text, FILE_MAX_LINE_BYTES) != 0)
{
list_add_item(names, (tbus)g_strdup(text));
list_add_strdup(names, text);
}
}
}
@@ -286,7 +286,7 @@ l_file_read_section(int fd, int max_file_size, const char *section,
if (g_strlen(text) > 0)
{
file_split_name_value(text, name, value);
list_add_item(names, (tbus)g_strdup(name));
list_add_strdup(names, name);
if (value[0] == '$')
{
@@ -294,16 +294,16 @@ l_file_read_section(int fd, int max_file_size, const char *section,
if (lvalue != 0)
{
list_add_item(values, (tbus)g_strdup(lvalue));
list_add_strdup(values, lvalue);
}
else
{
list_add_item(values, (tbus)g_strdup(""));
list_add_strdup(values, "");
}
}
else
{
list_add_item(values, (tbus)g_strdup(value));
list_add_strdup(values, value);
}
}
}
+61 -17
View File
@@ -23,6 +23,7 @@
#endif
#include <stdlib.h>
#include <stdarg.h>
#include "arch.h"
#include "os_calls.h"
@@ -232,6 +233,65 @@ list_insert_item(struct list *self, int index, tbus item)
return 1;
}
/******************************************************************************/
int
list_add_strdup(struct list *self, const char *str)
{
int rv;
char *dup;
if (str == NULL)
{
rv = list_add_item(self, (tintptr)str);
}
else if ((dup = g_strdup(str)) == NULL)
{
rv = 0;
}
else
{
rv = list_add_item(self, (tintptr)dup);
if (!rv)
{
g_free(dup);
}
}
return rv;
}
/******************************************************************************/
int
list_add_strdup_multi(struct list *self, ...)
{
va_list ap;
int entry_count = self->count;
const char *s;
int rv = 1;
va_start(ap, self);
while ((s = va_arg(ap, const char *)) != NULL)
{
if (!list_add_strdup(self, s))
{
rv = 0;
break;
}
}
va_end(ap);
if (rv == 0)
{
// Remove the additional items we added
while (self->count > entry_count)
{
list_remove_item(self, self->count - 1);
}
}
return rv;
}
/******************************************************************************/
/* append one list to another using strdup for each item in the list */
/* begins copy at start_index, a zero based index on the source list */
@@ -245,23 +305,7 @@ list_append_list_strdup(struct list *self, struct list *dest, int start_index)
for (index = start_index; index < self->count; index++)
{
const char *item = (const char *)list_get_item(self, index);
char *dup;
if (item == NULL)
{
// This shouldn't really happen, but if it does we'll
// copy the item anyway.
dup = NULL;
}
else
{
dup = g_strdup(item);
if (dup == NULL)
{
rv = 0;
break;
}
}
if (!list_add_item(dest, (tbus)dup))
if (!list_add_strdup(dest, item))
{
rv = 0;
break;
+33
View File
@@ -113,4 +113,37 @@ list_dump_items(struct list *self);
struct list *
split_string_into_list(const char *str, char character);
/**
* As list_add_item() but for a C string
*
* This is a convenience function for a common operation
* @param self List to append to
* @param str String to append
*
* The passed-in string is strdup'd onto the list, so if auto_free
* isn't set, memory leaks will occur.
*
* A NULL pointer will be added as a NULL entry.
*
* @result 0 if any memory allocation failure occurred. In this case
* the list is unchanged.
*/
int
list_add_strdup(struct list *self, const char *str);
/**
* Add multiple strings to a list
*
* This is a convenience function for a common operation
* @param self List to append to
* @param ... Strings to append. Terminate the list with a NULL.
*
* @result 0 if any memory allocation failure occurred. In this case
* the list is unchanged.
*/
int
list_add_strdup_multi(struct list *self, ...);
#endif