From 38aa87faffe94ff566c5b16d3700b2393a35d86c Mon Sep 17 00:00:00 2001 From: Mihail Chindris Date: Wed, 16 Jun 2021 18:24:57 +0100 Subject: [PATCH] Update 19 aparitions of same 4 lines with function This improve code readbility and reduce code size. Signed-off-by: Mihail Chindris --- channel.c | 33 ++++++----------------------- context.c | 44 +++++++++----------------------------- device.c | 58 ++++++++++++++------------------------------------- iio-private.h | 10 +++++++++ 4 files changed, 42 insertions(+), 103 deletions(-) diff --git a/channel.c b/channel.c index d0f3a2dc4..850bcb4a1 100644 --- a/channel.c +++ b/channel.c @@ -207,60 +207,39 @@ ssize_t iio_snprintf_channel_xml(char *ptr, ssize_t len, ret = iio_snprintf(ptr, len, "id); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + iio_update_xml_indexes(ret, &ptr, &len, &alen); if (chn->name) { ret = iio_snprintf(ptr, len, " name=\"%s\"", chn->name); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + iio_update_xml_indexes(ret, &ptr, &len, &alen); } ret = iio_snprintf(ptr, len, " type=\"%s\" >", chn->is_output ? "output" : "input"); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + iio_update_xml_indexes(ret, &ptr, &len, &alen); if (chn->is_scan_element) { ret = iio_snprintf_scan_element_xml(ptr, len, chn); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + iio_update_xml_indexes(ret, &ptr, &len, &alen); } for (i = 0; i < chn->nb_attrs; i++) { ret = iio_snprintf_chan_attr_xml(ptr, len, &chn->attrs[i]); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + iio_update_xml_indexes(ret, &ptr, &len, &alen); } ret = iio_snprintf(ptr, len, ""); if (ret < 0) return ret; - alen += ret; - return alen; + return alen + ret; } const char * iio_channel_get_id(const struct iio_channel *chn) diff --git a/context.c b/context.c index 30398ff8b..d74f6a046 100644 --- a/context.c +++ b/context.c @@ -64,12 +64,7 @@ static ssize_t sanitize_xml(char *ptr, ssize_t len, const char *str) if (ret < 0) return ret; - if (ptr) { - len -= ret; - ptr += ret; - } - - count += ret; + iio_update_xml_indexes(ret, &ptr, &len, &count); } return count; @@ -92,11 +87,7 @@ static ssize_t iio_snprintf_context_xml(char *ptr, ssize_t len, if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + iio_update_xml_indexes(ret, &ptr, &len, &alen); for (i = 0; i < ctx->nb_attrs; i++) { ret = iio_snprintf(ptr, len, @@ -104,48 +95,33 @@ static ssize_t iio_snprintf_context_xml(char *ptr, ssize_t len, ctx->attrs[i]); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + iio_update_xml_indexes(ret, &ptr, &len, &alen); ret = sanitize_xml(ptr, len, ctx->values[i]); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; - + + iio_update_xml_indexes(ret, &ptr, &len, &alen); ret = iio_snprintf(ptr, len, "\" />"); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); } for (i = 0; i < ctx->nb_devices; i++) { ret = iio_snprintf_device_xml(ptr, len, ctx->devices[i]); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); } ret = iio_snprintf(ptr, len, ""); if (ret < 0) return ret; - alen += ret; - return alen; + return alen + ret; } /* Returns a string containing the XML representation of this context */ diff --git a/device.c b/device.c index 15a767edf..1b0612412 100644 --- a/device.c +++ b/device.c @@ -38,52 +38,36 @@ ssize_t iio_snprintf_device_xml(char *ptr, ssize_t len, ret = iio_snprintf(ptr, len, "id); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + iio_update_xml_indexes(ret, &ptr, &len, &alen); if (dev->name) { ret = iio_snprintf(ptr, len, " name=\"%s\"", dev->name); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); } if (dev->label) { ret = iio_snprintf(ptr, len, " label=\"%s\"", dev->label); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); } ret = iio_snprintf(ptr, len, " >"); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); for (i = 0; i < dev->nb_channels; i++) { ret = iio_snprintf_channel_xml(ptr, len, dev->channels[i]); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); } for (i = 0; i < dev->attrs.num; i++) { @@ -91,11 +75,8 @@ ssize_t iio_snprintf_device_xml(char *ptr, ssize_t len, IIO_ATTR_TYPE_DEVICE); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); } for (i = 0; i < dev->buffer_attrs.num; i++) { @@ -103,11 +84,8 @@ ssize_t iio_snprintf_device_xml(char *ptr, ssize_t len, IIO_ATTR_TYPE_BUFFER); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); } for (i = 0; i < dev->debug_attrs.num; i++) { @@ -115,19 +93,15 @@ ssize_t iio_snprintf_device_xml(char *ptr, ssize_t len, IIO_ATTR_TYPE_DEBUG); if (ret < 0) return ret; - if (ptr) { - ptr += ret; - len -= ret; - } - alen += ret; + + iio_update_xml_indexes(ret, &ptr, &len, &alen); } ret = iio_snprintf(ptr, len, ""); if (ret < 0) return ret; - alen += ret; - return alen; + return alen + ret; } int add_iio_dev_attr(struct iio_dev_attrs *attrs, const char *attr, diff --git a/iio-private.h b/iio-private.h index b5bbf93f3..3ab87d84c 100644 --- a/iio-private.h +++ b/iio-private.h @@ -271,4 +271,14 @@ int add_iio_dev_attr(struct iio_dev_attrs *attrs, const char *attr, ssize_t __iio_printf iio_snprintf(char *buf, size_t len, const char *fmt, ...); +static inline void iio_update_xml_indexes(ssize_t ret, char **ptr, ssize_t *len, + ssize_t *alen) +{ + if (*ptr) { + *ptr += ret; + *len -= ret; + } + *alen += ret; +} + #endif /* __IIO_PRIVATE_H__ */