summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmitesh Singh <amitesh.sh@samsung.com>2017-11-28 12:12:08 +0900
committerAmitesh Singh <amitesh.sh@samsung.com>2017-11-28 12:16:48 +0900
commite97d00d5983bf9514c88097fe85e745f0019fe9b (patch)
tree8f7e3276fffd572429faa42e89d24d4002bb3090
parent8193864d7d89adfc4585a5ce8432b765fabfb96c (diff)
Efl.Ui.Format: do proper checking for string template.
Add support to take action based on string format specifier. If string template is '%d' and value type is double, do safe conversion to int from double.
-rw-r--r--src/lib/efl/interfaces/efl_ui_format.c53
1 files changed, 43 insertions, 10 deletions
diff --git a/src/lib/efl/interfaces/efl_ui_format.c b/src/lib/efl/interfaces/efl_ui_format.c
index 04bf964..7fdbc49 100644
--- a/src/lib/efl/interfaces/efl_ui_format.c
+++ b/src/lib/efl/interfaces/efl_ui_format.c
@@ -8,19 +8,26 @@ typedef struct
8 const char *template; 8 const char *template;
9} Efl_Ui_Format_Data; 9} Efl_Ui_Format_Data;
10 10
11typedef enum _Format_Type
12{
13 FORMAT_TYPE_INVALID,
14 FORMAT_TYPE_DOUBLE,
15 FORMAT_TYPE_INT
16} Format_Type;
17
11static Eina_Bool 18static Eina_Bool
12_is_valid_digit(char x) 19_is_valid_digit(char x)
13{ 20{
14 return ((x >= '0' && x <= '9') || (x == '.')) ? EINA_TRUE : EINA_FALSE; 21 return ((x >= '0' && x <= '9') || (x == '.')) ? EINA_TRUE : EINA_FALSE;
15} 22}
16 23
17static int 24static Format_Type
18_format_string_check(const char *fmt) 25_format_string_check(const char *fmt)
19{ 26{
20 const char *itr = NULL; 27 const char *itr = NULL;
21 const char *start = NULL; 28 const char *start = NULL;
22 Eina_Bool found = EINA_FALSE; 29 Eina_Bool found = EINA_FALSE;
23 int ret_type = 0; 30 Format_Type ret_type = FORMAT_TYPE_INVALID;
24 31
25 start = strchr(fmt, '%'); 32 start = strchr(fmt, '%');
26 if (!start) return 0; 33 if (!start) return 0;
@@ -29,7 +36,8 @@ _format_string_check(const char *fmt)
29 { 36 {
30 if (found && start[1] != '%') 37 if (found && start[1] != '%')
31 { 38 {
32 return 0; 39 ret_type = FORMAT_TYPE_INVALID;
40 break;
33 } 41 }
34 42
35 if (start[1] != '%' && !found) 43 if (start[1] != '%' && !found)
@@ -40,12 +48,12 @@ _format_string_check(const char *fmt)
40 if ((*itr == 'd') || (*itr == 'u') || (*itr == 'i') || 48 if ((*itr == 'd') || (*itr == 'u') || (*itr == 'i') ||
41 (*itr == 'o') || (*itr == 'x') || (*itr == 'X')) 49 (*itr == 'o') || (*itr == 'x') || (*itr == 'X'))
42 { 50 {
43 ret_type = 1; //int 51 ret_type = FORMAT_TYPE_INT;
44 break; 52 break;
45 } 53 }
46 else if ((*itr == 'f') || (*itr == 'F')) 54 else if ((*itr == 'f') || (*itr == 'F'))
47 { 55 {
48 ret_type = 2; //double 56 ret_type = FORMAT_TYPE_DOUBLE;
49 break; 57 break;
50 } 58 }
51 else if (_is_valid_digit(*itr)) 59 else if (_is_valid_digit(*itr))
@@ -54,7 +62,8 @@ _format_string_check(const char *fmt)
54 } 62 }
55 else 63 else
56 { 64 {
57 return 0; 65 ret_type = FORMAT_TYPE_INVALID;
66 break;
58 } 67 }
59 } 68 }
60 } 69 }
@@ -82,24 +91,48 @@ _default_format_cb(void *data, Eina_Strbuf *str, const Eina_Value value)
82 91
83 format_check_result = _format_string_check(sd->template); 92 format_check_result = _format_string_check(sd->template);
84 93
85 if (format_check_result == 0) 94 if (format_check_result == FORMAT_TYPE_INVALID)
86 { 95 {
87 ERR("Wrong String Format: %s\n", sd->template); 96 ERR("Wrong String Format: %s", sd->template);
88 return; 97 return;
89 } 98 }
90 99
91 if (type == EINA_VALUE_TYPE_DOUBLE) 100 if ((format_check_result == FORMAT_TYPE_DOUBLE)
101 && (type == EINA_VALUE_TYPE_DOUBLE))
92 { 102 {
93 double v; 103 double v;
94 eina_value_get(&value, &v); 104 eina_value_get(&value, &v);
95 eina_strbuf_append_printf(str, sd->template, v); 105 eina_strbuf_append_printf(str, sd->template, v);
96 } 106 }
97 else if (type == EINA_VALUE_TYPE_INT) 107 else if ((format_check_result == FORMAT_TYPE_INT)
108 && (type == EINA_VALUE_TYPE_INT))
98 { 109 {
99 int v; 110 int v;
100 eina_value_get(&value, &v); 111 eina_value_get(&value, &v);
101 eina_strbuf_append_printf(str, sd->template, v); 112 eina_strbuf_append_printf(str, sd->template, v);
102 } 113 }
114 else if ((format_check_result == FORMAT_TYPE_DOUBLE)
115 && (type == EINA_VALUE_TYPE_INT))
116 {
117 int v;
118 double d_v;
119
120 eina_value_get(&value, &v);
121
122 d_v = v;
123 eina_strbuf_append_printf(str, sd->template, d_v);
124 }
125 else if ((format_check_result == FORMAT_TYPE_INT)
126 && (type == EINA_VALUE_TYPE_DOUBLE))
127 {
128 double v;
129 int i_v;
130
131 eina_value_get(&value, &v);
132
133 i_v = v;
134 eina_strbuf_append_printf(str, sd->template, i_v);
135 }
103 else 136 else
104 { 137 {
105 char *v = eina_value_to_string(&value); 138 char *v = eina_value_to_string(&value);