Commit 7fa88586 authored by rswindell's avatar rswindell
Browse files

New function: strListModifyEach(), iterates through a string list, calling

a caller-supplied function (callback) which can modify each string or leave
the string in tact by returning NULL. Shrinking strings is trivial while
expanding strings (making them bigger) is a little more involved, probably
using the cbdata to pass a temporary buffer around to store each expanded
string or returning a new globally-valid pointer.
Removed the malloc.h #include as we haven't used alloca() here for a while.
parent 166cd415
......@@ -37,9 +37,6 @@
#include <stdlib.h> /* malloc and qsort */
#include <string.h> /* strtok */
#if defined(_WIN32)
#include <malloc.h> /* alloca() on Win32 */
#include "genwrap.h" /* stricmp */
#include "str_list.h"
......@@ -201,6 +198,22 @@ char* DLLCALL strListReplace(const str_list_t list, size_t index, const char* st
size_t DLLCALL strListModifyEach(const str_list_t list, char*(modify(size_t, char*, void*)), void* cbdata)
size_t i;
for(i = 0; list[i] != NULL; i++) {
char* str = modify(i, list[i], cbdata);
if(str == NULL || str == list[i]) // Same old pointer (or NULL), no realloc() needed
char* p = realloc(list[i], strlen(str) + 1);
if(p == NULL)
list[i] = p;
strcpy(p, str);
return i;
BOOL DLLCALL strListSwap(const str_list_t list, size_t index1, size_t index2)
char* tmp;
......@@ -87,6 +87,16 @@ DLLEXPORT BOOL DLLCALL strListDelete(str_list_t*, size_t index);
/* Replace a string at a specific index */
DLLEXPORT char* DLLCALL strListReplace(const str_list_t, size_t index, const char* str);
/* Call a modification callback function for each string in a list */
/* and replace each string with the result of the modification callback. */
/* If the modification callback function returns NULL, the string is not modified. */
/* If the modification callback function returns the same string item pointer it was passed, the string is not realloc'd. */
/* If the modification callback function needs to expand the string item (make it bigger), it must return a new valid pointer */
/* (possibly, the cbdata, a global array or a static automatic variable). Since the new pointer is not free'd here, it should */
/* not be dynamically allocated by the callback function. */
/* Returns the number of modified strings (normally, the list count unless there was a failure) */
DLLEXPORT size_t DLLCALL strListModifyEach(const str_list_t list, char*(modify(size_t index, char* str, void*)), void* cbdata);
/* Swap the strings at index1 and index2 */
DLLEXPORT BOOL DLLCALL strListSwap(const str_list_t, size_t index1, size_t index2);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment