tracker r1251 - in trunk: . src/tracker-extract



Author: ifrade
Date: Mon Apr  7 21:03:15 2008
New Revision: 1251
URL: http://svn.gnome.org/viewvc/tracker?rev=1251&view=rev

Log:
Commit #526727. Use standard C functions to parse dates

Modified:
   trunk/ChangeLog
   trunk/src/tracker-extract/tracker-extract-exif.c
   trunk/src/tracker-extract/tracker-extract-png.c
   trunk/src/tracker-extract/tracker-extract-ps.c
   trunk/src/tracker-extract/tracker-extract.c
   trunk/src/tracker-extract/tracker-extract.h

Modified: trunk/src/tracker-extract/tracker-extract-exif.c
==============================================================================
--- trunk/src/tracker-extract/tracker-extract-exif.c	(original)
+++ trunk/src/tracker-extract/tracker-extract-exif.c	Mon Apr  7 21:03:15 2008
@@ -25,30 +25,16 @@
 #include <glib.h>
 #include <libexif/exif-data.h>
 
+#define EXIF_DATE_FORMAT "%Y:%m:%d %H:%M:%S"
 
 static gchar *
-date_to_iso8160 (gchar *exif_date)
+date_to_iso8601 (gchar *exif_date)
 {
         /* ex; date "2007:04:15 15:35:58"
            To
            ex. "2007-04-15T17:35:58+0200 where +0200 is localtime
         */
-
-        gsize len = strlen (exif_date);
-        guint i;
-
-        /* We transform "2007:04:15 15:35:58" into  "2007 04 15 15:35:58" */
-        for (i = 0; i < len && i < 10; i++) {
-                if (exif_date[i] == ':') {
-                        exif_date[i] = ' ';
-                }
-        }
-
-        steps steps_to_do[] = {
-                YEAR, MONTH, DAY, TIME, LAST_STEP
-        };
-
-        return tracker_generic_date_extractor (exif_date, steps_to_do);
+        return tracker_generic_date_to_iso8601 (exif_date, EXIF_DATE_FORMAT);
 }
 
 
@@ -121,7 +107,7 @@
 	{ EXIF_TAG_RELATED_IMAGE_WIDTH, "Image:Width", NULL },
 	{ EXIF_TAG_DOCUMENT_NAME, "Image:Title", NULL },
 	/* { -1, "Image:Album", NULL }, */
-	{ EXIF_TAG_DATE_TIME, "Image:Date", date_to_iso8160 },
+	{ EXIF_TAG_DATE_TIME, "Image:Date", date_to_iso8601 },
 	/* { -1, "Image:Keywords", NULL }, */
 	{ EXIF_TAG_ARTIST, "Image:Creator", NULL },
 	{ EXIF_TAG_USER_COMMENT, "Image:Comments", NULL },

Modified: trunk/src/tracker-extract/tracker-extract-png.c
==============================================================================
--- trunk/src/tracker-extract/tracker-extract-png.c	(original)
+++ trunk/src/tracker-extract/tracker-extract-png.c	Mon Apr  7 21:03:15 2008
@@ -34,20 +34,17 @@
 #include <glib/gstdio.h>
 #include <png.h>
 
+#define RFC1123_DATE_FORMAT "%d %B %Y %H:%M:%S %z"
+
 
 static gchar *
-rfc1123_to_iso8160_date (gchar *rfc_date)
+rfc1123_to_iso8601_date (gchar *rfc_date)
 {
         /* ex. RFC1123 date: "22 May 1997 18:07:10 -0600"
            To
-           ex. ISO8160 date: "2007-05-22T18:07:10-0600"
+           ex. ISO8601 date: "2007-05-22T18:07:10-0600"
         */
-
-        steps steps_to_do[] = {
-                DAY_STR, DAY, MONTH, YEAR, TIME, TIMEZONE, LAST_STEP
-        };
-
-        return tracker_generic_date_extractor (rfc_date, steps_to_do);
+        return tracker_generic_date_to_iso8601 (rfc_date, RFC1123_DATE_FORMAT);
 }
 
 
@@ -65,7 +62,7 @@
   { "Description",        "Image:Description",  NULL},
   { "Comment",            "Image:Comments",     NULL},
   { "Copyright",          "File:Copyright",     NULL},
-  { "Creation Time",      "Image:Date",         rfc1123_to_iso8160_date},
+  { "Creation Time",      "Image:Date",         rfc1123_to_iso8601_date},
   { "Title",              "Image:Title",        NULL},
   { "Software",           "Image:Software",     NULL},
   { "Disclaimer",         "File:License",       NULL},

