Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: core: Consolidate iio_format_avail_{list,range}()

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>
Link: https://lore.kernel.org/r/20201114120000.6533-1-lars@metafoo.de
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Lars-Peter Clausen and committed by
Jonathan Cameron
eda20ba1 6d90c9a9

+15 -44
+15 -44
drivers/iio/industrialio-core.c
··· 711 711 return iio_format_value(buf, ret, val_len, vals); 712 712 } 713 713 714 - static ssize_t iio_format_avail_list(char *buf, const int *vals, 715 - int type, int length) 714 + static ssize_t iio_format_list(char *buf, const int *vals, int type, int length, 715 + const char *prefix, const char *suffix) 716 716 { 717 + ssize_t len; 717 718 int i; 718 - ssize_t len = 0; 719 + 720 + len = scnprintf(buf, PAGE_SIZE, prefix); 719 721 720 722 switch (type) { 721 723 case IIO_VAL_INT: ··· 731 729 " "); 732 730 else 733 731 len += scnprintf(buf + len, PAGE_SIZE - len, 734 - "\n"); 732 + "%s\n", suffix); 735 733 if (len >= PAGE_SIZE) 736 734 return -EFBIG; 737 735 } ··· 747 745 " "); 748 746 else 749 747 len += scnprintf(buf + len, PAGE_SIZE - len, 750 - "\n"); 748 + "%s\n", suffix); 751 749 if (len >= PAGE_SIZE) 752 750 return -EFBIG; 753 751 } ··· 756 754 return len; 757 755 } 758 756 757 + static ssize_t iio_format_avail_list(char *buf, const int *vals, 758 + int type, int length) 759 + { 760 + 761 + return iio_format_list(buf, vals, type, length, "", ""); 762 + } 763 + 759 764 static ssize_t iio_format_avail_range(char *buf, const int *vals, int type) 760 765 { 761 - int i; 762 - ssize_t len; 763 - 764 - len = snprintf(buf, PAGE_SIZE, "["); 765 - switch (type) { 766 - case IIO_VAL_INT: 767 - for (i = 0; i < 3; i++) { 768 - len += __iio_format_value(buf + len, PAGE_SIZE - len, 769 - type, 1, &vals[i]); 770 - if (len >= PAGE_SIZE) 771 - return -EFBIG; 772 - if (i < 2) 773 - len += scnprintf(buf + len, PAGE_SIZE - len, 774 - " "); 775 - else 776 - len += scnprintf(buf + len, PAGE_SIZE - len, 777 - "]\n"); 778 - if (len >= PAGE_SIZE) 779 - return -EFBIG; 780 - } 781 - break; 782 - default: 783 - for (i = 0; i < 3; i++) { 784 - len += __iio_format_value(buf + len, PAGE_SIZE - len, 785 - type, 2, &vals[i * 2]); 786 - if (len >= PAGE_SIZE) 787 - return -EFBIG; 788 - if (i < 2) 789 - len += scnprintf(buf + len, PAGE_SIZE - len, 790 - " "); 791 - else 792 - len += scnprintf(buf + len, PAGE_SIZE - len, 793 - "]\n"); 794 - if (len >= PAGE_SIZE) 795 - return -EFBIG; 796 - } 797 - } 798 - 799 - return len; 766 + return iio_format_list(buf, vals, type, 3, "[", "]"); 800 767 } 801 768 802 769 static ssize_t iio_read_channel_info_avail(struct device *dev,