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 [moved from src/parser.c with 55% similarity]
src/feedback-config.h [moved from src/parser.h with 100% similarity]
src/sound.c
src/vibrator.c

index 4189a67..e83e7e9 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 364c8f3..e09ffd7 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 36275a6..99481dd 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 80776d1..433b572 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();
        }
similarity index 55%
rename from src/parser.c
rename to src/feedback-config.c
index 17af305..d75a9bb 100644 (file)
 
 #include <stdio.h>
 #include <errno.h>
+#include <stdlib.h>
+#include <libsyscommon/ini-parser.h>
 
-#include "parser.h"
+#include "feedback-config.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;
similarity index 100%
rename from src/parser.h
rename to src/feedback-config.h
index 5ec708f..e2fc1a1 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 a12a40a..17cc70e 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"