Removing array elements

Just the usual includes:

#include <stdio.h>
#include <string.h>
#include <Eina.h>

This is the callback we are going to use to decide which strings stay on the array and which will be removed. We use something simple, but this can be as complex as you like:

Eina_Bool keep(void *data, void *gdata EINA_UNUSED)
{
if (strlen((const char*)data) <= 5)
return EINA_TRUE;
return EINA_FALSE;
}

This is the same code we used before to populate the list with the slight difference of not using strdup:

int
main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
const char* strs[] = {
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourtenn", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen", "twenty"
};
const char* strings[] = {
"helo", "hera", "starbuck", "kat", "boomer",
"hotdog", "longshot", "jammer", "crashdown", "hardball",
"duck", "racetrack", "apolo", "husker", "freaker",
"skulls", "bulldog", "flat top", "hammerhead", "gonzo"
};
Eina_Array *array;
const char *item;
unsigned int i;
array = eina_array_new(10);
for (i = 0; i < 20; i++)
eina_array_push(array, strs[i]);

So we have added all our elements to the array, but it turns out that is not the elements we wanted, so let's empty the array and add the correct strings:

for (i = 0; i < 20; i++)
eina_array_push(array, strings[i]);

It seems we made a little mistake in one of our strings so we need to replace it, here is how:

eina_array_data_set(array, 17, "flattop");

Now that there is a populated array we can remove elements from it easily:

eina_array_remove(array, keep, NULL);

And check that the elements were actually removed:

EINA_ARRAY_ITER_NEXT(array, i, item, iterator)
printf("item #%u: %s\n", i, item);

Since this time we didn't use strdup we don't need to free each string:

return 0;
}

The full source code can be found in the examples folder in the eina_array_02.c file.

eina_array_clean
static void eina_array_clean(Eina_Array *array) EINA_ARG_NONNULL(1)
Clears an array of its elements, without deallocating memory.
eina_array_remove
EAPI Eina_Bool eina_array_remove(Eina_Array *array, Eina_Bool(*keep)(void *data, void *gdata), void *gdata) EINA_ARG_NONNULL(1
Rebuilds an array by specifying the data to keep.
EINA_UNUSED
#define EINA_UNUSED
Definition: eina_types.h:339
Eina.h
Eina Utility library.
EINA_FALSE
#define EINA_FALSE
Definition: eina_types.h:533
eina_init
EAPI int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:279
EINA_ARRAY_ITER_NEXT
#define EINA_ARRAY_ITER_NEXT(array, index, item, iterator)
Iterates through an array's elements.
Definition: eina_array.h:507
_Eina_Array
Definition: eina_array.h:229
eina_array_free
EAPI void eina_array_free(Eina_Array *array)
Frees an array.
Definition: eina_array.c:295
eina_array_push
EAPI Eina_Bool static Eina_Bool eina_array_push(Eina_Array *array, const void *data) EINA_ARG_NONNULL(1
Appends a data item to an array.
eina_shutdown
EAPI int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:350
EINA_TRUE
#define EINA_TRUE
Definition: eina_types.h:539
eina_array_new
EAPI Eina_Array * eina_array_new(unsigned int step)
Creates a new array.
Definition: eina_array.c:276
Eina_Array_Iterator
void ** Eina_Array_Iterator
Definition: eina_array.h:222
Eina_Bool
unsigned char Eina_Bool
Definition: eina_types.h:527
eina_array_data_set
static void eina_array_data_set(const Eina_Array *array, unsigned int idx, const void *data) EINA_ARG_NONNULL(1)
Sets the data at a given position in an array.