1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/*
* risin.c
*
* Copyright (c) Chris Putnam 2003-2009
*
* Program and source code released under the GPL
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "newstr.h"
#include "newstr_conv.h"
#include "fields.h"
#include "name.h"
#include "title.h"
#include "serialno.h"
#include "reftypes.h"
#include "doi.h"
#include "risin.h"
/* RIS definition of a tag is strict:
character 1 = uppercase alphabetic character
character 2 = uppercase alphabetic character or digit
character 3 = space (ansi 32)
character 4 = space (ansi 32)
character 5 = dash (ansi 45)
character 6 = space (ansi 32)
*/
static int
risin_istag( char *buf )
{
if (! (buf[0]>='A' && buf[0]<='Z') ) return 0;
if (! (((buf[1]>='A' && buf[1]<='Z'))||(buf[1]>='0'&&buf[1]<='9')) )
return 0;
if (buf[2]!=' ') return 0;
if (buf[3]!=' ') return 0;
if (buf[4]!='-') return 0;
if (buf[5]!=' ') return 0;
return 1;
}
static int
readmore( FILE *fp, char *buf, int bufsize, int *bufpos, newstr *line )
{
if ( line->len ) return 1;
else return newstr_fget( fp, buf, bufsize, bufpos, line );
}
int
risin_readf( FILE *fp, char *buf, int bufsize, int *bufpos, newstr *line,
newstr *reference, int *fcharset )
{
int haveref = 0, inref = 0, readtoofar = 0;
unsigned char *up;
char *p;
*fcharset = CHARSET_UNKNOWN;
while ( !haveref && readmore( fp, buf, bufsize, bufpos, line ) ) {
if ( !line->data || line->len==0 ) continue;
p = &( line->data[0] );
/* Recognize UTF8 BOM */
up = (unsigned char * ) p;
if ( line->len > 2 &&
up[0]==0xEF && up[1]==0xBB && up[2]==0xBF ) {
*fcharset = CHARSET_UNICODE;
p += 3;
}
/* Each reference starts with 'TY - ' &&
* ends with 'ER - ' */
if ( strncmp(p,"TY - ",6)==0 ) {
if ( !inref ) {
inref = 1;
} else {
/* we've read too far.... */
readtoofar = 1;
inref = 0;
}
}
if ( risin_istag( p ) ) {
if ( !inref ) {
fprintf(stderr,"Warning. Tagged line not "
"in properly started reference.\n");
fprintf(stderr,"Ignored: '%s'\n", p );
} else if ( !strncmp(p,"ER -",5) ) {
inref = 0;
} else {
newstr_addchar( reference, '\n' );
newstr_strcat( reference, p );
}
}
/* not a tag, but we'll append to last values ...*/
else if ( inref && strncmp(p,"ER -",5)) {
newstr_addchar( reference, '\n' );
newstr_strcat( reference, p );
}
if ( !inref && reference->len ) haveref = 1;
if ( !readtoofar ) newstr_empty( line );
}
if ( inref ) haveref = 1;
return haveref;
}
static char*
process_line2( newstr *tag, newstr *data, char *p )
{
while ( *p==' ' || *p=='\t' ) p++;
while ( *p && *p!='\r' && *p!='\n' )
newstr_addchar( data, *p++ );
while ( *p=='\r' || *p=='\n' ) p++;
return p;
}
static char*
process_line( newstr *tag, newstr *data, char *p )
{
int i = 0;
while ( i<6 && *p ) {
if ( i<2 ) newstr_addchar( tag, *p );
p++;
i++;
}
while ( *p==' ' || *p=='\t' ) p++;
while ( *p && *p!='\r' && *p!='\n' )
newstr_addchar( data, *p++ );
newstr_trimendingws( data );
while ( *p=='\n' || *p=='\r' ) p++;
return p;
}
int
risin_processf( fields *risin, char *p, char *filename, long nref )
{
newstr tag, data;
newstrs_init( &tag, &data, NULL );
while ( *p ) {
if ( risin_istag( p ) ) {
p = process_line( &tag, &data, p );
/* no anonymous fields allowed */
if ( tag.len )
fields_add( risin, tag.data, data.data, 0 );
} else {
p = process_line2( &tag, &data, p );
if ( data.len && risin->nfields>0 ) {
newstr *od;
od = &(risin->data[risin->nfields-1] );
newstr_addchar( od, ' ' );
newstr_strcat( od, data.data );
}
}
newstrs_empty( &tag, &data, NULL );
}
newstrs_free( &tag, &data, NULL );
return 1;
}
/* oxfordjournals hide the DOI in the NOTES N1 field */
static void
notes_add( fields *info, char *tag, newstr *s, int level )
{
int doi = is_doi( s->data );
if ( doi!=-1 )
fields_add( info, "DOI", &(s->data[doi]), level );
else
fields_add( info, tag, s->data, level );
}
/* scopus puts DOI in the DO or DI tag, but it needs cleaning */
static void
doi_add( fields *info, char *tag, newstr *s, int level )
{
int doi = is_doi( s->data );
if ( doi!=-1 )
fields_add( info, "DOI", &(s->data[doi]), level );
}
static void
adddate( fields *info, char *tag, char *p, int level )
{
newstr date;
int part = ( !strncasecmp( tag, "PART", 4 ) );
newstr_init( &date );
while ( *p && *p!='/' ) newstr_addchar( &date, *p++ );
if ( *p=='/' ) p++;
if ( date.len>0 ) {
if ( part ) fields_add( info, "PARTYEAR", date.data, level );
else fields_add( info, "YEAR", date.data, level );
}
newstr_empty( &date );
while ( *p && *p!='/' ) newstr_addchar( &date, *p++ );
if ( *p=='/' ) p++;
if ( date.len>0 ) {
if ( part ) fields_add( info, "PARTMONTH", date.data, level );
else fields_add( info, "MONTH", date.data, level );
}
newstr_empty( &date );
while ( *p && *p!='/' ) newstr_addchar( &date, *p++ );
if ( *p=='/' ) p++;
if ( date.len>0 ) {
if ( part ) fields_add( info, "PARTDAY", date.data, level );
else fields_add( info, "DAY", date.data, level );
}
newstr_empty( &date );
while ( *p ) newstr_addchar( &date, *p++ );
if ( date.len>0 ) {
if ( part ) fields_add( info, "PARTDATEOTHER", date.data,level);
else fields_add( info, "DATEOTHER", date.data, level );
}
newstr_free( &date );
}
int
risin_typef( fields *risin, char *filename, int nref, param *p, variants *all, int nall )
{
char *refnum = "";
int n, reftype, nreftype;
n = fields_find( risin, "TY", 0 );
nreftype = fields_find( risin, "ID", 0 );
if ( nreftype!=-1 ) refnum = risin[n].data->data;
if ( n!=-1 )
reftype = get_reftype( (risin[n].data)->data, nref, p->progname,
all, nall, refnum );
else
reftype = get_reftype( "", nref, p->progname, all, nall, refnum ); /*default */
return reftype;
}
static void
risin_report_notag( param *p, char *tag )
{
if ( p->verbose && strcmp( tag, "TY" ) ) {
if ( p->progname ) fprintf( stderr, "%s: ", p->progname );
fprintf( stderr, "Did not identify RIS tag '%s'\n", tag );
}
}
void
risin_convertf( fields *risin, fields *info, int reftype, param *p, variants *all, int nall )
{
newstr *t, *d;
int process, level, i, n;
char *newtag;
for ( i=0; i<risin->nfields; ++i ) {
t = &( risin->tag[i] );
d = &( risin->data[i] );
n = process_findoldtag( t->data, reftype, all, nall );
if ( n==-1 ) {
risin_report_notag( p, t->data );
continue;
}
process = ((all[reftype]).tags[n]).processingtype;
level = ((all[reftype]).tags[n]).level;
newtag = ((all[reftype]).tags[n]).newstr;
if ( process==SIMPLE )
fields_add( info, newtag, d->data, level );
else if ( process==PERSON )
name_add( info, newtag, d->data, level, &(p->asis),
&(p->corps) );
else if ( process==TITLE )
title_process( info, newtag, d->data, level,
p->nosplittitle );
else if ( process==DATE )
adddate( info, newtag, d->data, level );
else if ( process==SERIALNO )
addsn( info, d->data, level );
else if ( process==NOTES )
notes_add( info, newtag, d, level );
else if ( process==DOI )
doi_add( info, newtag, d, level );
else { /* do nothing */ }
}
/* look for thesis-type hint */
if ( !strcasecmp( all[reftype].type, "THES" ) ) {
for ( i=0; i<risin->nfields; ++i ) {
if ( strcasecmp(risin->tag[i].data, "U1") )
continue;
if ( !strcasecmp(risin->data[i].data,"Ph.D. Thesis")||
!strcasecmp(risin->data[i].data,"Masters Thesis")||
!strcasecmp(risin->data[i].data,"Diploma Thesis")||
!strcasecmp(risin->data[i].data,"Doctoral Thesis")||
!strcasecmp(risin->data[i].data,"Habilitation Thesis"))
fields_add( info, "GENRE", risin->data[i].data,
0 );
}
}
}
|