[FFmpeg-devel] [PATCH] lavu/error: add av_make_strerr() and av_err2str() convenience utilities

Stefano Sabatini stefasab at gmail.com
Sun Jun 10 16:21:09 CEST 2012


On date Sunday 2012-06-10 14:07:38 +0200, Nicolas George encoded:
> 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.

Changed. Name of the function renamed to:
av_make_error_string()

which makes more sense, I pondered that against the shorter variant
av_make_errstr() but I tend to prefer the longer variant.

> > +{
> > +    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.

I'd prefer to keep things simple, especially considering that the
passed string is not const and can be changed later.
 
> > +}
> > +
> > +/**
> > + * 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.

Updated.
-- 
FFmpeg = Formidable and Fiendish Multimedia Ponderous Elfic Goblin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0004-lavu-error-add-av_make_error_string-and-av_err2str-c.patch
Type: text/x-diff
Size: 2646 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20120610/361c966c/attachment.bin>


More information about the ffmpeg-devel mailing list