[FFmpeg-devel] [PATCH] lavu/error: add av_make_strerr() and av_err2str() convenience utilities
Nicolas George
nicolas.george at normalesup.org
Sun Jun 10 14:07:38 CEST 2012
Le tridi 23 prairial, an CCXX, Stefano Sabatini a écrit :
> These functions are modeled after the corresponding utilities in
> libavutil/timestamp.h.
> ---
> libavutil/error.c | 7 ++-----
> libavutil/error.h | 22 ++++++++++++++++++++++
> 2 files changed, 24 insertions(+), 5 deletions(-)
>
> diff --git a/libavutil/error.c b/libavutil/error.c
> index 16c2587..7f08314 100644
> --- a/libavutil/error.c
> +++ b/libavutil/error.c
> @@ -78,17 +78,14 @@ int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
> int main(void)
> {
> int i;
> - char errbuf[256];
>
> for (i = 0; i < FF_ARRAY_ELEMS(error_entries); i++) {
> struct error_entry *entry = &error_entries[i];
> - av_strerror(entry->num, errbuf, sizeof(errbuf));
> - printf("%d: %s [%s]\n", entry->num, errbuf, entry->tag);
> + printf("%d: %s [%s]\n", entry->num, av_err2str(entry->num), entry->tag);
> }
>
> for (i = 0; i < 256; i++) {
> - av_strerror(-i, errbuf, sizeof(errbuf));
> - printf("%d: %s\n", -i, errbuf);
> + printf("%d: %s\n", -i, av_err2str(-i));
> }
>
> return 0;
> diff --git a/libavutil/error.h b/libavutil/error.h
> index 316734f..0f3945e 100644
> --- a/libavutil/error.h
> +++ b/libavutil/error.h
> @@ -82,6 +82,28 @@
> */
> int av_strerror(int errnum, char *errbuf, size_t errbuf_size);
>
> +#define AV_ERROR_MAX_STRING_SIZE 64
> +
> +/**
> + * Fill the provided buffer with a string containing an error string
> + * representation.
> + *
> + * @param buf a buffer with size in bytes of at least AV_ERROR_MAX_STRING_SIZE
> + * @param errnum error code to describe
> + * @return the buffer in input, filled with the error description
> + */
> +static inline char *av_make_strerr(char *errbuf, int errnum)
I would be somewhat happier if there was a size argument, and the
MAX_STRING_SIZE was only for the macro with the automatic buffer.
> +{
> + av_strerror(errnum, errbuf, AV_ERROR_MAX_STRING_SIZE);
> + return errbuf;
If the error code is one of our internal ones, the function could return
directly the pointer to the static string and avoid a string copy. I do not
know if it really matters, though, as errors should be rare.
> +}
> +
> +/**
> + * Convenience macro, the return value should be used only directly in
> + * function arguments but never stand-alone.
> + */
> +#define av_err2str(errnum) av_make_strerr((char[AV_ERROR_MAX_STRING_SIZE]){0}, errnum)
This part LGTM.
Regards,
--
Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120610/c408e9fc/attachment.asc>
More information about the ffmpeg-devel
mailing list