summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--AUTHORS1
-rw-r--r--src/examples/eina/eina_str_01.c11
-rw-r--r--src/lib/eina/eina_str.c33
-rw-r--r--src/lib/eina/eina_str.h26
-rw-r--r--src/tests/eina/eina_test_str.c19
5 files changed, 89 insertions, 1 deletions
diff --git a/AUTHORS b/AUTHORS
index eab2011b7a..f714c39aad 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -53,6 +53,7 @@ ChunEon Park (Hermet) <hermet@hermet.pe.kr>
53Rajeev Ranjan (Rajeev) <rajeev.r@samsung.com> <rajeev.jnnce@gmail.com> 53Rajeev Ranjan (Rajeev) <rajeev.r@samsung.com> <rajeev.jnnce@gmail.com>
54Subodh Kumar <s7158.kumar@samsung.com> 54Subodh Kumar <s7158.kumar@samsung.com>
55Michelle Legrand <legrand.michelle@outlook.com> 55Michelle Legrand <legrand.michelle@outlook.com>
56Shilpa Singh <shilpa.singh@samsung.com> <shilpasingh.o@gmail.com>
56 57
57Eet 58Eet
58--- 59---
diff --git a/src/examples/eina/eina_str_01.c b/src/examples/eina/eina_str_01.c
index 25fe75a6b8..fbe2cf0815 100644
--- a/src/examples/eina/eina_str_01.c
+++ b/src/examples/eina/eina_str_01.c
@@ -14,6 +14,9 @@ int main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
14 char *part2 = "There are many copies. And they have a plan."; 14 char *part2 = "There are many copies. And they have a plan.";
15 char **arr; 15 char **arr;
16 int i; 16 int i;
17 char *time_arr;
18 time_t curr_time;
19 struct tm *info;
17 20
18 eina_init(); 21 eina_init();
19 22
@@ -58,7 +61,13 @@ int main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
58 eina_strlcat(str, "humans", 14); 61 eina_strlcat(str, "humans", 14);
59 printf("%s\n", str); 62 printf("%s\n", str);
60 free(str); 63 free(str);
61 64
65 curr_time = time(NULL);
66 info = localtime(&curr_time);
67 time_arr = eina_strftime("%d/%m/%Y", info);
68 printf("Today's Date: %s\n", time_arr);
69 free(time_arr);
70
62 eina_shutdown(); 71 eina_shutdown();
63 72
64 return 0; 73 return 0;
diff --git a/src/lib/eina/eina_str.c b/src/lib/eina/eina_str.c
index c3d9c4ca25..952f6ce94b 100644
--- a/src/lib/eina/eina_str.c
+++ b/src/lib/eina/eina_str.c
@@ -374,6 +374,39 @@ eina_strlcat(char *dst, const char *src, size_t siz)
374 return(dlen + (s - src)); /* count does not include NUL */ 374 return(dlen + (s - src)); /* count does not include NUL */
375} 375}
376 376
377EAPI char *
378eina_strftime(const char *format, const struct tm *tm)
379{
380 const size_t flen = strlen(format);
381 size_t buflen = 16; // An arbitrary starting size
382 char *buf = NULL;
383
384 do {
385 char *tmp;
386 size_t len;
387
388 tmp = realloc(buf, buflen * sizeof(char));
389 if (!tmp) goto on_error;
390 buf = tmp;
391
392 len = strftime(buf, buflen, format, tm);
393 // Check if we have the expected result and return it.
394 if ((len > 0 && len < buflen) || (len == 0 && flen == 0))
395 {
396 tmp = realloc(buf, ((len + 1) * sizeof(char)));
397 buf = tmp;
398 return buf;
399 }
400
401 /* Possibly buf overflowed - try again with a bigger buffer */
402 buflen <<= 1; // multiply buffer size by 2
403 } while (buflen < 128 * flen);
404
405 on_error:
406 free(buf);
407 return NULL;
408}
409
377EAPI Eina_Bool 410EAPI Eina_Bool
378eina_str_has_prefix(const char *str, const char *prefix) 411eina_str_has_prefix(const char *str, const char *prefix)
379{ 412{
diff --git a/src/lib/eina/eina_str.h b/src/lib/eina/eina_str.h
index 100cc81c48..0448269792 100644
--- a/src/lib/eina/eina_str.h
+++ b/src/lib/eina/eina_str.h
@@ -355,6 +355,32 @@ static inline size_t eina_strlen_bounded(const char *str, size_t maxlen) EINA_PU
355 * @since 1.13 355 * @since 1.13
356 */ 356 */
357EAPI unsigned char *eina_memdup(unsigned char *mem, size_t size, Eina_Bool terminate); 357EAPI unsigned char *eina_memdup(unsigned char *mem, size_t size, Eina_Bool terminate);
358
359/**
360 * @brief Create and update the buffer based on strftime output.
361 *
362 * @param tm Pointer to a tm structure needed by strftime.
363 * @param format String containing format specifiers needed by strftime.
364 * @return Updated buffer based on strftime output
365 *
366 * This will create a buffer of exact required size based on strftime output
367 * once use is complete the buffer has to be freed using free.
368 *
369 * Example usage:
370 * @code
371 * time_t curr_time;
372 * struct tm *info;
373 * char *buf;
374 * curr_time = time(NULL);
375 * info = localtime(&curr_time);
376 * buf = eina_strftime("%I:%M%p", info);
377 * //after use
378 * free(buf);
379 * @endcode
380 *
381 * @since 1.16.0
382 */
383EAPI char *eina_strftime(const char *format, const struct tm *tm);
358#include "eina_inline_str.x" 384#include "eina_inline_str.x"
359 385
360/** 386/**
diff --git a/src/tests/eina/eina_test_str.c b/src/tests/eina/eina_test_str.c
index 8233dd8b37..97b3865a9d 100644
--- a/src/tests/eina/eina_test_str.c
+++ b/src/tests/eina/eina_test_str.c
@@ -347,6 +347,24 @@ START_TEST(str_memdup)
347} 347}
348END_TEST 348END_TEST
349 349
350START_TEST(str_strftime)
351{
352 time_t curr_time;
353 struct tm *info;
354 char *buf;
355 eina_init();
356
357 curr_time = time(NULL);
358 info = localtime(&curr_time);
359
360 buf = eina_strftime("%I:%M%p", info);
361 fail_if(buf == NULL);
362 free(buf);
363
364 eina_shutdown();
365}
366END_TEST
367
350#ifdef HAVE_ICONV 368#ifdef HAVE_ICONV
351START_TEST(str_convert) 369START_TEST(str_convert)
352{ 370{
@@ -385,6 +403,7 @@ eina_test_str(TCase *tc)
385 tcase_add_test(tc, str_lcat_lcpy); 403 tcase_add_test(tc, str_lcat_lcpy);
386 tcase_add_test(tc, str_join_len); 404 tcase_add_test(tc, str_join_len);
387 tcase_add_test(tc, str_memdup); 405 tcase_add_test(tc, str_memdup);
406 tcase_add_test(tc, str_strftime);
388#ifdef HAVE_ICONV 407#ifdef HAVE_ICONV
389 tcase_add_test(tc, str_convert); 408 tcase_add_test(tc, str_convert);
390#endif 409#endif