Use libsyscommon for list and ini-parser 01/250901/2
authorYunmi Ha <yunmi.ha@samsung.com>
Tue, 5 Jan 2021 09:14:13 +0000 (18:14 +0900)
committerYunmi Ha <yunmi.ha@samsung.com>
Wed, 6 Jan 2021 03:54:24 +0000 (12:54 +0900)
Change-Id: Id71a4088c732d202b7837992b10986ec8e6bbda8
Signed-off-by: Yunmi Ha <yunmi.ha@samsung.com>
CMakeLists.txt
src/check.c
src/common.h
src/devices.c
src/feedback-config.c [new file with mode: 0644]
src/feedback-config.h [new file with mode: 0644]
src/parser.c [deleted file]
src/parser.h [deleted file]
src/sound.c
src/vibrator.c

index 4189a67ecfa3da9928c034113283de758f81421b..e83e7e9d44b76c042c50c84adc2ebc0065845693 100644 (file)
@@ -14,7 +14,7 @@ SET(SRCS
        src/sound.c
        src/vibrator.c
        src/devices.c
-       src/parser.c
+       src/feedback-config.c
        src/feedback.c
        src/check.c)
 
index 364c8f3e789c772b1bc1cb06921c95c87de15c60..e09ffd7458885b0f2990c858d6fa8182a85479cd 100644 (file)
@@ -17,6 +17,8 @@
 
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
 #include <vconf.h>
 #include <system_info.h>
 #include <sys/stat.h>
