Eina_Strbuf example

First thing always is including Eina:

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

Next we initialize eina and create a string buffer to play with:

int main(int argc, char **argv)
{
buf = eina_strbuf_new();
int eina_init(void)
Initialize the Eina library.
Definition eina_main.c:244
Eina_Strbuf * eina_strbuf_new(void)
Create a new string buffer.
struct _Eina_Strbuf Eina_Strbuf
Type for a string buffer.
Definition eina_strbuf.h:65

Here you can see two different ways of creating a buffer with the same contents. We could create them in simpler ways, but this gives us an opportunity to demonstrate several functions in action:

eina_strbuf_append_length(buf, "buffe", 5);
printf("%s\n", eina_strbuf_string_get(buf));
printf("%s\n", eina_strbuf_string_get(buf));
Eina_Bool eina_strbuf_insert_escaped(Eina_Strbuf *buf, const char *str, size_t pos)
Insert an escaped string to a buffer, reallocating as necessary.
Eina_Bool eina_strbuf_append_char(Eina_Strbuf *buf, char c)
Append a character to a string buffer, reallocating as necessary.
const char * eina_strbuf_string_get(const Eina_Strbuf *buf)
Retrieve a pointer to the contents of a string buffer.
void eina_strbuf_reset(Eina_Strbuf *buf)
Reset a string buffer.
Eina_Bool eina_strbuf_append_length(Eina_Strbuf *buf, const char *str, size_t length)
Append a string of exact length to a buffer, reallocating as necessary.
eina_strbuf_append_escaped(buf, "my buffer");
printf("%s\n", eina_strbuf_string_get(buf));
Eina_Bool eina_strbuf_append_escaped(Eina_Strbuf *buf, const char *str)
Append an escaped string to a buffer, reallocating as necessary.

Next we use the printf family of functions to create a formated string, add, remove and replace some content:

eina_strbuf_append_printf(buf, "%s%c", "buffe", 'r');
eina_strbuf_insert_printf(buf, " %s: %d", 6, "length", eina_strbuf_length_get(buf));
printf("%s\n", eina_strbuf_string_get(buf));
Eina_Bool eina_strbuf_append_printf(Eina_Strbuf *buf, const char *fmt,...) EINA_PRINTF(2
Append a string to a buffer, reallocating as necessary.
Eina_Bool eina_strbuf_insert_printf(Eina_Strbuf *buf, const char *fmt, size_t pos,...) EINA_PRINTF(2
Insert a string to a buffer, reallocating as necessary.
size_t eina_strbuf_length_get(const Eina_Strbuf *buf)
Retrieve the length of the string buffer content.
eina_strbuf_remove(buf, 0, 7);
printf("%s\n", eina_strbuf_string_get(buf));
Eina_Bool eina_strbuf_remove(Eina_Strbuf *buf, size_t start, size_t end)
Remove a slice of the given string buffer.
eina_strbuf_replace_all(buf, "length", "size");
printf("%s\n", eina_strbuf_string_get(buf));
int eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with)
Replace all strings with an other string.
Definition eina_strbuf_common.c:801

Once done we free our string buffer, shut down Eina and end the application:

return 0;
}
int eina_shutdown(void)
Shut down the Eina library.
Definition eina_main.c:315
void eina_strbuf_free(Eina_Strbuf *buf)
Free a string buffer.