* [PATCH 1/2] iio: core: Consolidate iio_format_avail_{list,range}()
@ 2020-11-14 11:59 Lars-Peter Clausen
2020-11-14 12:00 ` [PATCH 2/2] iio: core: Simplify iio_format_list() Lars-Peter Clausen
2020-11-28 15:42 ` [PATCH 1/2] iio: core: Consolidate iio_format_avail_{list,range}() Jonathan Cameron
0 siblings, 2 replies; 3+ messages in thread
From: Lars-Peter Clausen @ 2020-11-14 11:59 UTC (permalink / raw)
To: Jonathan Cameron; +Cc: Peter Meerwald-Stadler, linux-iio, Lars-Peter Clausen
The iio_format_avail_list() and iio_format_avail_range() functions are
almost identical. The only differences are that iio_format_avail_range()
expects a fixed amount of items and adds brackets "[ ]" around the output.
Refactor them into a common helper function. This improves the
maintainability of the code as it makes it easier to modify the
implementation of these functions.
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
drivers/iio/industrialio-core.c | 57 ++++++++-------------------------
1 file changed, 14 insertions(+), 43 deletions(-)
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 9955672fc16a..3e71fcab7cbd 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -698,11 +698,13 @@ static ssize_t iio_read_channel_info(struct device *dev,
return iio_format_value(buf, ret, val_len, vals);
}
-static ssize_t iio_format_avail_list(char *buf, const int *vals,
- int type, int length)
+static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
+ const char *prefix, const char *suffix)
{
+ ssize_t len;
int i;
- ssize_t len = 0;
+
+ len = scnprintf(buf, PAGE_SIZE, prefix);
switch (type) {
case IIO_VAL_INT:
@@ -716,7 +718,7 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
" ");
else
len += scnprintf(buf + len, PAGE_SIZE - len,
- "\n");
+ "%s\n", suffix);
if (len >= PAGE_SIZE)
return -EFBIG;
}
@@ -732,7 +734,7 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
" ");
else
len += scnprintf(buf + len, PAGE_SIZE - len,
- "\n");
+ "%s\n", suffix);
if (len >= PAGE_SIZE)
return -EFBIG;
}
@@ -741,47 +743,16 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
return len;
}
-static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
+static ssize_t iio_format_avail_list(char *buf, const int *vals,
+ int type, int length)
{
- int i;
- ssize_t len;
- len = snprintf(buf, PAGE_SIZE, "[");
- switch (type) {
- case IIO_VAL_INT:
- for (i = 0; i < 3; i++) {
- len += __iio_format_value(buf + len, PAGE_SIZE - len,
- type, 1, &vals[i]);
- if (len >= PAGE_SIZE)
- return -EFBIG;
- if (i < 2)
- len += scnprintf(buf + len, PAGE_SIZE - len,
- " ");
- else
- len += scnprintf(buf + len, PAGE_SIZE - len,
- "]\n");
- if (len >= PAGE_SIZE)
- return -EFBIG;
- }
- break;
- default:
- for (i = 0; i < 3; i++) {
- len += __iio_format_value(buf + len, PAGE_SIZE - len,
- type, 2, &vals[i * 2]);
- if (len >= PAGE_SIZE)
- return -EFBIG;
- if (i < 2)
- len += scnprintf(buf + len, PAGE_SIZE - len,
- " ");
- else
- len += scnprintf(buf + len, PAGE_SIZE - len,
- "]\n");
- if (len >= PAGE_SIZE)
- return -EFBIG;
- }
- }
+ return iio_format_list(buf, vals, type, length, "", "");
+}
- return len;
+static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
+{
+ return iio_format_list(buf, vals, type, 3, "[", "]");
}
static ssize_t iio_read_channel_info_avail(struct device *dev,
--
2.20.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH 2/2] iio: core: Simplify iio_format_list()
2020-11-14 11:59 [PATCH 1/2] iio: core: Consolidate iio_format_avail_{list,range}() Lars-Peter Clausen
@ 2020-11-14 12:00 ` Lars-Peter Clausen
2020-11-28 15:42 ` [PATCH 1/2] iio: core: Consolidate iio_format_avail_{list,range}() Jonathan Cameron
1 sibling, 0 replies; 3+ messages in thread
From: Lars-Peter Clausen @ 2020-11-14 12:00 UTC (permalink / raw)
To: Jonathan Cameron; +Cc: Peter Meerwald-Stadler, linux-iio, Lars-Peter Clausen
iio_format_list() has two branches in a switch statement that are almost
identical. They only differ in the stride that is used to iterate through
the item list.
Consolidate this into a common code path to simplify the code.
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
---
drivers/iio/industrialio-core.c | 45 +++++++++++++--------------------
1 file changed, 18 insertions(+), 27 deletions(-)
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 3e71fcab7cbd..0a0d27a8d996 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -702,44 +702,35 @@ static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
const char *prefix, const char *suffix)
{
ssize_t len;
+ int stride;
int i;
- len = scnprintf(buf, PAGE_SIZE, prefix);
-
switch (type) {
case IIO_VAL_INT:
- for (i = 0; i < length; i++) {
- len += __iio_format_value(buf + len, PAGE_SIZE - len,
- type, 1, &vals[i]);
- if (len >= PAGE_SIZE)
- return -EFBIG;
- if (i < length - 1)
- len += scnprintf(buf + len, PAGE_SIZE - len,
- " ");
- else
- len += scnprintf(buf + len, PAGE_SIZE - len,
- "%s\n", suffix);
- if (len >= PAGE_SIZE)
- return -EFBIG;
- }
+ stride = 1;
break;
default:
- for (i = 0; i < length / 2; i++) {
- len += __iio_format_value(buf + len, PAGE_SIZE - len,
- type, 2, &vals[i * 2]);
- if (len >= PAGE_SIZE)
- return -EFBIG;
- if (i < length / 2 - 1)
- len += scnprintf(buf + len, PAGE_SIZE - len,
- " ");
- else
- len += scnprintf(buf + len, PAGE_SIZE - len,
- "%s\n", suffix);
+ stride = 2;
+ break;
+ }
+
+ len = scnprintf(buf, PAGE_SIZE, prefix);
+
+ for (i = 0; i <= length - stride; i += stride) {
+ if (i != 0) {
+ len += scnprintf(buf + len, PAGE_SIZE - len, " ");
if (len >= PAGE_SIZE)
return -EFBIG;
}
+
+ len += __iio_format_value(buf + len, PAGE_SIZE - len, type,
+ stride, &vals[i]);
+ if (len >= PAGE_SIZE)
+ return -EFBIG;
}
+ len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n", suffix);
+
return len;
}
--
2.20.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH 1/2] iio: core: Consolidate iio_format_avail_{list,range}()
2020-11-14 11:59 [PATCH 1/2] iio: core: Consolidate iio_format_avail_{list,range}() Lars-Peter Clausen
2020-11-14 12:00 ` [PATCH 2/2] iio: core: Simplify iio_format_list() Lars-Peter Clausen
@ 2020-11-28 15:42 ` Jonathan Cameron
1 sibling, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2020-11-28 15:42 UTC (permalink / raw)
To: Lars-Peter Clausen; +Cc: Peter Meerwald-Stadler, linux-iio
On Sat, 14 Nov 2020 12:59:59 +0100
Lars-Peter Clausen <lars@metafoo.de> wrote:
> The iio_format_avail_list() and iio_format_avail_range() functions are
> almost identical. The only differences are that iio_format_avail_range()
> expects a fixed amount of items and adds brackets "[ ]" around the output.
>
> Refactor them into a common helper function. This improves the
> maintainability of the code as it makes it easier to modify the
> implementation of these functions.
>
> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Nice little cleanup. Series applied to the togreg branch of iio.git and pushed
out as testing for the autobuilders to poke at it.
Thanks,
Jonathan
> ---
> drivers/iio/industrialio-core.c | 57 ++++++++-------------------------
> 1 file changed, 14 insertions(+), 43 deletions(-)
>
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 9955672fc16a..3e71fcab7cbd 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -698,11 +698,13 @@ static ssize_t iio_read_channel_info(struct device *dev,
> return iio_format_value(buf, ret, val_len, vals);
> }
>
> -static ssize_t iio_format_avail_list(char *buf, const int *vals,
> - int type, int length)
> +static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
> + const char *prefix, const char *suffix)
> {
> + ssize_t len;
> int i;
> - ssize_t len = 0;
> +
> + len = scnprintf(buf, PAGE_SIZE, prefix);
>
> switch (type) {
> case IIO_VAL_INT:
> @@ -716,7 +718,7 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
> " ");
> else
> len += scnprintf(buf + len, PAGE_SIZE - len,
> - "\n");
> + "%s\n", suffix);
> if (len >= PAGE_SIZE)
> return -EFBIG;
> }
> @@ -732,7 +734,7 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
> " ");
> else
> len += scnprintf(buf + len, PAGE_SIZE - len,
> - "\n");
> + "%s\n", suffix);
> if (len >= PAGE_SIZE)
> return -EFBIG;
> }
> @@ -741,47 +743,16 @@ static ssize_t iio_format_avail_list(char *buf, const int *vals,
> return len;
> }
>
> -static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
> +static ssize_t iio_format_avail_list(char *buf, const int *vals,
> + int type, int length)
> {
> - int i;
> - ssize_t len;
>
> - len = snprintf(buf, PAGE_SIZE, "[");
> - switch (type) {
> - case IIO_VAL_INT:
> - for (i = 0; i < 3; i++) {
> - len += __iio_format_value(buf + len, PAGE_SIZE - len,
> - type, 1, &vals[i]);
> - if (len >= PAGE_SIZE)
> - return -EFBIG;
> - if (i < 2)
> - len += scnprintf(buf + len, PAGE_SIZE - len,
> - " ");
> - else
> - len += scnprintf(buf + len, PAGE_SIZE - len,
> - "]\n");
> - if (len >= PAGE_SIZE)
> - return -EFBIG;
> - }
> - break;
> - default:
> - for (i = 0; i < 3; i++) {
> - len += __iio_format_value(buf + len, PAGE_SIZE - len,
> - type, 2, &vals[i * 2]);
> - if (len >= PAGE_SIZE)
> - return -EFBIG;
> - if (i < 2)
> - len += scnprintf(buf + len, PAGE_SIZE - len,
> - " ");
> - else
> - len += scnprintf(buf + len, PAGE_SIZE - len,
> - "]\n");
> - if (len >= PAGE_SIZE)
> - return -EFBIG;
> - }
> - }
> + return iio_format_list(buf, vals, type, length, "", "");
> +}
>
> - return len;
> +static ssize_t iio_format_avail_range(char *buf, const int *vals, int type)
> +{
> + return iio_format_list(buf, vals, type, 3, "[", "]");
> }
>
> static ssize_t iio_read_channel_info_avail(struct device *dev,
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-11-28 22:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-14 11:59 [PATCH 1/2] iio: core: Consolidate iio_format_avail_{list,range}() Lars-Peter Clausen
2020-11-14 12:00 ` [PATCH 2/2] iio: core: Simplify iio_format_list() Lars-Peter Clausen
2020-11-28 15:42 ` [PATCH 1/2] iio: core: Consolidate iio_format_avail_{list,range}() Jonathan Cameron
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.