Skip to content

Commit

Permalink
Update 19 aparitions of same 4 lines with function
Browse files Browse the repository at this point in the history
This improve code readbility and reduce code size.

Signed-off-by: Mihail Chindris <[email protected]>
  • Loading branch information
Mihail Chindris authored and pcercuei committed Jun 18, 2021
1 parent 0571fcc commit 38aa87f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 103 deletions.
33 changes: 6 additions & 27 deletions channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,60 +207,39 @@ ssize_t iio_snprintf_channel_xml(char *ptr, ssize_t len,
ret = iio_snprintf(ptr, len, "<channel id=\"%s\"", chn->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, "</channel>");
if (ret < 0)
return ret;
alen += ret;

return alen;
return alen + ret;
}

const char * iio_channel_get_id(const struct iio_channel *chn)
Expand Down
44 changes: 10 additions & 34 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -92,60 +87,41 @@ 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,
"<context-attribute name=\"%s\" value=\"",
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, "</context>");
if (ret < 0)
return ret;
alen += ret;

return alen;
return alen + ret;
}

/* Returns a string containing the XML representation of this context */
Expand Down
58 changes: 16 additions & 42 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,96 +38,70 @@ ssize_t iio_snprintf_device_xml(char *ptr, ssize_t len,
ret = iio_snprintf(ptr, len, "<device id=\"%s\"", dev->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++) {
ret = iio_snprintf_xml_attr(ptr, len, dev->attrs.names[i],
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++) {
ret = iio_snprintf_xml_attr(ptr, len, dev->buffer_attrs.names[i],
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++) {
ret = iio_snprintf_xml_attr(ptr, len, dev->debug_attrs.names[i],
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, "</device>");
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,
Expand Down
10 changes: 10 additions & 0 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */

0 comments on commit 38aa87f

Please sign in to comment.