Modified: trunk/src/tracker-extract/tracker-extract-ps.c
==============================================================================
--- trunk/src/tracker-extract/tracker-extract-ps.c	(original)
+++ trunk/src/tracker-extract/tracker-extract-ps.c	Mon Apr  7 21:03:15 2008
@@ -40,7 +40,6 @@
 #undef getdelim
 #undef getline
 
-
 static ssize_t
 igetdelim (gchar **linebuf, size_t *linebufsz, gint delimiter, FILE *file)
 {
@@ -103,14 +102,10 @@
 {
         /* ex. date: "(18:07 Tuesday 22 May 2007)"
            To
-           ex. ISO8160 date: "2007-05-22T18:07:10-0600"
+           ex. ISO8601 date: "2007-05-22T18:07:10-0600"
         */
 
-        steps steps_to_do[] = {
-                TIME, DAY_STR, DAY, MONTH, YEAR, LAST_STEP
-        };
-
-        return tracker_generic_date_extractor (date + 1, steps_to_do);
+        return tracker_generic_date_to_iso8601 (date, "(%H:%M %A %d %B %Y)");
 }
 
 
@@ -119,14 +114,9 @@
 {
         /* ex. date: "Tue May 22 18:07:10 2007"
            To
-           ex. ISO8160 date: "2007-05-22T18:07:10-0600"
+           ex. ISO8601 date: "2007-05-22T18:07:10-0600"
         */
-
-        steps steps_to_do[] = {
-                DAY_STR, MONTH, DAY, TIME, YEAR, LAST_STEP
-        };
-
-        return tracker_generic_date_extractor (date, steps_to_do);
+        return tracker_generic_date_to_iso8601 (date, "%A %B %d %H:%M:%S %Y");
 }
 
 
@@ -135,14 +125,9 @@
 {
         /* ex. date: "22 May 1997 18:07:10 -0600"
            To
-           ex. ISO8160 date: "2007-05-22T18:07:10-0600"
+           ex. ISO8601 date: "2007-05-22T18:07:10-0600"
         */
-
-        steps steps_to_do[] = {
-                DAY_STR, MONTH, DAY, TIME, YEAR, LAST_STEP
-        };
-
-        return tracker_generic_date_extractor (date, steps_to_do);
+        return tracker_generic_date_to_iso8601 (date, "%d %B %Y %H:%M:%S %z");
 }
 
 
@@ -151,19 +136,14 @@
 {
         /* ex. date: "6:07 PM May 22, 2007"
            To
-           ex. ISO8160 date: "2007-05-22T18:07:10-0600"
+           ex. ISO8601 date: "2007-05-22T18:07:10-0600"
         */
-
-        steps steps_to_do[] = {
-                TIME, DAY_PART, MONTH, DAY, YEAR, LAST_STEP
-        };
-
-        return tracker_generic_date_extractor (date, steps_to_do);
+        return tracker_generic_date_to_iso8601 (date, "%I:%M %p %B %d, %Y");
 }
 
 
 static gchar *
-date_to_iso8160 (gchar *date)
+date_to_iso8601 (gchar *date)
 {
         if (date && date[1] && date[2]) {
                 if (date[0] == '(') {
@@ -185,7 +165,7 @@
                            "6:07 PM May 22, 2007" */
                         return hour_month_day_date (date);
 
-                }
+                } 
         }
 
         return NULL;
@@ -233,7 +213,7 @@
                                                      g_strdup ("Doc:Author"), g_strdup (line + 11));
 
 			} else if (!header_finished && strncmp (line, "%%CreationDate:", 15) == 0) {
-                                gchar *date = date_to_iso8160 (line + 16);
+                                gchar *date = date_to_iso8601 (line + 16);
                                 if (date) {
                                         g_hash_table_insert (metadata, g_strdup ("Doc:Created"), date);
                                 }

Modified: trunk/src/tracker-extract/tracker-extract.c
==============================================================================
--- trunk/src/tracker-extract/tracker-extract.c	(original)
+++ trunk/src/tracker-extract/tracker-extract.c	Mon Apr  7 21:03:15 2008
@@ -23,7 +23,6 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <locale.h>
-#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 #ifndef OS_WIN32
@@ -34,263 +33,41 @@
 #include <glib.h>
 #include <gmodule.h>
 
+#define _XOPEN_SOURCE
+#include <time.h>
+
 #include "tracker-extract.h"
 
 #define MAX_MEM 128
 #define MAX_MEM_AMD64 512
 
-typedef enum {
-	IGNORE_METADATA,
-	NO_METADATA,
-	DOC_METADATA,
-	IMAGE_METADATA,
-	VIDEO_METADATA,
-	AUDIO_METADATA
-} MetadataFileType;
-
+#define ISO8601_FORMAT "%Y-%m-%dT%H:%M:%S%z"
 
 GArray *extractors = NULL;
 
 
-static gboolean
-read_day (gchar *day, gint *ret)
-{
-        guint64 val = g_ascii_strtoull (day, NULL, 10);
-
-        if (val == G_MAXUINT64) {
-                return FALSE;
-        } else {
-                *ret = CLAMP (val, 1, 31);
-                return TRUE;
-        }
-}
-
-
-static gboolean
-read_month (gchar *month, gint *ret)
-{
-        if (g_ascii_isdigit (month[0])) {
-                /* month is already given in a numerical form */
-                guint64 val = g_ascii_strtoull (month, NULL, 10) - 1;
-
-                if (val == G_MAXUINT64) {
-                        return FALSE;
-                } else {
-                        *ret = CLAMP (val, 0, 11);
-                        return TRUE;
-                }
-        } else {
-                /* month is given by its name */
-                gchar *months[] = {
-                        "Ja", "Fe", "Mar", "Av", "Ma", "Jun",
-                        "Jul", "Au", "Se", "Oc", "No", "De",
-                        NULL };
-                gchar **tmp;
-                gint i;
-
-                for (tmp = months, i = 0; *tmp; tmp++, i++) {
-                        if (g_str_has_prefix (month, *tmp)) {
-                                *ret = i;
-                                return TRUE;
-                        }
-                }
-
-                return FALSE;
-        }
-}
-
-
-static gboolean
-read_year (gchar *year, gint *ret)
-{
-        guint64 val = g_ascii_strtoull (year, NULL, 10);
-
-        if (val == G_MAXUINT64) {
-                return FALSE;
-        } else {
-                *ret = CLAMP (val, 0, G_MAXINT) - 1900;
-                return TRUE;
-        }
-}
-
-
-static gboolean
-read_time (gchar *time, gint *ret_hour, gint *ret_min, gint *ret_sec)
-{
-        /* Hours */
-        guint64 val = g_ascii_strtoull (time, &time, 10);
-        if (val == G_MAXUINT64 || *time != ':') {
-                return FALSE;
-        }
-        *ret_hour = CLAMP (val, 0, 24);
-        time++;
-
-        /* Minutes */
-        val = g_ascii_strtoull (time, &time, 10);
-        if (val == G_MAXUINT64) {
-                return FALSE;
-        }
-        *ret_min = CLAMP (val, 0, 99);
-
-        if (*time == ':') {
-                /* Yeah! We have seconds. */
-                time++;
-                val = g_ascii_strtoull (time, &time, 10);
-                if (val == G_MAXUINT64) {
-                        return FALSE;
-                }
-                *ret_sec = CLAMP (val, 0, 99);
-        }
-
-        return TRUE;
-}
-
-
-static gboolean
-read_timezone (gchar *timezone, gchar *ret, gsize ret_len)
-{
-        /* checks that we are not reading word "GMT" instead of timezone */
-        if (timezone[0] && g_ascii_isdigit (timezone[1])) {
-                gchar *n = timezone + 1;  /* "+1" to not read timezone sign */
-                gint  hours, minutes;
-
-                if (strlen (n) < 4) {
-                        return FALSE;  
-                }
-
-                #define READ_PAIR(ret, min, max)                        \
-                {                                                       \
-                        gchar buff[3];                                  \
-                        guint64 val;                                    \
-                        buff[0] = n[0];                                 \
-                        buff[1] = n[1];                                 \
-                        buff[2] = '\0';                                 \
-                                                                        \
-                        val = g_ascii_strtoull (buff, NULL, 10);        \
-                        if (val == G_MAXUINT64) {                       \
-                                return FALSE;                           \
-                        }                                               \
-                        ret = CLAMP (val, min, max);                    \
-                        n += 2;                                         \
-                }
-
-                READ_PAIR (hours, 0, 24);
-                if (*n == ':') {
-                        /* that should not happen, but he... */
-                        n++;
-                }
-                READ_PAIR (minutes, 0, 99);
-
-                g_snprintf (ret, ret_len,
-                            "%c%.2d%.2d",
-                            (timezone[0] == '-' ? '-' : '+'),
-                            hours, minutes);
-
-                #undef READ_PAIR
-
-                return TRUE;
-
-        } else if (g_ascii_isalpha (timezone[1])) {
-                /* GMT, so we keep current time */
-                if (ret_len >= 2) {
-                        ret[0] = 'Z';
-                        ret[1] = '\0';
-                        return TRUE;
-
-                } else {
-                        return FALSE;
-                }
-
-        } else {
-                return FALSE;
-        }
-}
-
-
 gchar *
-tracker_generic_date_extractor (gchar *date, steps steps_to_do[])
+tracker_generic_date_to_iso8601 (const gchar *date, const gchar *format)
 {
-        gchar buffer[20], timezone_buffer[6];
-        gchar **date_parts;
-        gsize count;
-        guint i;
-
-        g_return_val_if_fail (date, NULL);
-
-        struct tm tm;
-        memset (&tm, 0, sizeof (struct tm));
-
-        date_parts = g_strsplit (date, " ", 0);
-
-        for (i = 0; date_parts[i] && steps_to_do[i] != LAST_STEP; i++) {
-                gchar *part = date_parts[i];
-
-                switch (steps_to_do[i]) {
-                        case TIME: {
-                                if (!read_time (part, &tm.tm_hour, &tm.tm_min, &tm.tm_sec)) {
-                                        goto error;
-                                }
-                                break;
-                        }
-                        case TIMEZONE: {
-                                if (!read_timezone (part,
-                                                    timezone_buffer,
-                                                    G_N_ELEMENTS (timezone_buffer))) {
-                                        goto error;
-                                }
-                                break;
-                        }
-                        case DAY_PART: {
-                                if (strcmp (part, "AM") == 0) {
-                                        /* We do nothing... */
-                                } else if (strcmp (part, "PM") == 0) {
-                                        tm.tm_hour += 12;
-                                }
-                                break;
-                        }
-                        case DAY_STR: {
-                                /* We do not care about monday, tuesday, etc. */
-                                break;
-                        }
-                        case DAY: {
-                                if (!read_day (part, &tm.tm_mday)) {
-                                        goto error;
-                                }
-                                break;
-                        }
-                        case MONTH: {
-                                if (!read_month (part, &tm.tm_mon)) {
-                                        goto error;
-                                }
-                                break;
-                        }
-                        case YEAR : {
-                                if (!read_year (part, &tm.tm_year)) {
-                                        goto error;
-                                }
-                                break;
-                        }
-                        default: {
-                                /* that cannot happen! */
-                                g_strfreev (date_parts);
-                                g_return_val_if_reached (NULL);
-                        }
-                }
-        }
-
-        count = strftime (buffer, sizeof (buffer), "%FT%T", &tm);
-
-        g_strfreev (date_parts);
 
-        if (count > 0) {
-                return g_strconcat (buffer, timezone_buffer, NULL);
-        } else {
+        gchar *processed;
+        gchar *result;
+        struct tm date_tm;
+        
+        memset (&date_tm, 0, sizeof (struct tm));
+
+        processed = strptime (date, format, &date_tm);
+        
+        if (processed == NULL) {
+                // Unable to parse the input
                 return NULL;
         }
 
- error:
-        g_strfreev (date_parts);
-        return NULL;
+        result = g_malloc (sizeof (char)*25);
+
+        strftime (result, 25, ISO8601_FORMAT , &date_tm);
+
+        return result;
 }
 
 

Modified: trunk/src/tracker-extract/tracker-extract.h
==============================================================================
--- trunk/src/tracker-extract/tracker-extract.h	(original)
+++ trunk/src/tracker-extract/tracker-extract.h	Mon Apr  7 21:03:15 2008
@@ -22,18 +22,6 @@
 
 #include <glib.h>
 
-
-typedef enum {
-        TIME = 0,       /* hh:mm:ss (seconds are optionals) */
-        TIMEZONE,       /* time added to current time */
-        DAY_PART,       /* AM or PM?  */
-        DAY_STR,        /* Monday, Tuesday, etc. */
-        DAY,            /* day 01, 02, 03, or... 31 in a month */
-        MONTH,          /* month? 0 to 11. Or, Jan, Feb, etc. */
-        YEAR,           /* 1900 - year */
-        LAST_STEP       /* This is the end... The end my friend... */
-} steps;
-
 typedef struct TrackerExtractorData TrackerExtractorData;
 typedef TrackerExtractorData * (* TrackerExtractorDataFunc) (void);
 
@@ -45,7 +33,7 @@
 };
 
 
-gchar *         tracker_generic_date_extractor (gchar *date, steps steps_to_do[]);
+gchar *         tracker_generic_date_to_iso8601 (const gchar *date, const gchar *format);
 
 gboolean        tracker_is_empty_string (const gchar *s);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]