[FFmpeg-devel] [PATCH 2/2] ffprobe: add basic JSON print format.
Clément Bœsch
ubitux at gmail.com
Sat Sep 3 20:11:21 CEST 2011
On Sat, Sep 03, 2011 at 03:23:51PM +0200, Alexander Strasser wrote:
[...]
> > +static const char *json_escape = "\"\\\b\f\n\r\t";
> > +static const char *json_subst = "\"\\bfnrt";
> > +
> > +static char *json_escape_str(const char *s)
> > +{
> > + char *ret, *p;
> > + int i, len = 0;
> > +
> > + for (i = 0; s[i]; i++) {
> > + if (strchr(json_escape, s[i])) len += 2;
> > + else if ((unsigned char)s[i] < 32) len += 6;
> > + else len += 1;
> > + }
> > +
> > + p = ret = av_malloc(len + 1);
> > + if (!p)
> > + return NULL;
> > + for (i = 0; s[i]; i++) {
> > + char *q = strchr(json_escape, s[i]);
> > + if (q) {
> > + *p++ = '\\';
> > + *p++ = json_subst[q - json_escape];
> > + } else if ((unsigned char)s[i] < 32) {
> > + snprintf(p, 7, "\\u00%02x", s[i] & 0xff);
> > + p += 6;
> > + } else {
> > + *p++ = s[i];
> > + }
> > + }
> > + *p = 0;
> > + return ret;
> > +}
>
> What do you think of implementing it like
>
> static char *json_escape_str(const char *s)
> {
> static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t'};
> static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't'};
>
> that?
>
> Would get rid of the pointer-to-char-array indirection and the
> trailing ASCII NUL that terminates the C strings.
>
> Other advantages are:
> - arrays are private to the only function that uses them
> - the array initializers emphasize the symmetry of the two arrays
>
Sure ok. Changed.
> > +static void json_print_fmt(const char *key, const char *fmt, ...)
> > +{
> > + int len;
> > + char *raw_str = NULL, *value_esc = NULL, *key_esc;
>
> NIT: Why is value_esc inited to NULL but key_esc isn't? Or vice versa.
>
value_esc is set to NULL in case of error. key_esc is computed in case of
error (send "end:" label), so it doesn't matter.
BTW, I moved that code outside in another patch, so I'll send yet another
patch set (3 patchs) in a minute (sorry for changing my mind again).
> > + va_list ap;
> > +
> > + va_start(ap, fmt);
> > + len = vsnprintf(NULL, 0, fmt, ap);
> > + va_end(ap);
> > + if (len < 0)
> > + goto end;
> > +
> > + raw_str = av_malloc(len + 1);
> > + if (!raw_str)
> > + goto end;
> > +
> > + va_start(ap, fmt);
> > + len = vsnprintf(raw_str, len + 1, fmt, ap);
> > + va_end(ap);
> > + if (len < 0)
> > + goto end;
> > +
> > + value_esc = json_escape_str(raw_str);
> > +
> > +end:
> > + key_esc = json_escape_str(key);
> > + printf(" \"%s\": \"%s\"",
> > + key_esc ? key_esc : "",
> > + value_esc ? value_esc : "");
> > + av_free(raw_str);
> > + av_free(key_esc);
> > + av_free(value_esc);
> > +}
> > +
[...]
--
Clément B.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 490 bytes
Desc: not available
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20110903/c38cde1a/attachment.asc>
More information about the ffmpeg-devel
mailing list