Eina_Strbuf example

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

Eina_Strbuf example

First thing always is including Eina:

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

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

int main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
Eina_Strbuf *buf, *substr;
eina_init();
buf = eina_strbuf_new();
eina_init
EINA_API int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:291
eina_strbuf_new
EINA_API Eina_Strbuf * eina_strbuf_new(void)
Creates a new string buffer.
EINA_UNUSED
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
_Eina_Strbuf
String buffer to facilitate string operations.
Definition: eina_strbuf_common.h:15

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);
eina_strbuf_append_char(buf, 'R');
printf("%s\n", eina_strbuf_string_get(buf));
eina_strbuf_tolower(buf);
printf("%s\n", eina_strbuf_string_get(buf));
substr = eina_strbuf_substr_get(buf, 3, 2);
printf("%s\n", eina_strbuf_string_get(substr));
eina_strbuf_free(substr);
eina_strbuf_insert_escaped(buf, "my ", 0);
printf("%s\n", eina_strbuf_string_get(buf));
eina_strbuf_reset(buf);
eina_strbuf_reset
EINA_API void eina_strbuf_reset(Eina_Strbuf *buf)
Resets a string buffer.
eina_strbuf_append_char
EINA_API Eina_Bool eina_strbuf_append_char(Eina_Strbuf *buf, char c)
Appends a character to a string buffer, reallocating as necessary.
eina_strbuf_free
EINA_API void eina_strbuf_free(Eina_Strbuf *buf)
Frees a string buffer.
eina_strbuf_append_length
EINA_API Eina_Bool eina_strbuf_append_length(Eina_Strbuf *buf, const char *str, size_t length)
Appends a string of exact length to a buffer, reallocating as necessary.
eina_strbuf_tolower
EINA_API void eina_strbuf_tolower(Eina_Strbuf *buf)
Converts the string to lower case.
Definition: eina_strbuf.c:197
eina_strbuf_insert_escaped
EINA_API Eina_Bool eina_strbuf_insert_escaped(Eina_Strbuf *buf, const char *str, size_t pos)
Inserts an escaped string into a buffer, reallocating as necessary.
eina_strbuf_substr_get
EINA_API Eina_Strbuf * eina_strbuf_substr_get(Eina_Strbuf *buf, size_t pos, size_t len)
Obtains substring from a source string buffer.
Definition: eina_strbuf.c:205
eina_strbuf_string_get
EINA_API const char * eina_strbuf_string_get(const Eina_Strbuf *buf)
Retrieves a pointer to the contents of a string buffer.
eina_strbuf_append_escaped(buf, "my buffer");
printf("%s\n", eina_strbuf_string_get(buf));
eina_strbuf_reset(buf);
eina_strbuf_append_escaped
EINA_API Eina_Bool eina_strbuf_append_escaped(Eina_Strbuf *buf, const char *str)
Appends an escaped string to a buffer, reallocating as necessary.

Next we use the printf family of functions to create a formatted 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", (int)eina_strbuf_length_get(buf));
printf("%s\n", eina_strbuf_string_get(buf));
eina_strbuf_length_get
EINA_API size_t eina_strbuf_length_get(const Eina_Strbuf *buf)
Retrieves the length of the string buffer's content.
eina_strbuf_remove(buf, 0, 7);
printf("%s\n", eina_strbuf_string_get(buf));
eina_strbuf_remove
EINA_API Eina_Bool eina_strbuf_remove(Eina_Strbuf *buf, size_t start, size_t end)
Removes a section from the given string buffer.
eina_strbuf_replace_all(buf, "length", "size");
printf("%s\n", eina_strbuf_string_get(buf));
eina_strbuf_replace_all
EINA_API int eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with)
Replaces all matching substrings with another string.
Definition: eina_strbuf_common.c:1039

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

eina_strbuf_free(buf);
buf = eina_strbuf_manage_read_only_new_length("Example string", 14);
printf("%s\n", eina_strbuf_string_get(buf));
eina_strbuf_append_char(buf, '.');
printf("%s\n", eina_strbuf_string_get(buf));
eina_strbuf_append_length(buf, "Another string.", 15);
printf("%s\n", eina_strbuf_string_get(buf));
eina_strbuf_free(buf);
eina_shutdown();
return 0;
}
eina_shutdown
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:379
eina_strbuf_manage_read_only_new_length
EINA_API Eina_Strbuf * eina_strbuf_manage_read_only_new_length(const char *str, size_t length)
Creates a new string buffer using the passed string.