eina: improve usability of Eina_Tmpstr.

Added eina_tmpstr_add_length and eina_tmpstr_strlen.
This commit is contained in:
Cedric BAIL 2013-03-14 20:36:56 +09:00
parent 4ab02d7f6b
commit 81f739da84
3 changed files with 89 additions and 16 deletions

22
NEWS
View File

@ -7,11 +7,18 @@ Changes since 1.7.0:
Additions:
* Add multiple font draws support to engines
* Add DOCTYPE children parsing in eina_simple_xml
* Add eina_barrier thread API
* Add eina_tmpstr_add() and eina_tmpstr_del()
* Add eina_thread API
* Add eina_list_last_data_get
* eina :
- Add DOCTYPE children parsing in eina_simple_xml
- Add eina_barrier thread API
- Add eina_tmpstr_add(), eina_tmpstr_del(), eina_tmpstr_add_length() and
eina_tmpstr_strlen().
- Add eina_thread API
- Add eina_list_last_data_get
- Add eina_xattr_fd_get(), eina_xattr_fd_set(),
eina_xattr_del(), eina_xattr_fd_del(), eina_xattr_copy() and
eina_xattr_fd_copy()
- Add eina_stringshare_refplace()
- Add eina_file_copy()
* Add Cserve2 scalecache support
* ecore_x:
- Add window profile support.
@ -58,11 +65,6 @@ Additions:
- Add EVAS_GL_DIRECT_MEM_OPT to enable on-demand fallback memory allocation policy for EvasGL direct rendering.
- Add engine specific alpha_get.
* Add ecore_audio API
* Added eina_xattr_fd_get(), eina_xattr_fd_set(),
eina_xattr_del(), eina_xattr_fd_del(), eina_xattr_copy() and
eina_xattr_fd_copy()
* added eina_stringshare_refplace()
* Added eina_file_copy()
* Add eet_mmap.
* added eet_data_descriptor_name_get()
* Add eio_eet_sync symbols.

View File

@ -45,6 +45,7 @@ typedef struct _Str Str;
struct _Str
{
size_t length;
Str *next;
char *str;
};
@ -67,15 +68,14 @@ eina_tmpstr_shutdown(void)
}
EAPI Eina_Tmpstr *
eina_tmpstr_add(const char *str)
eina_tmpstr_add_length(const char *str, size_t length)
{
Str *s;
int len;
if (!str) return NULL;
len = strlen(str);
s = malloc(sizeof(Str) + len + 1);
if (!str || !length) return NULL;
s = malloc(sizeof(Str) + length + 1);
if (!s) return NULL;
s->length = length;
s->str = ((char *)s) + sizeof(Str);
strcpy(s->str, str);
eina_lock_take(&_mutex);
@ -85,6 +85,16 @@ eina_tmpstr_add(const char *str)
return s->str;
}
EAPI Eina_Tmpstr *
eina_tmpstr_add(const char *str)
{
size_t len;
if (!str) return NULL;
len = strlen(str);
return eina_tmpstr_add_length(str, len);
}
EAPI void
eina_tmpstr_del(Eina_Tmpstr *tmpstr)
{
@ -104,3 +114,24 @@ eina_tmpstr_del(Eina_Tmpstr *tmpstr)
}
eina_lock_release(&_mutex);
}
EAPI size_t
eina_tmpstr_strlen(Eina_Tmpstr *tmpstr)
{
Str *s;
if (!tmpstr) return 0;
if (!strs) return strlen(tmpstr) + 1;
eina_lock_take(&_mutex);
for (s = strs; s; s = s->next)
{
if (s->str == tmpstr)
{
eina_lock_release(&_mutex);
return s->length;
}
}
eina_lock_release(&_mutex);
return strlen(tmpstr) + 1;
}

View File

@ -158,11 +158,51 @@ typedef const char Eina_Tmpstr;
* @endcode
*
* @see eina_tmpstr_del()
* @see eina_tmpstr_add_length()
*
* @since 1.8.0
*/
EAPI Eina_Tmpstr *eina_tmpstr_add(const char *str) EINA_WARN_UNUSED_RESULT;
/**
* @brief Add a new temporary string based on the input string and length.
*
* @param str This is the input stringthat is copied into the temp string.
* @param length This is the maximum length and the allocated length of the temp string.
* @return A pointer to the tmp string that is a standard C string.
*
* When you add a temporary string (tmpstr) it is expected to have a very
* short lifespan, and at any one time only a few of these are intended to
* exist. This is not intended for longer term storage of strings. The
* intended use is the ability to safely pass strings as return values from
* functions directly into parameters of new functions and then have the
* string be cleaned up automatically by the caller.
*
* If @p str is NULL, or no memory space exists to store the tmpstr, then
* NULL will be returned, otherwise a valid string pointer will be returned
* that you can treat as any other C string (eg strdup(tmpstr) or
* printf("%s\n", tmpstr) etc.). This string should be considered read-only
* and immutable, and when youa re done with the string yo should delete it
* with eina_tmpstr_del().
*
* @see eina_tmpstr_del()
* @see eina_tmpstr_add()
*
* @since 1.8.0
*/
EAPI Eina_Tmpstr *eina_tmpstr_add_length(const char *str, size_t length);
/**
* @brief Return the length of a temporary string including the '\0'.
*
* @param tmpstr This is any C string pointer, but if it is a tmp string
* it will return the length faster.
* @return The length of the string including the '\0';
*
* @since 1.8.0
*/
EAPI size_t eina_tmpstr_strlen(Eina_Tmpstr *tmpstr);
/**
* @brief Delete the temporary string if it is one, or ignore it if it is not.
*