Whenever using eina we need to include it:
In our main function we declare(and initialize) some variables and initialize eina:
int main(int argc, char **argv)
{
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;
int eina_init(void)
Initialize the Eina library.
Definition eina_main.c:244
It's frequently necessary to split a string into its constituent parts, eina_str_split() make's it easy to do so:
for (i = 0; arr[i]; i++)
printf("%s\n", arr[i]);
char ** eina_str_split(const char *string, const char *delimiter, int max_tokens)
Split a string using a delimiter.
Definition eina_str.c:403
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");
printf("%s\n", str);
void eina_str_toupper(char **str)
Uppercase all the characters in range [a-z] in the given string.
Definition eina_str.c:580
printf("%s\n", str);
void eina_str_tolower(char **str)
Lowercase all the characters in range [A-Z] in the given string.
Definition eina_str.c:569
Next we use eina to check if our names
string starts or ends with some values:
printf("Starts with 'Calvin'\n");
printf("Ends with 'sharon'\n");
printf("Has extension 'sharon'\n");
Eina_Bool eina_str_has_extension(const char *str, const char *ext)
Check if the given string has the given extension.
Definition eina_str.c:387
Eina_Bool eina_str_has_suffix(const char *str, const char *suffix)
Check if the given string has the given suffix.
Definition eina_str.c:381
Eina_Bool eina_str_has_prefix(const char *str, const char *prefix)
Check if the given string has the given prefix.
Definition eina_str.c:367
When strings will be used in a terminal(or a number of other places) it necessary to escape certain characters that appear in them:
printf("%s\n", tmp);
char * eina_str_escape(const char *str)
Escape slashes, spaces and apostrophes in strings.
Definition eina_str.c:542
Much as we previously split a string we will now join two strings:
free(tmp);
prologue = malloc(sizeof(char) * 106);
printf("%s\n", prologue);
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)
Join two strings of known length.
Definition eina_str.c:409
With strlcpy() we can copy what portion of the prologue
fits in str
and be sure that it's still NULL terminated:
printf("%s\n", str);
size_t eina_strlcpy(char *dst, const char *src, size_t siz)
Copy a c-string to another.
Definition eina_str.c:304
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+");
printf("%s\n", str);
size_t eina_strlcat(char *dst, const char *src, size_t siz)
Append a c-string.
Definition eina_str.c:336
And then shut eina down and exit:
free(str);
return 0;
}
int eina_shutdown(void)
Shut down the Eina library.
Definition eina_main.c:315