From 2cf24eb30428fd56e32b1633ab3d795a3fbea5a3 Mon Sep 17 00:00:00 2001 From: Amitesh Singh Date: Fri, 13 Oct 2017 14:36:31 +0900 Subject: [PATCH] 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 --- src/examples/eina/eina_strbuf_02.c | 35 ++++++++++++++++++++ src/lib/eina/eina_strbuf.c | 28 ++++++++++++++++ src/lib/eina/eina_strbuf.h | 68 ++++++++++++++++++++++++++++++++++++++ src/tests/eina/eina_test_strbuf.c | 31 +++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 src/examples/eina/eina_strbuf_02.c diff --git a/src/examples/eina/eina_strbuf_02.c b/src/examples/eina/eina_strbuf_02.c new file mode 100644 index 0000000..9282b69 --- /dev/null +++ b/src/examples/eina/eina_strbuf_02.c @@ -0,0 +1,35 @@ +//Compile with: +//gcc -Wall -o eina_strbuf_02 eina_strbuf_02c `pkg-config --cflags --libs eina` + +#include +#include + +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; +} diff --git a/src/lib/eina/eina_strbuf.c b/src/lib/eina/eina_strbuf.c index 586bffb..f18408d 100644 --- a/src/lib/eina/eina_strbuf.c +++ b/src/lib/eina/eina_strbuf.c @@ -220,6 +220,34 @@ eina_strbuf_substr_get(Eina_Strbuf *buf, size_t pos, size_t len) 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 */ #include "eina_strbuf_template_c.x" diff --git a/src/lib/eina/eina_strbuf.h b/src/lib/eina/eina_strbuf.h index 7d90ae6..eeea11c 100644 --- a/src/lib/eina/eina_strbuf.h +++ b/src/lib/eina/eina_strbuf.h @@ -738,6 +738,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); /** + * @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) + +/** * @} */ diff --git a/src/tests/eina/eina_test_strbuf.c b/src/tests/eina/eina_test_strbuf.c index 0511d96..4add7a1 100644 --- a/src/tests/eina/eina_test_strbuf.c +++ b/src/tests/eina/eina_test_strbuf.c @@ -660,6 +660,36 @@ START_TEST(strbuf_release_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 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_prepend_print); tcase_add_test(tc, strbuf_release_test); + tcase_add_test(tc, strbuf_strftime_test); } -- 2.7.4