4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 #include "simple_util.h"
25 #include <sys/types.h>
33 #define MIREGEX_DIR SHARE_PREFIX"/miregex"
34 #define ONELINE_BUF 1024
36 typedef struct miregex_file_info_t {
41 regex_tbl *miregex_tbl = NULL;
42 static time_t miregex_mtime = 0;
44 static void __free_miregex_file_info(miregex_file_info *info);
45 static miregex_file_info *__get_miregex_file_info(const char *path);
46 static int __add_miregex(const char *name, const char *regex, const char *desc);
47 static int __need_update_miregex_tbl();
48 static void __miregex_free_regex_table();
52 static void __free_miregex_file_info(miregex_file_info *info)
57 if (info->regex != NULL)
59 if (info->desc != NULL)
65 static miregex_file_info *__get_miregex_file_info(const char *path)
68 char oneline[ONELINE_BUF];
69 miregex_file_info *info;
73 _E("miregex file %s is cannot open", path);
77 info = (miregex_file_info *) malloc(sizeof(miregex_file_info));
86 while ((info->regex == NULL) || (info->desc == NULL)) {
87 memset(oneline, 0, ONELINE_BUF);
88 if (fgets(oneline, ONELINE_BUF, f) == NULL)
91 oneline[strlen(oneline) - 1] = 0;
93 if (info->regex == NULL)
94 info->regex = strdup(oneline);
96 info->desc = strdup(oneline);
99 /*_D("conf file process done : info->regex = %s,
100 info->desc = %s",info->regex, info->desc);*/
106 static int __add_miregex(const char *name, const char *regex, const char *desc)
108 regex_tbl *tbl = NULL;
116 tbl = (regex_tbl *) malloc(sizeof(regex_tbl));
118 _E("Malloc failed!");
122 if ((error = regcomp(&(tbl->regex_preg), regex,
123 REG_EXTENDED | REG_NOSUB)) != 0) {
124 ret = regerror(error, &(tbl->regex_preg), NULL, 0);
125 msg = (char *)malloc(sizeof(char) * ret);
127 _E("Malloc failed!");
134 regerror(error, &(tbl->regex_preg), msg, ret);
135 _E("regex compile error - %s\n", msg);
147 tbl->mimetype = strdup(name);
148 tbl->regex = strdup(regex);
150 tbl->desc = strdup(desc);
151 tbl->next = miregex_tbl;
154 /*_D("added regex - %d %s %s##",getpid(),tbl->mimetype,tbl->regex);*/
159 static int __need_update_miregex_tbl()
163 if (stat(MIREGEX_DIR, &st) < 0) {
164 _E("stat error - check miregex dir - %s", MIREGEX_DIR);
168 if (st.st_mtime != miregex_mtime) {
169 miregex_mtime = st.st_mtime;
173 if (miregex_tbl == NULL) {
174 miregex_mtime = st.st_mtime;
181 static void __miregex_free_regex_table()
185 while (miregex_tbl) {
186 if (miregex_tbl->mimetype != NULL)
187 free(miregex_tbl->mimetype);
188 if (miregex_tbl->regex != NULL)
189 free(miregex_tbl->regex);
190 if (miregex_tbl->desc != NULL)
191 free(miregex_tbl->desc);
192 regfree(&(miregex_tbl->regex_preg));
195 miregex_tbl = miregex_tbl->next;
202 regex_tbl *miregex_get_regex_table()
205 struct dirent *dentry;
206 char buf[MAX_LOCAL_BUFSZ];
207 miregex_file_info *info;
209 if (!__need_update_miregex_tbl())
212 _D("*** reload miregex tbl ***");
214 if (miregex_tbl != NULL)
215 __miregex_free_regex_table();
217 dp = opendir(MIREGEX_DIR);
221 while ((dentry = readdir(dp)) != NULL) {
222 if (dentry->d_name[0] == '.')
225 snprintf(buf, sizeof(buf), "%s/%s", MIREGEX_DIR,
227 info = __get_miregex_file_info(buf);
231 if (__add_miregex(dentry->d_name,
232 info->regex, info->desc) < 0) {
233 /* TODO : invalid regular expression - will be removed*/
236 __free_miregex_file_info(info);