~ubuntu-branches/ubuntu/saucy/haskell-hs-bibutils/saucy

« back to all changes in this revision

Viewing changes to bibutils/xml_encoding.c

  • Committer: Package Import Robot
  • Author(s): Joachim Breitner
  • Date: 2011-09-26 17:57:15 UTC
  • Revision ID: package-import@ubuntu.com-20110926175715-muzp5giy0rzonss2
Tags: upstream-4.12
ImportĀ upstreamĀ versionĀ 4.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * xml_getencoding.c
 
3
 *
 
4
 * Copyright (c) Chris Putnam 2007-2010
 
5
 *
 
6
 * Source code released under the GPL
 
7
 *
 
8
 */
 
9
#include <stdio.h>
 
10
#include <stdlib.h>
 
11
#include <string.h>
 
12
#include "newstr.h"
 
13
#include "newstr_conv.h"
 
14
#include "xml.h"
 
15
#include "xml_encoding.h"
 
16
 
 
17
static int
 
18
xml_getencodingr( xml *node )
 
19
{
 
20
        newstr *s;
 
21
        int n = CHARSET_UNKNOWN, m;
 
22
        if ( xml_tagexact( node, "xml" ) ) {
 
23
                s = xml_getattrib( node, "encoding" );
 
24
                if ( s && s->data ) {
 
25
                        if ( !strcasecmp( s->data, "UTF-8" ) ) 
 
26
                                n = CHARSET_UNICODE;
 
27
                        else n = get_charset( s->data );
 
28
                        if ( n==CHARSET_UNKNOWN ) {
 
29
                                fprintf( stderr, "Warning: did not recognize "
 
30
                                        "encoding '%s'\n", s->data );
 
31
                        }
 
32
                }
 
33
        }
 
34
        if ( node->down ) {
 
35
                m = xml_getencodingr( node->down );
 
36
                if ( m!=CHARSET_UNKNOWN ) n = m;
 
37
        }
 
38
        if ( node->next ) {
 
39
                m = xml_getencodingr( node->next );
 
40
                if ( m!=CHARSET_UNKNOWN ) n = m;
 
41
        }
 
42
        return n;
 
43
}
 
44
 
 
45
int
 
46
xml_getencoding( newstr *s )
 
47
{
 
48
        newstr descriptor;
 
49
        xml descriptxml;
 
50
        int file_charset = CHARSET_UNKNOWN;
 
51
        char *p, *q;
 
52
        p = strstr( s->data, "<?xml" );
 
53
        if ( !p ) p = strstr( s->data, "<?XML" );
 
54
        if ( p ) {
 
55
                q = strstr( p, "?>" );
 
56
                if ( q ) {
 
57
                        newstr_init( &descriptor );
 
58
                        newstr_segcpy( &descriptor, p, q+2 );
 
59
                        xml_init( &descriptxml );
 
60
                        xml_tree( descriptor.data, &descriptxml );
 
61
                        file_charset = xml_getencodingr( &descriptxml );
 
62
                        xml_free( &descriptxml );
 
63
                        newstr_free( &descriptor );
 
64
                        newstr_segdel( s, p, q+2 );
 
65
                }
 
66
        }
 
67
        return file_charset;
 
68
}