1 /* Init cclasses array from ctypes */
2
3 #include <my_global.h>
4 #include <m_ctype.h>
5 #include <m_string.h>
6 #include "cclass.h"
7 #include "my_regex.h"
8
9 static my_bool regex_inited=0;
10 extern my_regex_stack_check_t my_regex_enough_mem_in_stack;
11
my_regex_init(const CHARSET_INFO * cs,my_regex_stack_check_t func)12 void my_regex_init(const CHARSET_INFO *cs, my_regex_stack_check_t func)
13 {
14 char buff[CCLASS_LAST][256];
15 int count[CCLASS_LAST];
16 uint i;
17
18 if (!regex_inited)
19 {
20 regex_inited=1;
21 my_regex_enough_mem_in_stack= func;
22 memset(&count, 0, sizeof(count));
23
24 for (i=1 ; i<= 255; i++)
25 {
26 if (my_isalnum(cs,i))
27 buff[CCLASS_ALNUM][count[CCLASS_ALNUM]++]=(char) i;
28 if (my_isalpha(cs,i))
29 buff[CCLASS_ALPHA][count[CCLASS_ALPHA]++]=(char) i;
30 if (my_iscntrl(cs,i))
31 buff[CCLASS_CNTRL][count[CCLASS_CNTRL]++]=(char) i;
32 if (my_isdigit(cs,i))
33 buff[CCLASS_DIGIT][count[CCLASS_DIGIT]++]=(char) i;
34 if (my_isgraph(cs,i))
35 buff[CCLASS_GRAPH][count[CCLASS_GRAPH]++]=(char) i;
36 if (my_islower(cs,i))
37 buff[CCLASS_LOWER][count[CCLASS_LOWER]++]=(char) i;
38 if (my_isprint(cs,i))
39 buff[CCLASS_PRINT][count[CCLASS_PRINT]++]=(char) i;
40 if (my_ispunct(cs,i))
41 buff[CCLASS_PUNCT][count[CCLASS_PUNCT]++]=(char) i;
42 if (my_isspace(cs,i))
43 buff[CCLASS_SPACE][count[CCLASS_SPACE]++]=(char) i;
44 if (my_isupper(cs,i))
45 buff[CCLASS_UPPER][count[CCLASS_UPPER]++]=(char) i;
46 if (my_isxdigit(cs,i))
47 buff[CCLASS_XDIGIT][count[CCLASS_XDIGIT]++]=(char) i;
48 }
49 buff[CCLASS_BLANK][0]=' ';
50 buff[CCLASS_BLANK][1]='\t';
51 count[CCLASS_BLANK]=2;
52 for (i=0; i < CCLASS_LAST ; i++)
53 {
54 char *tmp=(char*) malloc(count[i]+1);
55 if (!tmp)
56 {
57 /*
58 This is very unlikely to happen as this function is called once
59 at program startup
60 */
61 fprintf(stderr,
62 "Fatal error: Can't allocate memory in regex_init\n");
63 exit(1);
64 }
65 memcpy(tmp,buff[i],count[i]*sizeof(char));
66 tmp[count[i]]=0;
67 cclasses[i].chars=tmp;
68 }
69 }
70 return;
71 }
72
my_regex_end()73 void my_regex_end()
74 {
75 if (regex_inited)
76 {
77 int i;
78 for (i=0; i < CCLASS_LAST ; i++)
79 free((char*) cclasses[i].chars);
80 my_regex_enough_mem_in_stack= NULL;
81 regex_inited=0;
82 }
83 }
84
85
86