2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <sys/types.h>
25 #include <linux/limits.h>
30 #define MIREGEX_DIR SHARE_PREFIX"/miregex"
31 #define ONELINE_BUF 1024
33 typedef struct miregex_file_info_t {
38 regex_tbl *miregex_tbl = NULL;
39 static time_t miregex_mtime = 0;
41 static void __free_miregex_file_info(miregex_file_info *info);
42 static miregex_file_info *__get_miregex_file_info(const char *path);
43 static int __add_miregex(const char *name, const char *regex, const char *desc);
44 static int __need_update_miregex_tbl();
45 static void __miregex_free_regex_table();
49 static void __free_miregex_file_info(miregex_file_info *info)
54 if (info->regex != NULL)
57 if (info->desc != NULL)
63 static miregex_file_info *__get_miregex_file_info(const char *path)
66 char oneline[ONELINE_BUF];
67 miregex_file_info *info;
71 _E("miregex file %s is cannot open", path);
75 info = (miregex_file_info *)malloc(sizeof(miregex_file_info));
84 while ((info->regex == NULL) || (info->desc == NULL)) {
85 memset(oneline, 0, ONELINE_BUF);
86 if (fgets(oneline, ONELINE_BUF, f) == NULL)
89 oneline[strlen(oneline) - 1] = 0;
91 if (info->regex == NULL)
92 info->regex = strdup(oneline);
94 info->desc = strdup(oneline);
102 static int __add_miregex(const char *name, const char *regex, const char *desc)
104 regex_tbl *tbl = NULL;
112 tbl = (regex_tbl *)malloc(sizeof(regex_tbl));
114 _E("Malloc failed!");
118 if ((error = regcomp(&(tbl->regex_preg), regex,
119 REG_EXTENDED | REG_NOSUB)) != 0) {
120 ret = regerror(error, &(tbl->regex_preg), NULL, 0);
121 msg = (char *)malloc(sizeof(char) * ret);
123 _E("Malloc failed!");
131 regerror(error, &(tbl->regex_preg), msg, ret);
132 _E("regex compile error - %s", msg);
146 tbl->mimetype = strdup(name);
147 tbl->regex = strdup(regex);
149 tbl->desc = strdup(desc);
150 tbl->next = miregex_tbl;
156 static int __need_update_miregex_tbl()
160 if (stat(MIREGEX_DIR, &st) < 0) {
161 _E("stat error - check miregex dir - %s", MIREGEX_DIR);
165 if (st.st_mtime != miregex_mtime) {
166 miregex_mtime = st.st_mtime;
170 if (miregex_tbl == NULL) {
171 miregex_mtime = st.st_mtime;
178 static void __miregex_free_regex_table()
182 while (miregex_tbl) {
183 if (miregex_tbl->mimetype != NULL)
184 free(miregex_tbl->mimetype);
185 if (miregex_tbl->regex != NULL)
186 free(miregex_tbl->regex);
187 if (miregex_tbl->desc != NULL)
188 free(miregex_tbl->desc);
189 regfree(&(miregex_tbl->regex_preg));
192 miregex_tbl = miregex_tbl->next;
199 regex_tbl *miregex_get_regex_table()
202 struct dirent *dentry = NULL;
204 miregex_file_info *info;
206 if (!__need_update_miregex_tbl())
209 _D("*** reload miregex tbl ***");
211 if (miregex_tbl != NULL)
212 __miregex_free_regex_table();
214 dp = opendir(MIREGEX_DIR);
218 while ((dentry = readdir(dp)) != NULL) {
219 if (dentry->d_name[0] == '.')
222 snprintf(buf, sizeof(buf), "%s/%s", MIREGEX_DIR,
225 info = __get_miregex_file_info(buf);
229 if (__add_miregex(dentry->d_name,
230 info->regex, info->desc) < 0) {
231 /* TODO : invalid regular expression - will be removed*/
234 __free_miregex_file_info(info);