Commit e08b60d3 authored by Lars-Peter Clausen's avatar Lars-Peter Clausen Committed by Jonathan Cameron

iio: core: Simplify iio_format_list()

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: default avatarLars-Peter Clausen <lars@metafoo.de>
Link: https://lore.kernel.org/r/20201114120000.6533-2-lars@metafoo.deSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent eda20ba1
...@@ -715,43 +715,34 @@ static ssize_t iio_format_list(char *buf, const int *vals, int type, int length, ...@@ -715,43 +715,34 @@ static ssize_t iio_format_list(char *buf, const int *vals, int type, int length,
const char *prefix, const char *suffix) const char *prefix, const char *suffix)
{ {
ssize_t len; ssize_t len;
int stride;
int i; int i;
len = scnprintf(buf, PAGE_SIZE, prefix);
switch (type) { switch (type) {
case IIO_VAL_INT: case IIO_VAL_INT:
for (i = 0; i < length; i++) { stride = 1;
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;
}
break; break;
default: default:
for (i = 0; i < length / 2; i++) { stride = 2;
len += __iio_format_value(buf + len, PAGE_SIZE - len, break;
type, 2, &vals[i * 2]); }
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) if (len >= PAGE_SIZE)
return -EFBIG; return -EFBIG;
if (i < length / 2 - 1) }
len += scnprintf(buf + len, PAGE_SIZE - len,
" "); len += __iio_format_value(buf + len, PAGE_SIZE - len, type,
else stride, &vals[i]);
len += scnprintf(buf + len, PAGE_SIZE - len,
"%s\n", suffix);
if (len >= PAGE_SIZE) if (len >= PAGE_SIZE)
return -EFBIG; return -EFBIG;
} }
}
len += scnprintf(buf + len, PAGE_SIZE - len, "%s\n", suffix);
return len; return len;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment