delta: use a table for converting between UCI_CMD_XXX and prefixes.
authorYousong Zhou <yszhou4tech@gmail.com>
Tue, 16 Dec 2014 07:00:16 +0000 (15:00 +0800)
committerFelix Fietkau <nbd@openwrt.org>
Thu, 18 Dec 2014 11:38:15 +0000 (12:38 +0100)
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
delta.c
uci.h

diff --git a/delta.c b/delta.c
index 2eb2ae73ade2cac666de39c5a5493362d20b901e..556250dc5b5150ed5727e58a7f5bac87bd32158a 100644 (file)
--- a/delta.c
+++ b/delta.c
@@ -95,34 +95,29 @@ int uci_add_delta_path(struct uci_context *ctx, const char *dir)
        return 0;
 }
 
+char const uci_command_char[] = {
+       [UCI_CMD_ADD] = '+',
+       [UCI_CMD_REMOVE] = '-',
+       [UCI_CMD_CHANGE] = 0,
+       [UCI_CMD_RENAME] = '@',
+       [UCI_CMD_REORDER] = '^',
+       [UCI_CMD_LIST_ADD] = '|',
+       [UCI_CMD_LIST_DEL] = '~'
+};
+
 static inline int uci_parse_delta_tuple(struct uci_context *ctx, struct uci_ptr *ptr)
 {
        struct uci_parse_context *pctx = ctx->pctx;
        char *str = pctx_cur_str(pctx), *arg;
-       int c = UCI_CMD_CHANGE;
+       int c;
 
        UCI_INTERNAL(uci_parse_argument, ctx, ctx->pctx->file, &str, &arg);
-       switch(*arg) {
-       case '^':
-               c = UCI_CMD_REORDER;
-               break;
-       case '-':
-               c = UCI_CMD_REMOVE;
-               break;
-       case '@':
-               c = UCI_CMD_RENAME;
-               break;
-       case '+':
-               /* UCI_CMD_ADD is used for anonymous sections or list values */
-               c = UCI_CMD_ADD;
-               break;
-       case '|':
-               c = UCI_CMD_LIST_ADD;
-               break;
-       case '~':
-               c = UCI_CMD_LIST_DEL;
-               break;
+       for (c = 0; c <= __UCI_CMD_LAST; c++) {
+               if (uci_command_char[c] == *arg)
+                       break;
        }
+       if (c > __UCI_CMD_LAST)
+               c = UCI_CMD_CHANGE;
 
        if (c != UCI_CMD_CHANGE)
                arg += 1;
@@ -445,30 +440,9 @@ int uci_save(struct uci_context *ctx, struct uci_package *p)
 
        uci_foreach_element_safe(&p->delta, tmp, e) {
                struct uci_delta *h = uci_to_delta(e);
-               char *prefix = "";
-
-               switch(h->cmd) {
-               case UCI_CMD_REMOVE:
-                       prefix = "-";
-                       break;
-               case UCI_CMD_RENAME:
-                       prefix = "@";
-                       break;
-               case UCI_CMD_ADD:
-                       prefix = "+";
-                       break;
-               case UCI_CMD_REORDER:
-                       prefix = "^";
-                       break;
-               case UCI_CMD_LIST_ADD:
-                       prefix = "|";
-                       break;
-               case UCI_CMD_LIST_DEL:
-                       prefix = "~";
-                       break;
-               default:
-                       break;
-               }
+               char prefix[2] = {0, 0};
+               if (h->cmd <= __UCI_CMD_LAST)
+                       prefix[0] = uci_command_char[h->cmd];
 
                fprintf(f, "%s%s.%s", prefix, p->e.name, h->section);
                if (e->name)
diff --git a/uci.h b/uci.h
index eb7470c459c771e731cc6897f840d0c6ffc2b7a0..b31d00795804bd1e443725eb83a328ffcde92042 100644 (file)
--- a/uci.h
+++ b/uci.h
@@ -450,6 +450,9 @@ struct uci_option
        } v;
 };
 
+/*
+ * UCI_CMD_ADD is used for anonymous sections or list values
+ */
 enum uci_command {
        UCI_CMD_ADD,
        UCI_CMD_REMOVE,
@@ -458,7 +461,10 @@ enum uci_command {
        UCI_CMD_REORDER,
        UCI_CMD_LIST_ADD,
        UCI_CMD_LIST_DEL,
+       __UCI_CMD_MAX,
+       __UCI_CMD_LAST = __UCI_CMD_MAX - 1
 };
+extern char const uci_command_char[];
 
 struct uci_delta
 {