]> git.sesse.net Git - vlc/blobdiff - modules/misc/xml/libxml.c
Require libxml2 < 2.7
[vlc] / modules / misc / xml / libxml.c
index 8af66b096e494e2a67cd3ca75409db111164928f..09c200e80e476d127dcd1b188c4a4e9d006193e5 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 
 #include "vlc_block.h"
 #include "vlc_stream.h"
 #include <libxml/xmlreader.h>
 #include <libxml/catalog.h>
 
+#if !defined (LIBXML_VERSION) || (LIBXML_VERSION > 20700)
+# error Stale config.cache detected. Erase it and re-run configure.
+#endif
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
-vlc_module_begin();
-    set_description( _("XML Parser (using libxml2)") );
-    set_capability( "xml", 10 );
-    set_callbacks( Open, Close );
-vlc_module_end();
+vlc_module_begin ()
+    set_description( N_("XML Parser (using libxml2)") )
+    set_capability( "xml", 10 )
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 struct xml_reader_sys_t
 {
@@ -56,12 +65,15 @@ static char *ReaderName( xml_reader_t * );
 static char *ReaderValue( xml_reader_t * );
 static int ReaderNextAttr( xml_reader_t * );
 
-static int ReaderUseDTD ( xml_reader_t *, vlc_bool_t );
+static int ReaderUseDTD ( xml_reader_t *, bool );
 
 static void CatalogLoad( xml_t *, const char * );
 static void CatalogAdd( xml_t *, const char *, const char *, const char * );
 static int StreamRead( void *p_context, char *p_buffer, int i_buffer );
 
+static unsigned refs = 0;
+static vlc_mutex_t lock = VLC_STATIC_MUTEX;
+
 /*****************************************************************************
  * Module initialization
  *****************************************************************************/
@@ -69,6 +81,14 @@ static int Open( vlc_object_t *p_this )
 {
     xml_t *p_xml = (xml_t *)p_this;
 
+    if( !xmlHasFeature( XML_WITH_THREAD ) )
+        return VLC_EGENERIC;
+
+    vlc_mutex_lock( &lock );
+    if( refs++ == 0 )
+        xmlInitParser();
+    vlc_mutex_unlock( &lock );
+
     p_xml->pf_reader_create = ReaderCreate;
     p_xml->pf_reader_delete = ReaderDelete;
 
@@ -83,6 +103,12 @@ static int Open( vlc_object_t *p_this )
  *****************************************************************************/
 static void Close( vlc_object_t *p_this )
 {
+    vlc_mutex_lock( &lock );
+    if( --refs == 0 )
+        xmlCleanupParser();
+    vlc_mutex_unlock( &lock );
+
+    VLC_UNUSED(p_this);
     return;
 }
 
@@ -91,6 +117,7 @@ static void Close( vlc_object_t *p_this )
  *****************************************************************************/
 static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
 {
+    VLC_UNUSED(p_xml);
     if( !psz_filename ) xmlInitializeCatalog();
     else xmlLoadCatalog( psz_filename );
 }
@@ -98,7 +125,8 @@ static void CatalogLoad( xml_t *p_xml, const char *psz_filename )
 static void CatalogAdd( xml_t *p_xml, const char *psz_arg1,
                           const char *psz_arg2, const char *psz_filename )
 {
-    xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2, 
+    VLC_UNUSED(p_xml);
+    xmlCatalogAdd( (unsigned char*)psz_arg1, (unsigned char*)psz_arg2,
         (unsigned char*)psz_filename );
 }
 
@@ -109,6 +137,7 @@ static void ReaderErrorHandler( void *p_arg, const char *p_msg,
                                 xmlParserSeverities severity,
                                 xmlTextReaderLocatorPtr locator)
 {
+    VLC_UNUSED(severity);
     xml_reader_t *p_reader = (xml_reader_t *)p_arg;
     int line = xmlTextReaderLocatorLineNumber( locator );
     msg_Err( p_reader->p_xml, "XML parser error (line %d) : %s", line, p_msg );
@@ -125,11 +154,22 @@ static xml_reader_t *ReaderCreate( xml_t *p_xml, stream_t *p_stream )
     if( !p_libxml_reader )
     {
         msg_Err( p_xml, "failed to create XML parser" );
-        return 0;
+        return NULL;
     }
 
     p_reader = malloc( sizeof(xml_reader_t) );
+    if( !p_reader )
+    {
+        xmlFreeTextReader( p_libxml_reader );
+        return NULL;
+    }
     p_reader->p_sys = p_sys = malloc( sizeof(xml_reader_sys_t) );
+    if( !p_sys )
+    {
+        xmlFreeTextReader( p_libxml_reader );
+        free( p_reader );
+        return NULL;
+    }
     p_reader->p_sys->p_reader = p_libxml_reader;
     p_reader->p_xml = p_xml;
 
@@ -155,7 +195,7 @@ static void ReaderDelete( xml_reader_t *p_reader )
     free( p_reader );
 }
 
-static int ReaderUseDTD ( xml_reader_t *p_reader, vlc_bool_t b_use )
+static int ReaderUseDTD ( xml_reader_t *p_reader, bool b_use )
 {
     /* Activate DTD validation */
     xmlTextReaderSetParserProp( p_reader->p_sys->p_reader,
@@ -234,5 +274,5 @@ static int ReaderNextAttr( xml_reader_t *p_reader )
 static int StreamRead( void *p_context, char *p_buffer, int i_buffer )
 {
     stream_t *s = (stream_t*)p_context;
-    return stream_Read( s, p_buffer, i_buffer );    
+    return stream_Read( s, p_buffer, i_buffer );
 }