Skip to content

Commit

Permalink
context: Improve iio_context_create_xml()
Browse files Browse the repository at this point in the history
- Make it static, since it's only ever used in context.c

- Use pointer-encoded errors, which allows the error codes generated in
  iio_context_create_xml() to be retrieved by the caller function.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Apr 7, 2021
1 parent 3b67aa5 commit 92ae8aa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
21 changes: 8 additions & 13 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,29 +100,24 @@ static ssize_t iio_snprintf_context_xml(char *ptr, ssize_t len,
}

/* Returns a string containing the XML representation of this context */
char * iio_context_create_xml(const struct iio_context *ctx)
static char * iio_context_create_xml(const struct iio_context *ctx)
{
ssize_t len;
char *str;

len = iio_snprintf_context_xml(NULL, 0, ctx);
if (len < 0) {
errno = -len;
return NULL;
}
if (len < 0)
return ERR_TO_PTR(len);

len++; /* room for terminating NULL */
str = malloc(len);
if (!str) {
errno = ENOMEM;
return NULL;
}
if (!str)
return ERR_TO_PTR(-ENOMEM);

len = iio_snprintf_context_xml(str, len, ctx);
if (len < 0) {
errno = -len;
free(str);
return NULL;
return ERR_TO_PTR(len);
}

return str;
Expand Down Expand Up @@ -290,8 +285,8 @@ int iio_context_init(struct iio_context *ctx)

if (!ctx->xml) {
ctx->xml = iio_context_create_xml(ctx);
if (!ctx->xml)
return -ENOMEM;
if (IS_ERR(ctx->xml))
return PTR_TO_ERR(ctx->xml);
}

return 0;
Expand Down
1 change: 0 additions & 1 deletion iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ ssize_t iio_snprintf_channel_xml(char *str, ssize_t slen,
ssize_t iio_snprintf_device_xml(char *str, ssize_t slen,
const struct iio_device *dev);

char *iio_context_create_xml(const struct iio_context *ctx);
int iio_context_init(struct iio_context *ctx);

bool iio_device_is_tx(const struct iio_device *dev);
Expand Down

0 comments on commit 92ae8aa

Please sign in to comment.