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

« back to all changes in this revision

Viewing changes to bibutils/doi.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
 * doi.c
 
3
 *
 
4
 * doi_to_url()
 
5
 * Handle outputing DOI as a URL (Endnote and RIS formats)
 
6
 *     1) Append http://dx.doi.org as necessary
 
7
 *     2) Check for overlap with pre-existing URL for the DOI
 
8
 *
 
9
 * is_doi()
 
10
 * Check for DOI buried in another field.
 
11
 *
 
12
 * Copyright (c) Chris Putnam 2008-2010
 
13
 *
 
14
 * Source code released under the GPL
 
15
 */
 
16
#include <stdio.h>
 
17
#include <stdlib.h>
 
18
#include <string.h>
 
19
#include <ctype.h>
 
20
#include "newstr.h"
 
21
#include "fields.h"
 
22
 
 
23
static void
 
24
construct_url( char *prefix, newstr *id, newstr *id_url )
 
25
{
 
26
        if ( !strncasecmp( id->data, "http:", 5 ) )
 
27
                newstr_newstrcpy( id_url, id );
 
28
        else {
 
29
                newstr_strcpy( id_url, prefix );
 
30
                if ( id->data[0]!='/' ) newstr_addchar( id_url, '/' );
 
31
                newstr_newstrcat( id_url, id );
 
32
        }
 
33
}
 
34
 
 
35
static int
 
36
url_exists( fields *info, char *urltag, newstr *doi_url )
 
37
{
 
38
        int i, found = 0;
 
39
        if ( urltag ) {
 
40
                for ( i=0; i<info->nfields && !found; ++i ) {
 
41
                        if ( strcmp( info->tag[i].data, urltag ) )
 
42
                                continue;
 
43
                        if ( !strcmp( info->data[i].data, doi_url->data ) )
 
44
                                found=1;
 
45
                }
 
46
        }
 
47
        return found;
 
48
}
 
49
 
 
50
void
 
51
doi_to_url( fields *info, int n, char *urltag, newstr *doi_url )
 
52
{
 
53
        newstr_empty( doi_url );
 
54
        construct_url( "http://dx.doi.org", &(info->data[n]), doi_url );
 
55
        if ( url_exists( info, urltag, doi_url ) )
 
56
                newstr_empty( doi_url );
 
57
}
 
58
 
 
59
void
 
60
jstor_to_url( fields *info, int n, char *urltag, newstr *jstor_url )
 
61
{
 
62
        newstr_empty( jstor_url );
 
63
        construct_url( "http://www.jstor.org/stable", &(info->data[n]),
 
64
                jstor_url );
 
65
        if ( url_exists( info, urltag, jstor_url ) )
 
66
                newstr_empty( jstor_url );
 
67
}
 
68
 
 
69
void
 
70
pmid_to_url( fields *info, int n, char *urltag, newstr *pmid_url )
 
71
{
 
72
        newstr_empty( pmid_url );
 
73
        construct_url( "http://www.ncbi.nlm.nih.gov/pubmed", &(info->data[n]),
 
74
                        pmid_url );
 
75
        if ( url_exists( info, urltag, pmid_url ) )
 
76
                newstr_empty( pmid_url );
 
77
}
 
78
 
 
79
void
 
80
arxiv_to_url( fields *info, int n, char *urltag, newstr *arxiv_url )
 
81
{
 
82
        newstr_empty( arxiv_url );
 
83
        construct_url( "http://arxiv.org/abs", &(info->data[n]), arxiv_url );
 
84
        if ( url_exists( info, urltag, arxiv_url ) )
 
85
                newstr_empty( arxiv_url );
 
86
}
 
87
 
 
88
/* Rules for the pattern:
 
89
 *   '#' = number
 
90
 *   isalpha() = match precisely (matchcase==1) or match regardless of case
 
91
 *      (matchcase==0)
 
92
 *   all others must match precisely
 
93
 */
 
94
static int
 
95
string_pattern( char *s, char *pattern, int matchcase )
 
96
{
 
97
        int patlen, match, i;
 
98
        patlen = strlen( pattern );
 
99
        if ( strlen( s ) < patlen ) return 0; /* too short */
 
100
        for ( i=0; i<patlen; ++i ) {
 
101
                match = 0;
 
102
                if ( pattern[i]=='#' ) {
 
103
                        if ( isdigit( s[i] ) ) match = 1;
 
104
                } else if ( !matchcase && isalpha( pattern[i] ) ) {
 
105
                        if ( tolower(pattern[i])==tolower(s[i])) match = 1;
 
106
                } else {
 
107
                        if ( pattern[i] == s[i] ) match = 1;
 
108
                }
 
109
                if ( !match ) return 0;
 
110
        }
 
111
        return 1;
 
112
}
 
113
 
 
114
/* science direct is now doing "M3  - doi: DOI: 10.xxxx/xxxxx" */
 
115
int
 
116
is_doi( char *s )
 
117
{
 
118
        if ( string_pattern( s, "##.####/", 0 ) ) return 0;
 
119
        if ( string_pattern( s, "doi:##.####/", 0 ) ) return 4;
 
120
        if ( string_pattern( s, "doi: ##.####/", 0 ) ) return 5;
 
121
        if ( string_pattern( s, "doi: DOI: ##.####/", 0 ) ) return 10;
 
122
        return -1;
 
123
}