From patchwork Mon Dec 15 08:46:51 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yousong Zhou X-Patchwork-Id: 421039 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id ABE80140082 for ; Mon, 15 Dec 2014 20:28:32 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id AE8D6289D15; Mon, 15 Dec 2014 10:24:15 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 917C928BB80 for ; Mon, 15 Dec 2014 10:23:32 +0100 (CET) X-policyd-weight: using cached result; rate:hard: -8.5 Received: from mail-pd0-f173.google.com (mail-pd0-f173.google.com [209.85.192.173]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Mon, 15 Dec 2014 10:23:29 +0100 (CET) Received: by mail-pd0-f173.google.com with SMTP id ft15so11356234pdb.32 for ; Mon, 15 Dec 2014 01:25:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=VlpDFXyHmslKbZpKLJbCdTDS+jl/48bEePwqSTuybWE=; b=MDT9Knd9di03MMsIP9/kCPB5CG8JYzjb2mB+2by0h7SPS1/l7/cpoyrP7BM/RnrB08 S9Z9jy1IHMhUZ77iYRIzERvCKZL4cmZ/qlnaLlLbXGvrgxfN+KKE5L3StSHy1tSY84BF XAbdvuJd+3MYf/lMMbs/kuJB2wzG2eYN5l/gnqQz202QUwJuVUHDetqOebsByhNtuH5U XT86B7d9A6acHlthp0tO9Lw/EuIQiIFyg8YyyFxbgxhxy6IJ6gSn6vyH/7in7GDeotl9 kfjXySWRfnMb0xaL1xyRhNngZaHXdux86Y2j1lJC0EvLCfeoP8sUnnDEWnrM7UyQoizi t+tg== X-Received: by 10.68.239.70 with SMTP id vq6mr3530815pbc.110.1418635516798; Mon, 15 Dec 2014 01:25:16 -0800 (PST) Received: from debian.lan ([103.29.140.56]) by mx.google.com with ESMTPSA id rh11sm8631536pdb.66.2014.12.15.01.25.14 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Dec 2014 01:25:15 -0800 (PST) From: Yousong Zhou To: nbd@openwrt.org Date: Mon, 15 Dec 2014 16:46:51 +0800 Message-Id: <1418633213-50491-11-git-send-email-yszhou4tech@gmail.com> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1418633213-50491-1-git-send-email-yszhou4tech@gmail.com> References: <1418633213-50491-1-git-send-email-yszhou4tech@gmail.com> Cc: openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH 10/12] delta: use a table for converting between UCI_CMD_XXX and prefixes. X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" Signed-off-by: Yousong Zhou --- delta.c | 64 +++++++++++++++++++-------------------------------------------- uci.h | 6 ++++++ 2 files changed, 25 insertions(+), 45 deletions(-) diff --git a/delta.c b/delta.c index 2eb2ae7..556250d 100644 --- 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 eb7470c..b31d007 100644 --- 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 {