eina_tmpstr_ppage

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

eina_tmpstr_ppage

Eina tmpstr is intended for being able to conveniently pass strings back to a calling parent without having to use single static buffers (which don't work with multiple threads or when returning multiple times as parameters to a single function.

The traditional way to "return" a string in C is either to provide a buffer as a parameter to return it in, return a pointer to a single static buffer, which has issues, or return a duplicated string. All cases are inconvenient and return special handling. This is intended to make this easier. Now you can do something like this:

Eina_Tmpstr *my_homedir(void) {
return eina_tmpstr_add(eina_environment_home_get());
}
Eina_Tmpstr *my_tmpdir(void) {
return eina_tmpstr_add(getenv("TMP"));
}
void my_movefile(Eina_Tmpstr *src, Eina_Tmpstr *dst) {
rename(src, dst);
eina_tmpstr_del(src);
eina_tmpstr_del(dst);
}
char buf[500];
my_movefile(my_homedir(), my_tmpdir());
my_movefile("/tmp/file", "/tmp/newname");
my_movefile(my_homedir(), "/var/tmp");
snprintf(buf, sizeof(buf), "/tmp/%i.file", rand());
my_movefile("/tmp.file", buf);
eina_tmpstr_del
EINA_API void eina_tmpstr_del(Eina_Tmpstr *tmpstr)
Deletes the temporary string if it is one, or ignore it if it is not.
Definition: eina_tmpstr.c:125
eina_tmpstr_add
EINA_API Eina_Tmpstr * eina_tmpstr_add(const char *str)
Adds a new temporary string based on the input string.
Definition: eina_tmpstr.c:115
Eina_Tmpstr
const char Eina_Tmpstr
Interchangeable with "const char *" but still a good visual hint for the purpose.
Definition: eina_tmpstr.h:121
eina_environment_home_get
EINA_API const char * eina_environment_home_get(void)
Returns the content of the environment referred by HOME on this system.
Definition: eina_util.c:52

Notice that you can interchange standard C strings (static ones or even generated buffers) with tmpstrings. The Eina_Tmpstr type is merely a type marker letting you know that the function will clean up those strings after use, and it is totally interchangeable with const char.