eina_tmpstr: add eina_tmpstr_strftime

Summary:

@feature

Test Plan:
eina_tmpstr_strftime API can be used to create a temporary string
which is updated with strftime output

eina_tmpstr_steal can be used to get actual string set in eina_tmpstr

Reviewers: cedric

Subscribers: rajeshps, cedric, govi

Differential Revision: https://phab.enlightenment.org/D3048

Signed-off-by: Cedric BAIL <cedric@osg.samsung.com>
This commit is contained in:
Shilpa Singh 2015-09-21 23:48:16 +02:00 committed by Cedric BAIL
parent 77541f7ba2
commit abaf29cb76
3 changed files with 63 additions and 2 deletions

View File

@ -53,6 +53,7 @@ ChunEon Park (Hermet) <hermet@hermet.pe.kr>
Rajeev Ranjan (Rajeev) <rajeev.r@samsung.com> <rajeev.jnnce@gmail.com>
Subodh Kumar <s7158.kumar@samsung.com>
Michelle Legrand <legrand.michelle@outlook.com>
Shilpa Singh <shilpa.singh@samsung.com> <shilpasingh.o@gmail.com>
Eet
---

View File

@ -133,13 +133,48 @@ eina_tmpstr_len(Eina_Tmpstr *tmpstr)
for (s = strs; s; s = s->next)
{
if (s->str == tmpstr)
{
{
size_t ret = s->length;
eina_lock_release(&_mutex);
return ret;
}
}
}
eina_lock_release(&_mutex);
return strlen(tmpstr);
}
EAPI Eina_Tmpstr *
eina_tmpstr_strftime(const char *format, const struct tm *tm)
{
const size_t flen = strlen(format);
size_t buflen = 16; // An arbitrary starting size
char *buf = NULL;
do {
char *tmp;
size_t len;
tmp = realloc(buf, buflen * sizeof(char));
if (!tmp) goto on_error;
buf = tmp;
len = strftime(buf, buflen, format, tm);
// Check if we have the expected result and return it.
if ((len > 0 && len < buflen) || (len == 0 && flen == 0))
{
Eina_Tmpstr *r;
r = eina_tmpstr_add_length(buf, len + 1);
free(buf);
return r;
}
/* Possibly buf overflowed - try again with a bigger buffer */
buflen <<= 1; // multiply buffer size by 2
} while (buflen < 128 * flen);
on_error:
free(buf);
return NULL;
}

View File

@ -237,6 +237,31 @@ EAPI size_t eina_tmpstr_len(Eina_Tmpstr *tmpstr);
*/
EAPI void eina_tmpstr_del(Eina_Tmpstr *tmpstr) EINA_ARG_NONNULL(1);
/**
* @brief Add a new temporary string based on strftime output.
*
* @param tm Pointer to a tm structure needed by strftime.
* @param format String containing format specifiers needed by strftime.
*
* This will add a new temporary string by updating the string value by output
* of strftime.
*
* Example usage:
*
* @code
* time_t curr_time;
* struct tm * info;
* Eina_Tmpstr *buf;
*
* curr_time = time(NULL);
* info = localtime(&curr_time);
* buf = eina_tmpstr_strftime("%I:%M%p", info);
* @endcode
*
* @since 1.16.0
*/
EAPI Eina_Tmpstr *eina_tmpstr_strftime(const char *format, const struct tm *tm) EINA_ARG_NONNULL(2);
/**
* @}
*/