Just the usual includes:
#include <stdio.h>
#include <string.h>
This 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:
{
if (strlen((const char*)data) <= 5)
}
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition eina_types.h:299
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition eina_types.h:293
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:287
This is the same code we used before to populate the list with the slight difference of not using strdup:
int
main(int argc, char **argv)
{
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"
};
const char *item;
unsigned int i;
for (i = 0; i < 20; i++)
eina_array_push(array, strs[i]);
void ** Eina_Array_Iterator
Type for an iterator on arrays, used with EINA_ARRAY_ITER_NEXT.
Definition eina_array.h:226
struct _Eina_Array Eina_Array
Type for a generic vector.
Definition eina_array.h:220
Eina_Array * eina_array_new(unsigned int step)
Create a new array.
Definition eina_array.c:265
int eina_init(void)
Initialize the Eina library.
Definition eina_main.c:244
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]);
static void eina_array_clean(Eina_Array *array)
Clean an array.
It seems we made a little mistake in one of our strings so we need to replace it, here is how:
static void eina_array_data_set(const Eina_Array *array, unsigned int idx, const void *data)
Set the data at a given position in an array.
Now that there is a populated array we can remove elements from it easily:
Eina_Bool eina_array_remove(Eina_Array *array, Eina_Bool(*keep)(void *data, void *gdata), void *gdata)
Rebuild an array by specifying the data to keep.
Definition eina_array.c:340
And check that the elements were actually removed:
printf("item #%d: %s\n", i, item);
#define EINA_ARRAY_ITER_NEXT(array, index, item, iterator)
Macro to iterate over an array easily.
Definition eina_array.h:431
Since this time we didn't use strdup we don't need to free each string:
return 0;
}
void eina_array_free(Eina_Array *array)
Free an array.
Definition eina_array.c:289
int eina_shutdown(void)
Shut down the Eina library.
Definition eina_main.c:315
The full source code can be found on the examples folder on the eina_array_02.c file.