Linting: improve some loop code 10/260610/2
authorMichal Bloch <m.bloch@samsung.com>
Tue, 29 Jun 2021 18:33:04 +0000 (20:33 +0200)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 30 Jun 2021 14:00:38 +0000 (14:00 +0000)
Prevent signed/unsigned comparison in the loop condition
and reduce pyramidity.

Change-Id: I20a24f37810726d08a60a2791d1e2f8d7695b962

src/peripheral_gpio.c

index b77bcdb..fa9c870 100644 (file)
@@ -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;