forked from enlightenment/efl
eina: strbuf - Add strftime related functions
eina_strbuf_append_strftime() eina_strbuf_insert_strftime() eina_strbuf_prepend_strftime() - macro We need these functions for implementing generic format function interface especially for calander. Ref T6204
This commit is contained in:
parent
6857f349b0
commit
2cf24eb304
|
@ -0,0 +1,35 @@
|
||||||
|
//Compile with:
|
||||||
|
//gcc -Wall -o eina_strbuf_02 eina_strbuf_02c `pkg-config --cflags --libs eina`
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <Eina.h>
|
||||||
|
|
||||||
|
int main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
|
||||||
|
{
|
||||||
|
Eina_Strbuf *buf;
|
||||||
|
time_t curr_time;
|
||||||
|
struct tm *info;
|
||||||
|
|
||||||
|
eina_init();
|
||||||
|
|
||||||
|
curr_time = time(NULL);
|
||||||
|
info = localtime(&curr_time);
|
||||||
|
|
||||||
|
buf = eina_strbuf_new();
|
||||||
|
eina_strbuf_append_strftime(buf, "%I:%M%p", info);
|
||||||
|
printf("current time: %s\n", eina_strbuf_string_get(buf));
|
||||||
|
eina_strbuf_reset(buf);
|
||||||
|
|
||||||
|
buf = eina_strbuf_new();
|
||||||
|
eina_strbuf_append(buf, "Hours: Minutes");
|
||||||
|
//insert hour at ^Hours: Minutes where ^ is the position
|
||||||
|
eina_strbuf_prepend_strftime(buf, "%I ", info);
|
||||||
|
//insert hour at hhhHours: ^Minutes where ^ is the position
|
||||||
|
eina_strbuf_insert_strftime(buf, "%M ", info, 10);
|
||||||
|
printf("%s\n", eina_strbuf_string_get(buf));
|
||||||
|
|
||||||
|
eina_strbuf_free(buf);
|
||||||
|
eina_shutdown();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -220,6 +220,34 @@ eina_strbuf_substr_get(Eina_Strbuf *buf, size_t pos, size_t len)
|
||||||
return eina_strbuf_manage_new(str);
|
return eina_strbuf_manage_new(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EAPI Eina_Bool
|
||||||
|
eina_strbuf_append_strftime(Eina_Strbuf *buf, const char *format, const struct tm *tm)
|
||||||
|
{
|
||||||
|
char *outputbuf;
|
||||||
|
|
||||||
|
outputbuf = eina_strftime(format, tm);
|
||||||
|
if (!outputbuf) return EINA_FALSE;
|
||||||
|
|
||||||
|
eina_strbuf_append(buf, outputbuf);
|
||||||
|
free(outputbuf);
|
||||||
|
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
EAPI Eina_Bool
|
||||||
|
eina_strbuf_insert_strftime(Eina_Strbuf *buf, const char *format, const struct tm *tm, size_t pos)
|
||||||
|
{
|
||||||
|
char *outputbuf;
|
||||||
|
|
||||||
|
outputbuf = eina_strftime(format, tm);
|
||||||
|
if (!outputbuf) return EINA_FALSE;
|
||||||
|
|
||||||
|
eina_strbuf_insert_length(buf, outputbuf, strlen(outputbuf), pos);
|
||||||
|
free(outputbuf);
|
||||||
|
|
||||||
|
return EINA_TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
/* Unicode */
|
/* Unicode */
|
||||||
|
|
||||||
#include "eina_strbuf_template_c.x"
|
#include "eina_strbuf_template_c.x"
|
||||||
|
|
|
@ -737,6 +737,74 @@ EAPI Eina_Rw_Slice eina_strbuf_rw_slice_get(const Eina_Strbuf *buf) EINA_WARN_UN
|
||||||
*/
|
*/
|
||||||
EAPI char* eina_strbuf_release(Eina_Strbuf *buf) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
|
EAPI char* eina_strbuf_release(Eina_Strbuf *buf) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief append the given buffer based on strftime output.
|
||||||
|
*
|
||||||
|
* @param tm Pointer to a tm structure needed by strftime.
|
||||||
|
* @param fmt String containing format specifiers needed by strftime.
|
||||||
|
* @return #EINA_TRUE on success, #EINA_FALSE on failure.
|
||||||
|
*
|
||||||
|
* This will add append buffer of exact required size based on strftime output
|
||||||
|
*
|
||||||
|
* Example usage:
|
||||||
|
* @code
|
||||||
|
* time_t curr_time;
|
||||||
|
* struct tm *info;
|
||||||
|
* Eina_Strbuf *buf = eina_strbuf_new();
|
||||||
|
* curr_time = time(NULL);
|
||||||
|
* info = localtime(&curr_time);
|
||||||
|
* eina_strbuf_append_strftime(buf, "%I:%M%p", info);
|
||||||
|
* //after use
|
||||||
|
* eina_strbuf_free(buf);
|
||||||
|
* @endcode #EINA_TRUE on success, #EINA_FALSE on failure.
|
||||||
|
*
|
||||||
|
* @since 1.21
|
||||||
|
*/
|
||||||
|
EAPI Eina_Bool eina_strbuf_append_strftime(Eina_Strbuf *buf, const char *fmt, const struct tm *tm);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief insert the given buffer based on strftime output at given position
|
||||||
|
*
|
||||||
|
* @param buf The string buffer to prepend to.
|
||||||
|
* @param fmt String containing format specifiers needed by strftime.
|
||||||
|
* @param tm Pointer to a tm structure needed by strftime.
|
||||||
|
* @return #EINA_TRUE on success, #EINA_FALSE on failure.
|
||||||
|
*
|
||||||
|
* This will add append buffer of exact required size based on strftime output
|
||||||
|
*
|
||||||
|
* Example usage:
|
||||||
|
* @code
|
||||||
|
* time_t curr_time;
|
||||||
|
* struct tm *info;
|
||||||
|
* Eina_Strbuf *buf = eina_strbuf_new();
|
||||||
|
* curr_time = time(NULL);
|
||||||
|
* info = localtime(&curr_time);
|
||||||
|
* eina_strbuf_insert_strftime(buf, "%I:%M%p", info, 2);
|
||||||
|
* //after use
|
||||||
|
* eina_strbuf_free(buf);
|
||||||
|
* @endcode
|
||||||
|
*
|
||||||
|
* @since 1.21
|
||||||
|
*/
|
||||||
|
EAPI Eina_Bool eina_strbuf_insert_strftime(Eina_Strbuf *buf, const char *fmt, const struct tm *tm, size_t pos);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @def eina_strbuf_prepend_strftime(buf, fmt, tm)
|
||||||
|
* @brief Prepends the given string to the given buffer.
|
||||||
|
*
|
||||||
|
* @param buf The string buffer to prepend to.
|
||||||
|
* @param fmt The string to prepend.
|
||||||
|
* @param tm The variable arguments.
|
||||||
|
* @return #EINA_TRUE on success, #EINA_FALSE on failure.
|
||||||
|
*
|
||||||
|
* This macro is calling eina_strbuf_insert_strftime() at position 0. If @p buf
|
||||||
|
* can't prepend it, #EINA_FALSE is returned, otherwise #EINA_TRUE is
|
||||||
|
* returned.
|
||||||
|
*
|
||||||
|
* @since 1.21
|
||||||
|
*/
|
||||||
|
#define eina_strbuf_prepend_strftime(buf, fmt, tm) eina_strbuf_insert_strftime(buf, fmt, tm, 0)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -660,6 +660,36 @@ START_TEST(strbuf_release_test)
|
||||||
}
|
}
|
||||||
END_TEST
|
END_TEST
|
||||||
|
|
||||||
|
START_TEST(strbuf_strftime_test)
|
||||||
|
{
|
||||||
|
Eina_Strbuf *buf;
|
||||||
|
time_t curr_time;
|
||||||
|
struct tm *info;
|
||||||
|
char cbuf[32];
|
||||||
|
const char *str;
|
||||||
|
|
||||||
|
curr_time = time(NULL);
|
||||||
|
info = localtime(&curr_time);
|
||||||
|
|
||||||
|
strftime(cbuf, 32, "%I:%M%p", info);
|
||||||
|
|
||||||
|
buf = eina_strbuf_new();
|
||||||
|
eina_strbuf_append_strftime(buf, "%I:%M%p", info);
|
||||||
|
str = eina_strbuf_string_get(buf);
|
||||||
|
fail_if(str == NULL || strcmp(str, cbuf) != 0);
|
||||||
|
eina_strbuf_reset(buf);
|
||||||
|
|
||||||
|
buf = eina_strbuf_new();
|
||||||
|
eina_strbuf_append(buf, "Hours: Minutes");
|
||||||
|
eina_strbuf_prepend_strftime(buf, "%I ", info);
|
||||||
|
eina_strbuf_insert_strftime(buf, "%M ", info, 10);
|
||||||
|
strftime(cbuf, 32, "%I Hours: %M Minutes", info);
|
||||||
|
str = eina_strbuf_string_get(buf);
|
||||||
|
fail_if(str == NULL || strcmp(str, cbuf) != 0);
|
||||||
|
|
||||||
|
eina_strbuf_free(buf);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
eina_test_strbuf(TCase *tc)
|
eina_test_strbuf(TCase *tc)
|
||||||
{
|
{
|
||||||
|
@ -677,4 +707,5 @@ eina_test_strbuf(TCase *tc)
|
||||||
tcase_add_test(tc, strbuf_substr_get);
|
tcase_add_test(tc, strbuf_substr_get);
|
||||||
tcase_add_test(tc, strbuf_prepend_print);
|
tcase_add_test(tc, strbuf_prepend_print);
|
||||||
tcase_add_test(tc, strbuf_release_test);
|
tcase_add_test(tc, strbuf_release_test);
|
||||||
|
tcase_add_test(tc, strbuf_strftime_test);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue