4
* Copyright (c) Chris Putnam 2004-2010
6
* Program and source code released under the GPL
14
#include "newstr_conv.h"
24
copacin_initparams( param *p, const char *progname )
26
p->readformat = BIBL_COPACIN;
27
p->charsetin = BIBL_CHARSET_DEFAULT;
28
p->charsetin_src = BIBL_SRC_DEFAULT;
36
p->readf = copacin_readf;
37
p->processf = copacin_processf;
40
p->convertf = copacin_convertf;
44
list_init( &(p->asis) );
45
list_init( &(p->corps) );
47
if ( !progname ) p->progname = NULL;
48
else p->progname = strdup( progname );
51
/* Endnote-Refer/Copac tag definition:
52
character 1 = alphabetic character
53
character 2 = alphabetic character
58
copacin_istag( char *buf )
60
if (! ((buf[0]>='A' && buf[0]<='Z')) || (buf[0]>='a' && buf[0]<='z') )
62
if (! ((buf[1]>='A' && buf[1]<='Z')) || (buf[1]>='a' && buf[1]<='z') )
64
if (buf[2]!='-' ) return 0;
65
if (buf[3]!=' ' ) return 0;
69
readmore( FILE *fp, char *buf, int bufsize, int *bufpos, newstr *line )
71
if ( line->len ) return 1;
72
else return newstr_fget( fp, buf, bufsize, bufpos, line );
76
copacin_readf( FILE *fp, char *buf, int bufsize, int *bufpos, newstr *line, newstr *reference, int *fcharset )
78
int haveref = 0, inref=0;
80
*fcharset = CHARSET_UNKNOWN;
81
while ( !haveref && readmore( fp, buf, bufsize, bufpos, line ) ) {
82
/* blank line separates */
83
if ( line->data==NULL ) continue;
84
if ( inref && line->len==0 ) haveref=1;
86
/* Recognize UTF8 BOM */
88
(unsigned char)(p[0])==0xEF &&
89
(unsigned char)(p[1])==0xBB &&
90
(unsigned char)(p[2])==0xBF ) {
91
*fcharset = CHARSET_UNICODE;
94
if ( copacin_istag( p ) ) {
95
if ( inref ) newstr_addchar( reference, '\n' );
96
newstr_strcat( reference, p );
101
/* copac puts tag only on 1st line */
102
newstr_addchar( reference, ' ' );
106
newstr_strcat( reference, p );
108
newstr_empty( line );
110
newstr_empty( line );
117
copacin_addtag2( char *p, newstr *tag, newstr *data )
121
while ( i<3 && *p ) {
122
newstr_addchar( tag, *p++ );
125
while ( *p==' ' || *p=='\t' ) p++;
126
while ( *p && *p!='\r' && *p!='\n' ) {
127
newstr_addchar( data, *p );
130
newstr_trimendingws( data );
131
while ( *p=='\n' || *p=='\r' ) p++;
136
copacin_nextline( char *p )
138
while ( *p && *p!='\n' && *p!='\r') p++;
139
while ( *p=='\n' || *p=='\r' ) p++;
144
copacin_processf( fields *copacin, char *p, char *filename, long nref )
148
newstr_init( &data );
151
if ( copacin_istag( p ) ) {
152
p = copacin_addtag2( p, &tag, &data );
153
/* don't add empty strings */
154
if ( tag.len && data.len )
155
fields_add( copacin, tag.data, data.data, 0 );
156
newstr_empty( &tag );
157
newstr_empty( &data );
159
else p = copacin_nextline( p );
162
newstr_free( &data );
166
/* copac names appear to always start with last name first, but don't
167
* always seem to have a comma after the name
169
* editors seem to be stuck in as authors with the tag "[Editor]" in it
172
copacin_addname( fields *info, char *tag, newstr *name, int level, list *asis,
175
char *usetag = tag, editor[]="EDITOR", *p;
177
if ( strstr( name->data,"[Editor]" ) ) {
178
newstr_findreplace( name, "[Editor]", "" );
181
p = skip_ws( name->data );
182
while ( *p && !is_ws( *p ) ) {
183
if ( *p==',' ) comma++;
186
if ( !comma && is_ws( *p ) ) *p = ',';
187
name_add( info, usetag, name->data, level, asis, corps );
191
copacin_addpage( fields *info, char *p, int level )
194
newstr_init( &page );
196
while ( *p && !is_ws(*p) && *p!='-' && *p!='\r' && *p!='\n' )
197
newstr_addchar( &page, *p++ );
198
if ( page.len>0 ) fields_add( info, "PAGESTART", page.data, level );
199
newstr_empty( &page );
200
while ( *p && (is_ws(*p) || *p=='-' ) ) p++;
201
while ( *p && !is_ws(*p) && *p!='-' && *p!='\r' && *p!='\n' )
202
newstr_addchar( &page, *p++ );
203
if ( page.len>0 ) fields_add( info, "PAGEEND", page.data, level );
204
newstr_free( &page );
208
copacin_adddate( fields *info, char *tag, char *newtag, char *p, int level )
210
char *months[12]={ "January", "February", "March", "April",
211
"May", "June", "July", "August", "September",
212
"October", "November", "December" };
216
newstr_init( &date );
217
part = (!strncasecmp(newtag,"PART",4));
218
if ( !strcasecmp( tag, "%D" ) ) {
219
while ( *p ) newstr_addchar( &date, *p++ );
222
fields_add(info, "PARTYEAR", date.data, level);
224
fields_add( info, "YEAR", date.data, level );
226
} else if ( !strcasecmp( tag, "%8" ) ) {
227
while ( *p && *p!=' ' && *p!=',' ) newstr_addchar( &date, *p++ );
230
for ( i=0; i<12 && found==-1; ++i )
231
if ( !strncasecmp( date.data, months[i], 3 ) )
234
if (found>8) sprintf( month, "%d", found+1 );
235
else sprintf( month, "0%d", found+1 );
237
fields_add( info, "PARTMONTH", month, level );
238
else fields_add( info, "MONTH", month, level );
241
fields_add( info, "PARTMONTH", date.data, level );
243
fields_add( info, "MONTH", date.data, level );
246
newstr_empty( &date );
248
while ( *p && *p!='\n' && *p!=',' )
249
newstr_addchar( &date, *p++ );
250
if ( date.len>0 && date.len<3 ) {
252
fields_add( info, "PARTDAY", date.data, level );
254
fields_add( info, "DAY", date.data, level );
257
newstr_free( &date );
261
copacin_report_notag( param *p, char *tag )
264
if ( p->progname ) fprintf( stderr, "%s: ", p->progname );
265
fprintf( stderr, "Cannot find tag '%s'\n", tag );
270
copacin_convertf( fields *copacin, fields *info, int reftype, param *p, variants *all, int nall )
273
int process, level, i, n;
275
for ( i=0; i<copacin->nfields; ++i ) {
276
t = &( copacin->tag[i] );
277
d = &( copacin->data[i] );
278
n = process_findoldtag( t->data, reftype, all, nall );
280
copacin_report_notag( p, t->data );
283
process = ((all[reftype]).tags[n]).processingtype;
284
if ( process == ALWAYS ) continue; /*add these later*/
285
level = ((all[reftype]).tags[n]).level;
286
newtag = ((all[reftype]).tags[n]).newstr;
287
if ( process==SIMPLE )
288
fields_add( info, newtag, d->data, level );
289
else if ( process==TITLE )
290
title_process( info, newtag, d->data, level,
292
else if ( process==PERSON )
293
copacin_addname( info, newtag, d, level, &(p->asis),
295
else if ( process==DATE )
296
copacin_adddate(info,all[reftype].
297
tags[i].oldstr,newtag,d->data,level);
298
else if ( process==PAGES )
299
copacin_addpage( info, d->data, level );
300
else if ( process==SERIALNO )
301
addsn( info, d->data, level );
303
fprintf(stderr,"%s: internal error -- "
304
"illegal process %d\n", r->progname, process );