Eina String example

Whenever using eina we need to include it:

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

In our main function we declare (and initialize) some variables and initialize eina:

int main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
char *names = "Calvin;Leoben;D'anna;Simon;Doral;Six;Daniel;Sharon";
char *str;
char *tmp;
char *prologue;
char *part1 = "The Cylons were created by man. They evolved. They rebelled.";
char *part2 = "There are many copies. And they have a plan.";
char **arr;
int i;
char *time_arr;
time_t curr_time;
struct tm *info;

It's frequently necessary to split a string into its constituent parts, eina_str_split() make's it easy to do so:

arr = eina_str_split(names, ";", 0);
for (i = 0; arr[i]; i++)
printf("%s\n", arr[i]);

Another common need is to make a string uppercase or lowercase, so let's create a string and make it uppercase and then make it lowercase again:

free(arr[0]);
free(arr);
str = malloc(sizeof(char) * 4);
strcpy(str, "bsd");
eina_str_toupper((char **)&str);
printf("%s\n", str);
printf("%s\n", str);

Next we use eina to check if our names string starts or ends with some values:

if (eina_str_has_prefix(names, "Calvin"))
printf("Starts with 'Calvin'\n");
if (eina_str_has_suffix(names, "sharon"))
printf("Ends with 'sharon'\n");
if (eina_str_has_extension(names, "sharon"))
printf("Has extension 'sharon'\n");

When strings will be used in a terminal (or a number of other places) it necessary to escape certain characters that appear in them:

tmp = eina_str_escape("They'll start going ripe on us pretty soon.");
printf("%s\n", tmp);

Much as we previously split a string we will now join two strings:

free(tmp);
prologue = malloc(sizeof(char) * 106);
eina_str_join_len(prologue, 106, ' ', part1, strlen(part1), part2, strlen(part2));
printf("%s\n", prologue);

With strlcpy() we can copy what portion of the prologue fits in str and be sure that it's still NULL terminated:

eina_strlcpy(str, prologue, 4);
printf("%s\n", str);

Since we are done with prologue and str we should free them:

free(prologue);
free(str);

Finally we see strlcat in action:

str = malloc(sizeof(char) * 14);
sprintf(str, "%s", "cylons+");
eina_strlcat(str, "humans", 14);
printf("%s\n", str);

And then shut eina down and exit:

free(str);
curr_time = time(NULL);
info = localtime(&curr_time);
time_arr = eina_strftime("%d/%m/%Y", info);
printf("Today's Date: %s\n", time_arr);
free(time_arr);
return 0;
}
eina_strlcat
EAPI size_t EAPI size_t eina_strlcat(char *dst, const char *src, size_t siz) EINA_ARG_NONNULL(1
Appends a c-string.
EINA_UNUSED
#define EINA_UNUSED
Definition: eina_types.h:339
eina_str_has_extension
EAPI Eina_Bool eina_str_has_extension(const char *str, const char *ext) EINA_PURE EINA_ARG_NONNULL(1
Checks if the given string has the given extension.
eina_str_tolower
void eina_str_tolower(char **str)
Lowercases all the characters in range [A-Z] in the given string.
Definition: eina_str.c:698
eina_str_join_len
EAPI size_t eina_str_join_len(char *dst, size_t size, char sep, const char *a, size_t a_len, const char *b, size_t b_len) EINA_ARG_NONNULL(1
Joins two strings of known length.
Eina.h
Eina Utility library.
eina_init
EAPI int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:279
eina_str_has_suffix
EAPI Eina_Bool eina_str_has_suffix(const char *str, const char *suffix) EINA_PURE EINA_ARG_NONNULL(1
Checks if the given string has the given suffix.
eina_str_split
EAPI char ** eina_str_split(const char *string, const char *delimiter, int max_tokens) EINA_ARG_NONNULL(1
Splits a string using a delimiter.
eina_shutdown
EAPI int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:350
eina_strftime
char * eina_strftime(const char *format, const struct tm *tm)
Creates and update the buffer based on strftime output.
Definition: eina_str.c:383
eina_strlcpy
EAPI size_t eina_strlcpy(char *dst, const char *src, size_t siz) EINA_ARG_NONNULL(1
Copies a c-string to another.
eina_str_toupper
void eina_str_toupper(char **str)
Uppercases all the characters in range [a-z] in the given string.
Definition: eina_str.c:709
eina_str_escape
char * eina_str_escape(const char *str)
Escapes slashes, spaces and apostrophes in strings.
Definition: eina_str.c:648
eina_str_has_prefix
EAPI size_t EAPI size_t EAPI Eina_Bool eina_str_has_prefix(const char *str, const char *prefix) EINA_PURE EINA_ARG_NONNULL(1
Checks if the given string has the given prefix.