index 36275a613eb0e35edd90f58f9270cf9f4ffdf99c..99481dd3ff8b7fc69ce356ca835f322ad3d027f3 100644 (file)
@@ -40,25 +40,7 @@ extern "C" {
 #define __DESTRUCTOR__ __attribute__ ((destructor))
 #endif
 
-#ifndef __DD_LIST__
-#define __DD_LIST__
-#include <glib.h>
-typedef GList dd_list;
-#define DD_LIST_PREPEND(a, b)       \
-       a = g_list_prepend(a, b)
-#define DD_LIST_APPEND(a, b)        \
-       a = g_list_append(a, b)
-#define DD_LIST_REMOVE(a, b)        \
-       a = g_list_remove(a, b)
-#define DD_LIST_FOREACH(head, elem, node)   \
-       for (elem = head; elem && ((node = elem->data) != NULL); elem = elem->next, node = NULL)
-#define DD_LIST_FOREACH_SAFE(head, elem, elem_next, node)       \
-    for (elem = head, elem_next=g_list_next(elem), node = NULL; \
-            elem && ((node = elem->data) != NULL);      \
-            elem = elem_next, elem_next = g_list_next(elem), node=NULL)
-#endif
-
-#define FEEDBACK_DATA_DIR                      FEEDBACK_SYS_SHARE"/feedback"
+#define FEEDBACK_DATA_DIR                      FEEDBACK_SYS_SHARE"/feedback"
 #define FEEDBACK_ORIGIN_DATA_DIR       FEEDBACK_SYS_RO_SHARE"/feedback"
 
 #define FEEDBACK_RETRY_CNT       1
index 80776d11fd46281b87703c193ef205deea2f65c6..433b572abd37659152c2a5462c6fc2e9e721d079 100644 (file)
 
 
 #include <stdio.h>
+#include <libsyscommon/list.h>
 
 #include "feedback-ids.h"
 #include "devices.h"
 #include "log.h"
 
-static dd_list *dev_head;
+static list *dev_head;
 
 void add_device(const struct device_ops *dev)
 {
-       DD_LIST_APPEND(dev_head, (struct device_ops*)dev);
+       LIST_APPEND(dev_head, (struct device_ops*)dev);
 }
 
 //LCOV_EXCL_START System Error
 void remove_device(const struct device_ops *dev)
 {
-       DD_LIST_REMOVE(dev_head, (struct device_ops*)dev);
+       LIST_REMOVE(dev_head, (struct device_ops*)dev);
 }
 //LCOV_EXCL_STOP
 
 const struct device_ops *find_device(int type)
 {
-       dd_list *elem;
+       list *elem;
        const struct device_ops *dev;
 
-       DD_LIST_FOREACH(dev_head, elem, dev) {
+       LIST_FOREACH(dev_head, elem, dev) {
                if (dev->type == type)
                        return dev;
        }
@@ -51,10 +52,10 @@ const struct device_ops *find_device(int type)
 
 void devices_init(void)
 {
-       dd_list *elem;
+       list *elem;
        const struct device_ops *dev;
 
-       DD_LIST_FOREACH(dev_head, elem, dev) {
+       LIST_FOREACH(dev_head, elem, dev) {
                _D("[%s] initialize", dev->name);
                if (dev->init)
                        dev->init();
@@ -63,10 +64,10 @@ void devices_init(void)
 
 void devices_exit(void)
 {
-       dd_list *elem;
+       list *elem;
        const struct device_ops *dev;
 
-       DD_LIST_FOREACH(dev_head, elem, dev) {
+       LIST_FOREACH(dev_head, elem, dev) {
                _D("[%s] deinitialize", dev->name);
                if (dev->exit)
                        dev->exit();
@@ -75,11 +76,11 @@ void devices_exit(void)
 
 int devices_play(int pattern, bool always)
 {
-       dd_list *elem;
+       list *elem;
        const struct device_ops *dev;
        int ret, prev = -EPERM;
 
-       DD_LIST_FOREACH(dev_head, elem, dev) {
+       LIST_FOREACH(dev_head, elem, dev) {
                if (dev->play) {
                        ret = dev->play(pattern, always);
                        if ((prev < 0 && ret == 0) ||
@@ -98,11 +99,11 @@ int devices_play(int pattern, bool always)
 
 int devices_play_soundpath(int pattern, const char *soundpath, bool always)
 {
-       dd_list *elem;
+       list *elem;
        const struct device_ops *dev;
        int ret, prev = -EPERM;
 
-       DD_LIST_FOREACH(dev_head, elem, dev) {
+       LIST_FOREACH(dev_head, elem, dev) {
                if (dev->type == FEEDBACK_TYPE_SOUND) {
                        if (dev->play_path)
                                ret = dev->play_path(pattern, soundpath, always);
@@ -130,11 +131,11 @@ int devices_play_soundpath(int pattern, const char *soundpath, bool always)
 
 int devices_stop(void)
 {
-       dd_list *elem;
+       list *elem;
        const struct device_ops *dev;
        int ret = -ENOTSUP;
 
-       DD_LIST_FOREACH(dev_head, elem, dev) {
+       LIST_FOREACH(dev_head, elem, dev) {
                if (dev->stop)
                        ret = dev->stop();
        }
diff --git a/src/feedback-config.c b/src/feedback-config.c
new file mode 100644 (file)
index 0000000..d75a9bb
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * libfeedback
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <libsyscommon/ini-parser.h>
+
+#include "feedback-config.h"
+#include "profiles.h"
+#include "log.h"
+
+#define MAX_DATA       256
+
+#define MATCH(a, b)             (!strncmp(a, b, strlen(a)))
+#define SET_CONF(a, b)          (a = (b > 0.0 ? b : a))
+
+static int load_config_index = 0;
+
+static int load_config(struct parse_result *result, void *user_data)
+{
+       struct feedback_config_info *info = (struct feedback_config_info *)user_data;
+       char *name;
+       char *value;
+       int pattern;
+
+       if (!info)
+               return -EINVAL;
+
+       if (!MATCH(result->section, info->name))
+               return -ENOENT;
+
+       if (!result->name || !result->value)
+               return -ENOENT;
+
+       name = result->name;
+       value = result->value;
+
+       pattern = profile->get_pattern_enum(name);
+       if (pattern < 0 || pattern >= profile->max_pattern)
+               return -EINVAL;
+
+       info->data[load_config_index].pattern = pattern;
+
+       info->data[load_config_index].origin = strdup(value);
+       if (!info->data[load_config_index].origin)
+               _E("fail to copy %d sound data", //LCOV_EXCL_LINE
+                               pattern);
+       load_config_index++;
+
+       return 0;
+}
+
+int feedback_load_config(const char *path,
+               struct feedback_config_info *info)
+{
+       int ret;
+       int i;
+
+       if (!path || !info)
+               return -EINVAL;
+
+       info->data = calloc(1,
+                       sizeof(struct feedback_data) * profile->get_num_of_pattern());
+       if (!info->data) {
+               _E("fail to allocate %s data", path); //LCOV_EXCL_LINE
+               return -ENOMEM; //LCOV_EXCL_LINE System Error
+       }
+
+       for (i = 0; i < profile->get_num_of_pattern(); i++)
+               info->data[i].pattern = -1;
+
+       ret = config_parse(path, load_config, info);
+       if (ret < 0)
+               _E("Failed to load %s, %d Use default value!", //LCOV_EXCL_LINE
+                               path, ret);
+
+       return ret;
+}
+
+void feedback_free_config(struct feedback_config_info *info)
+{
+       int i;
+
+       if (!info || !(info->data))
+               return;
+
+       if (!profile)
+               return;
+
+       for (i = 0; i < profile->get_num_of_pattern(); i++) {
+               if (info->data[i].origin) {
+                       free(info->data[i].origin);
+                       info->data[i].origin = NULL;
+               }
+               if (info->data[i].changed) {
+                       free(info->data[i].changed);
+                       info->data[i].changed = NULL;
+               }
+       }
+
+       free(info->data);
+       info->data = NULL;
+       load_config_index = 0;
+}
diff --git a/src/feedback-config.h b/src/feedback-config.h
new file mode 100644 (file)
index 0000000..97cdd65
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * libfeedback
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __PARSER_H__
+#define __PARSER_H__
+
+struct feedback_data {
+       int pattern;
+       char *origin;
+       char *changed;
+};
+
+struct feedback_config_info {
+       char *name;
+       struct feedback_data *data;
+};
+
+int feedback_load_config(const char *path,
+               struct feedback_config_info *info);
+void feedback_free_config(struct feedback_config_info *info);
+
+#endif
diff --git a/src/parser.c b/src/parser.c
deleted file mode 100644 (file)
index 17af305..0000000
+++ /dev/null
@@ -1,226 +0,0 @@
-/*
- * libfeedback
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <stdio.h>
-#include <errno.h>
-
-#include "parser.h"
-#include "profiles.h"
-#include "log.h"
-
-#define MAX_DATA       256
-#define MAX_LINE       512
-#define MAX_SECTION    64
-#define WHITESPACE     " \t"
-#define NEWLINE                "\n\r"
-#define COMMENT                '#'
-
-#define MATCH(a, b)             (!strncmp(a, b, strlen(a)))
-#define SET_CONF(a, b)          (a = (b > 0.0 ? b : a))
-
-struct parse_result {
-       char *section;
-       char *name;
-       char *value;
-};
-
-static int load_config_index = 0;
-
-static inline char *trim_str(char *s)
-{
-       char *t;
-       /* left trim */
-       s += strspn(s, WHITESPACE);
-
-       /* right trim */
-       for (t = strchr(s, 0); t > s; t--)
-               if (!strchr(WHITESPACE, t[-1]))
-                       break;
-       *t = 0;
-       return s;
-}
-
-static int config_parse(const char *file_name,
-               int cb(struct parse_result *result, void *user_data),
-               void *user_data)
-{
-       FILE *f = NULL;
-       struct parse_result result;
-       /* use stack for parsing */
-       char line[MAX_LINE];
-       char section[MAX_SECTION];
-       char *start, *end, *name, *value;
-       int lineno = 0, ret = 0;
-
-       if (!file_name || !cb) {
-               ret = -EINVAL;
-               goto error;
-       }
-
-       /* open conf file */
-       f = fopen(file_name, "r");
-       if (!f) {
-               _E("Failed to open file %s", file_name); //LCOV_EXCL_LINE
-               ret = -EIO;
-               goto error;
-       }
-
-       /* parsing line by line */
-       while (fgets(line, MAX_LINE, f) != NULL) {
-               lineno++;
-
-               start = line;
-               start[strcspn(start, NEWLINE)] = '\0';
-               start = trim_str(start);
-
-               if (*start == COMMENT) {
-                       continue;
-               } else if (*start == '[') {
-                       /* parse section */
-                       end = strchr(start, ']');
-                       if (!end || *end != ']') {
-                               ret = -EBADMSG;
-                               goto error;
-                       }
-
-                       *end = '\0';
-                       strncpy(section, start + 1, sizeof(section));
-                       section[MAX_SECTION-1] = '\0';
-               } else if (*start) {
-                       /* parse name & value */
-                       end = strchr(start, '=');
-                       if (!end || *end != '=') {
-                               ret = -EBADMSG;
-                               goto error;
-                       }
-                       *end = '\0';
-                       name = trim_str(start);
-                       value = trim_str(end + 1);
-                       end = strchr(value, COMMENT);
-                       if (end && *end == COMMENT) {
-                               *end = '\0';
-                               value = trim_str(value);
-                       }
-
-                       result.section = section;
-                       result.name = name;
-                       result.value = value;
-                       /* callback with parse result */
-                       ret = cb(&result, user_data);
-                       if (ret < 0) {
-                               ret = -EBADMSG;
-                               goto error;
-                       }
-               }
-       }
-       _D("Success to load %s", file_name);
-       fclose(f);
-       return 0;
-
-error:
-       if (f)
-               fclose(f);
-       _E("Failed to read %s:%d!", file_name, lineno); //LCOV_EXCL_LINE
-       return ret;
-}
-
-static int load_config(struct parse_result *result, void *user_data)
-{
-       struct feedback_config_info *info = (struct feedback_config_info *)user_data;
-       char *name;
-       char *value;
-       int pattern;
-
-       if (!info)
-               return -EINVAL;
-
-       if (!MATCH(result->section, info->name))
-               return -ENOENT;
-
-       if (!result->name || !result->value)
-               return -ENOENT;
-
-       name = result->name;
-       value = result->value;
-
-       pattern = profile->get_pattern_enum(name);
-       if (pattern < 0 || pattern >= profile->max_pattern)
-               return -EINVAL;
-
-       info->data[load_config_index].pattern = pattern;
-
-       info->data[load_config_index].origin = strdup(value);
-       if (!info->data[load_config_index].origin)
-               _E("fail to copy %d sound data", //LCOV_EXCL_LINE
-                               pattern);
-       load_config_index++;
-
-       return 0;
-}
-
-int feedback_load_config(const char *path,
-               struct feedback_config_info *info)
-{
-       int ret;
-       int i;
-
-       if (!path || !info)
-               return -EINVAL;
-
-       info->data = calloc(1,
-                       sizeof(struct feedback_data) * profile->get_num_of_pattern());
-       if (!info->data) {
-               _E("fail to allocate %s data", path); //LCOV_EXCL_LINE
-               return -ENOMEM; //LCOV_EXCL_LINE System Error
-       }
-
-       for (i = 0; i < profile->get_num_of_pattern(); i++)
-               info->data[i].pattern = -1;
-
-       ret = config_parse(path, load_config, info);
-       if (ret < 0)
-               _E("Failed to load %s, %d Use default value!", //LCOV_EXCL_LINE
-                               path, ret);
-
-       return ret;
-}
-
-void feedback_free_config(struct feedback_config_info *info)
-{
-       int i;
-
-       if (!info || !(info->data))
-               return;
-
-       if (!profile)
-               return;
-
-       for (i = 0; i < profile->get_num_of_pattern(); i++) {
-               if (info->data[i].origin) {
-                       free(info->data[i].origin);
-                       info->data[i].origin = NULL;
-               }
-               if (info->data[i].changed) {
-                       free(info->data[i].changed);
-                       info->data[i].changed = NULL;
-               }
-       }
-
-       free(info->data);
-       info->data = NULL;
-       load_config_index = 0;
-}
diff --git a/src/parser.h b/src/parser.h
deleted file mode 100644 (file)
index 97cdd65..0000000
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * libfeedback
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the License);
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __PARSER_H__
-#define __PARSER_H__
-
-struct feedback_data {
-       int pattern;
-       char *origin;
-       char *changed;
-};
-
-struct feedback_config_info {
-       char *name;
-       struct feedback_data *data;
-};
-
-int feedback_load_config(const char *path,
-               struct feedback_config_info *info);
-void feedback_free_config(struct feedback_config_info *info);
-
-#endif
index 5ec708fc28aae63af427a93014c6f9eaff5b7a58..e2fc1a1b20d0debb2d9fb65147dba942128ece7a 100644 (file)
@@ -22,6 +22,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 #include <errno.h>
+#include <stdlib.h>
 #include <assert.h>
 #include <limits.h>
 #include <vconf.h>
@@ -33,7 +34,7 @@
 #include "profiles.h"
 #include "devices.h"
 #include "log.h"
-#include "parser.h"
+#include "feedback-config.h"
 
 #define SOUND_CONF_FILE FEEDBACK_SYS_RO_SHARE"/feedback/sound.conf"
 
index a12a40ab391c891a768d33f07558ee1db1a5f7b4..17cc70e9208638e37eefff7d6339d5f7421675fc 100644 (file)
@@ -29,7 +29,7 @@
 
 #include "feedback.h"
 #include "profiles.h"
-#include "parser.h"
+#include "feedback-config.h"
 #include "devices.h"
 #include "log.h"