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 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;
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]);
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);
printf("%s\n", str);
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");
When strings will be used in a terminal(or a number of other places) it necessary to escape certain characters that appear in them:
Much as we previously split a string we will now join two strings:
free(tmp);
prologue = malloc(sizeof(char) * 106);
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:
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);
And then shut eina down and exit:
free(str);
curr_time = time(NULL);
info = localtime(&curr_time);
printf("Today's Date: %s\n", time_arr);
free(time_arr);
return 0;
}