summaryrefslogtreecommitdiff
path: root/src/lib/elm_helper.c
blob: 57a042353722c11693e18be9d0ecb2c69678b6e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Elementary.h>
#include <regex.h>

struct _Elm_Validator_Regexp
{
   Eina_Stringshare *signal;
   int status;
   regex_t regex;
};

EAPI Elm_Validator_Regexp *
elm_validator_regexp_new(const char *pattern, const char *signal)
{
   Elm_Validator_Regexp *validator;

   validator = calloc(1, sizeof(Elm_Validator_Regexp));
   validator->signal = eina_stringshare_add(signal ? signal : "default");
   validator->status = regcomp(&validator->regex, pattern, REG_EXTENDED | REG_NOSUB) ? ELM_REG_BADPAT : ELM_REG_NOERROR;

   return validator;
}

EAPI void
elm_validator_regexp_free(Elm_Validator_Regexp *validator)
{
   eina_stringshare_del(validator->signal);
   regfree(&validator->regex);
   free(validator);
}

EAPI Elm_Regexp_Status
elm_validator_regexp_status_get(Elm_Validator_Regexp *validator)
{
   return validator->status;
}

EAPI Eina_Bool
elm_validator_regexp_helper(void *data,
                            Eo *obj EINA_UNUSED,
                            const Eo_Event_Description *desc EINA_UNUSED,
                            void *event_info)
{
   Elm_Validate_Content *vc = event_info;
   Elm_Validator_Regexp *validator = (Elm_Validator_Regexp *)data;

   validator->status = regexec(&validator->regex, vc->text, (size_t)0, NULL, 0) ? ELM_REG_NOMATCH : ELM_REG_NOERROR;
   vc->signal = validator->signal;
   return validator->status ? EO_CALLBACK_STOP : EO_CALLBACK_CONTINUE;
}