[vlc-commits] strings: Adding an helper to convert iso8601 durations

Hugo Beauzée-Luyssen git at videolan.org
Tue Jan 24 23:21:49 CET 2012


vlc/vlc-1.2 | branch: master | Hugo Beauzée-Luyssen <beauze.h at gmail.com> | Wed Dec 14 18:21:46 2011 +0100| [012d50b9d9078ddd323f06f856fb779c4a1b6236] | committer: Jean-Baptiste Kempf

strings: Adding an helper to convert iso8601 durations

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>
(cherry picked from commit 0b396ef1e6c2ec2bf370339e8df4a8790647eec9)

Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-1.2.git/?a=commit;h=012d50b9d9078ddd323f06f856fb779c4a1b6236
---

 include/vlc_strings.h |    2 +
 src/libvlccore.sym    |    1 +
 src/text/strings.c    |   59 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/include/vlc_strings.h b/include/vlc_strings.h
index 92e43a2..9b4d094 100644
--- a/include/vlc_strings.h
+++ b/include/vlc_strings.h
@@ -53,6 +53,8 @@ VLC_API char * str_format( vlc_object_t *, const char * );
 VLC_API void filename_sanitize( char * );
 VLC_API void path_sanitize( char * );
 
+VLC_API time_t str_duration( const char * );
+
 /**
  * @}
  */
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 0da29f5..5e84697 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -426,6 +426,7 @@ stream_vaControl
 str_format
 str_format_meta
 str_format_time
+str_duration
 subpicture_Delete
 subpicture_New
 subpicture_NewFromPicture
diff --git a/src/text/strings.c b/src/text/strings.c
index a1df0bb..4b51a6b 100644
--- a/src/text/strings.c
+++ b/src/text/strings.c
@@ -1275,3 +1275,62 @@ out:
     free (path);
     return ret; /* unknown scheme */
 }
+
+/*
+  Decodes a duration as defined by ISO 8601
+  http://en.wikipedia.org/wiki/ISO_8601#Durations
+  @param str A null-terminated string to convert
+  @return: The duration in seconds. -1 if an error occured.
+
+  Exemple input string: "PT0H9M56.46S"
+ */
+time_t str_duration( const char *psz_duration )
+{
+    bool        timeDesignatorReached = false;
+    time_t      res = 0;
+    char*       end_ptr;
+
+    if ( psz_duration == NULL )
+        return -1;
+    if ( ( *(psz_duration++) ) != 'P' )
+        return -1;
+    do
+    {
+        double number = strtod( psz_duration, &end_ptr );
+        double      mul = 0;
+        if ( psz_duration != end_ptr )
+            psz_duration = end_ptr;
+        switch( *psz_duration )
+        {
+            case 'M':
+            {
+                //M can mean month or minutes, if the 'T' flag has been reached.
+                //We don't handle months though.
+                if ( timeDesignatorReached == true )
+                    mul = 60.0;
+                break ;
+            }
+            case 'Y':
+            case 'W':
+                break ; //Don't handle this duration.
+            case 'D':
+                mul = 86400.0;
+                break ;
+            case 'T':
+                timeDesignatorReached = true;
+                break ;
+            case 'H':
+                mul = 3600.0;
+                break ;
+            case 'S':
+                mul = 1.0;
+                break ;
+            default:
+                break ;
+        }
+        res += (time_t)(mul * number);
+        if ( *psz_duration )
+            psz_duration++;
+    } while ( *psz_duration );
+    return res;
+}



More information about the vlc-commits mailing list