Efl.Ui.Format: add string format checking for user input

This commit is contained in:
Amitesh Singh 2017-11-24 14:56:10 +09:00
parent e9d7436fd2
commit 475ae7ef1b
1 changed files with 76 additions and 0 deletions

View File

@ -1,16 +1,92 @@
#include "config.h"
#include "Efl.h"
#define ERR(...) EINA_LOG_DOM_ERR(EINA_LOG_DOMAIN_DEFAULT, __VA_ARGS__)
typedef struct
{
const char *template;
} Efl_Ui_Format_Data;
static Eina_Bool
_is_valid_digit(char x)
{
return ((x >= '0' && x <= '9') || (x == '.')) ? EINA_TRUE : EINA_FALSE;
}
static int
_format_string_check(const char *fmt)
{
const char *itr = NULL;
const char *start = NULL;
Eina_Bool found = EINA_FALSE;
int ret_type = 0;
start = strchr(fmt, '%');
if (!start) return 0;
while (start)
{
if (found && start[1] != '%')
{
return 0;
}
if (start[1] != '%' && !found)
{
found = EINA_TRUE;
for (itr = start + 1; *itr != '\0'; itr++)
{
if ((*itr == 'd') || (*itr == 'u') || (*itr == 'i') ||
(*itr == 'o') || (*itr == 'x') || (*itr == 'X'))
{
ret_type = 1; //int
break;
}
else if ((*itr == 'f') || (*itr == 'F'))
{
ret_type = 2; //double
break;
}
else if (_is_valid_digit(*itr))
{
continue;
}
else
{
return 0;
}
}
}
start = strchr(start + 2, '%');
}
return ret_type;
}
static void
_default_format_cb(void *data, Eina_Strbuf *str, const Eina_Value value)
{
const Eina_Value_Type *type = eina_value_type_get(&value);
Efl_Ui_Format_Data *sd = data;
int format_check_result;
if (type == EINA_VALUE_TYPE_TM)
{
struct tm v;
eina_value_get(&value, &v);
eina_strbuf_append_strftime(str, sd->template, &v);
return;
}
format_check_result = _format_string_check(sd->template);
if (format_check_result == 0)
{
ERR("Wrong String Format: %s\n", sd->template);
return;
}
if (type == EINA_VALUE_TYPE_DOUBLE)
{