avidemux: Simplifying code

Greatly simplify the IDIT chunk handling by using sscanf
instead of 'manually' parsing. Also replaces strncasecmp and
is_alpha/is_digit with glib versions.
This commit is contained in:
Thiago Santos 2010-01-08 10:44:44 -03:00
parent 7024ce14cf
commit c0e184641a

View file

@ -45,7 +45,6 @@
#endif #endif
#include <string.h> #include <string.h>
#include <ctype.h>
#include "gst/riff/riff-media.h" #include "gst/riff/riff-media.h"
#include "gstavidemux.h" #include "gstavidemux.h"
@ -3101,79 +3100,46 @@ gst_avi_demux_add_date_tag (GstAviDemux * avi, gint y, gint m, gint d)
g_date_free (date); g_date_free (date);
} }
#define SKIP_TOKEN(data,size,label) \
while (size > 0 && !isspace (data[0])) { \
data++; \
size--; \
} \
while (size > 0 && isspace (data[0])) { \
data++; \
size--; \
} \
if (size == 0) \
goto label;
#define SKIP_DIGIT(data,size,label) \
while (size > 0 && isdigit (data[0])) { \
data++; \
size--; \
} \
while (size > 0 && !isdigit (data[0])) { \
data++; \
size--; \
} \
if (size == 0) \
goto label;
static void static void
gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data, guint size) gst_avi_demux_parse_idit_nums_only (GstAviDemux * avi, gchar * data, guint size)
{ {
gint y, m, d; gint y, m, d;
gint ret;
/* parse the year */ ret = sscanf (data, "%d:%d:%d", &y, &m, &d);
y = atoi (data); if (ret != 3) {
SKIP_DIGIT (data, size, parse_fail); GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
return;
/* parse the month */ }
m = atoi (data);
SKIP_DIGIT (data, size, parse_fail);
/* parse the day */
d = atoi (data);
gst_avi_demux_add_date_tag (avi, y, m, d); gst_avi_demux_add_date_tag (avi, y, m, d);
return;
parse_fail:
GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
} }
static gint static gint
get_month_num (gchar * data, guint size) get_month_num (gchar * data, guint size)
{ {
if (strncasecmp (data, "jan", 3) == 0) { if (g_ascii_strncasecmp (data, "jan", 3) == 0) {
return 1; return 1;
} else if (strncasecmp (data, "feb", 3) == 0) { } else if (g_ascii_strncasecmp (data, "feb", 3) == 0) {
return 2; return 2;
} else if (strncasecmp (data, "mar", 3) == 0) { } else if (g_ascii_strncasecmp (data, "mar", 3) == 0) {
return 3; return 3;
} else if (strncasecmp (data, "apr", 3) == 0) { } else if (g_ascii_strncasecmp (data, "apr", 3) == 0) {
return 4; return 4;
} else if (strncasecmp (data, "may", 3) == 0) { } else if (g_ascii_strncasecmp (data, "may", 3) == 0) {
return 5; return 5;
} else if (strncasecmp (data, "jun", 3) == 0) { } else if (g_ascii_strncasecmp (data, "jun", 3) == 0) {
return 6; return 6;
} else if (strncasecmp (data, "jul", 3) == 0) { } else if (g_ascii_strncasecmp (data, "jul", 3) == 0) {
return 7; return 7;
} else if (strncasecmp (data, "aug", 3) == 0) { } else if (g_ascii_strncasecmp (data, "aug", 3) == 0) {
return 8; return 8;
} else if (strncasecmp (data, "sep", 3) == 0) { } else if (g_ascii_strncasecmp (data, "sep", 3) == 0) {
return 9; return 9;
} else if (strncasecmp (data, "oct", 3) == 0) { } else if (g_ascii_strncasecmp (data, "oct", 3) == 0) {
return 10; return 10;
} else if (strncasecmp (data, "nov", 3) == 0) { } else if (g_ascii_strncasecmp (data, "nov", 3) == 0) {
return 11; return 11;
} else if (strncasecmp (data, "dec", 3) == 0) { } else if (g_ascii_strncasecmp (data, "dec", 3) == 0) {
return 12; return 12;
} }
@ -3183,28 +3149,20 @@ get_month_num (gchar * data, guint size)
static void static void
gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data, guint size) gst_avi_demux_parse_idit_text (GstAviDemux * avi, gchar * data, guint size)
{ {
gint y, m, d; gint year, month, day;
gint hour, min, sec;
gint ret;
gchar weekday[16];
gchar monthstr[16];
/* skip the week day */ ret = sscanf (data, "%s %s %d %d:%d:%d %d", weekday, monthstr, &day, &hour,
SKIP_TOKEN (data, size, parse_fail); &min, &sec, &year);
if (ret != 7) {
/* get the month */ GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
m = get_month_num (data, size); return;
SKIP_TOKEN (data, size, parse_fail); }
month = get_month_num (monthstr, strlen (monthstr));
d = atoi (data); gst_avi_demux_add_date_tag (avi, year, month, day);
SKIP_TOKEN (data, size, parse_fail);
/* skip the hour */
SKIP_TOKEN (data, size, parse_fail);
y = atoi (data);
gst_avi_demux_add_date_tag (avi, y, m, d);
return;
parse_fail:
GST_WARNING_OBJECT (avi, "Failed to parse IDIT tag");
} }
static void static void
@ -3212,6 +3170,7 @@ gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
{ {
gchar *data = (gchar *) GST_BUFFER_DATA (buf); gchar *data = (gchar *) GST_BUFFER_DATA (buf);
guint size = GST_BUFFER_SIZE (buf); guint size = GST_BUFFER_SIZE (buf);
gchar *safedata = NULL;
/* /*
* According to: * According to:
@ -3226,7 +3185,7 @@ gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
*/ */
/* skip eventual initial whitespace */ /* skip eventual initial whitespace */
while (size > 0 && isspace (data[0])) { while (size > 0 && g_ascii_isspace (data[0])) {
data++; data++;
size--; size--;
} }
@ -3235,15 +3194,24 @@ gst_avi_demux_parse_idit (GstAviDemux * avi, GstBuffer * buf)
goto non_parsable; goto non_parsable;
} }
/* make a safe copy to add a \0 to the end of the string */
safedata = g_malloc (sizeof (gchar) * size + 1);
memcpy (safedata, data, size);
safedata[size] = '\0';
/* test if the first char is a alpha or a number */ /* test if the first char is a alpha or a number */
if (isdigit (data[0])) { if (g_ascii_isdigit (data[0])) {
gst_avi_demux_parse_idit_nums_only (avi, data, size); gst_avi_demux_parse_idit_nums_only (avi, safedata, size);
g_free (safedata);
return; return;
} else if (isalpha (data[0])) { } else if (g_ascii_isalpha (data[0])) {
gst_avi_demux_parse_idit_text (avi, data, size); gst_avi_demux_parse_idit_text (avi, safedata, size);
g_free (safedata);
return; return;
} }
g_free (safedata);
non_parsable: non_parsable:
GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info"); GST_WARNING_OBJECT (avi, "IDIT tag has no parsable info");
} }