From fe14752e83203faec33c88b77e8ed8c796ec05f5 Mon Sep 17 00:00:00 2001 From: Michal Bloch Date: Tue, 29 Jun 2021 20:33:04 +0200 Subject: [PATCH] Linting: improve some loop code Prevent signed/unsigned comparison in the loop condition and reduce pyramidity. Change-Id: I20a24f37810726d08a60a2791d1e2f8d7695b962 --- src/peripheral_gpio.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/peripheral_gpio.c b/src/peripheral_gpio.c index b77bcdb..fa9c870 100644 --- a/src/peripheral_gpio.c +++ b/src/peripheral_gpio.c @@ -182,27 +182,27 @@ static int peripheral_gpio_set_initial_direction_into_handle(peripheral_gpio_h g {"out", 3}, }; - int index; char gpio_buf[GPIO_BUFFER_MAX] = {0, }; int ret = read(gpio->fd_direction, &gpio_buf, GPIO_BUFFER_MAX); CHECK_ERROR(ret <= 0); - for (index = 0; index < ARRAY_SIZE(types); index++) { - if (!strncmp(gpio_buf, types[index].type, types[index].len)) { - // PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_HIGH and PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW : out type - gpio->direction = (peripheral_gpio_direction_e)index; + for (size_t index = 0; index < ARRAY_SIZE(types); index++) { + if (strncmp(gpio_buf, types[index].type, types[index].len)) + continue; - /* - * Also write to direction for the very first time after boot - * to ensure that the pin is fully initialized. - * Without this, writing to 'edge' is not possible. - */ - ret = write(gpio->fd_direction, types[index].type, types[index].len); - CHECK_ERROR(ret != types[index].len); + // PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_HIGH and PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW : out type + gpio->direction = (peripheral_gpio_direction_e)index; - return PERIPHERAL_ERROR_NONE; - } + /* + * Also write to direction for the very first time after boot + * to ensure that the pin is fully initialized. + * Without this, writing to 'edge' is not possible. + */ + ret = write(gpio->fd_direction, types[index].type, types[index].len); + CHECK_ERROR(ret != types[index].len); + + return PERIPHERAL_ERROR_NONE; } return PERIPHERAL_ERROR_IO_ERROR; @@ -217,17 +217,17 @@ static int peripheral_gpio_set_initial_edge_into_handle(peripheral_gpio_h gpio) {"both", 4} }; - int index; char gpio_buf[GPIO_BUFFER_MAX] = {0, }; int ret = read(gpio->fd_edge, &gpio_buf, GPIO_BUFFER_MAX); CHECK_ERROR(ret <= 0); - for (index = 0; index < ARRAY_SIZE(types); index++) { - if (!strncmp(gpio_buf, types[index].type, types[index].len)) { - gpio->edge = (peripheral_gpio_edge_e)index; - return PERIPHERAL_ERROR_NONE; - } + for (size_t index = 0; index < ARRAY_SIZE(types); index++) { + if (strncmp(gpio_buf, types[index].type, types[index].len)) + continue; + + gpio->edge = (peripheral_gpio_edge_e)index; + return PERIPHERAL_ERROR_NONE; } return PERIPHERAL_ERROR_IO_ERROR; -- 2.7.4