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
|
/*
* bibdiff.c
*
* Copyright (c) Chris Putnam 2017-2021
*
* Program and source code released under the GPL version 2
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "bibutils.h"
#include "intlist.h"
#include "args.h"
char progname[] = "bibdiff";
/* find_tag_level_matches()
*
* Find all possible matches for tag/level combinations
*/
int
find_tag_level_matches( fields *f, char *tag, int level, intlist *matchs )
{
int i, n = 0;
char *ftag;
for ( i=0; i<f->n; ++i )
intlist_set( matchs, i, 0 );
for ( i=0; i<f->n; ++i ) {
if ( fields_level( f, i ) != level ) continue;
ftag = fields_tag( f, i, FIELDS_CHRP_NOLEN );
if ( !strcmp( tag, ftag ) ) {
intlist_set( matchs, i, 1 );
n++;
}
}
return n;
}
int
compare_references( fields *f1, const char *fname1, fields *f2, const char *fname2, long n )
{
int i, j, cnt, cnt1, cnt2, level, diff = 0;
intlist found1, found2, matches;
char *tag, *data;
intlist_init_fill( &found1, f1->n, -1 );
intlist_init_fill( &found2, f2->n, -1 );
intlist_init_fill( &matches, f2->n, 0 );
for ( i=0; i<f1->n; ++i ) {
tag = fields_tag( f1, i, FIELDS_CHRP_NOLEN );
data = fields_value( f1, i, FIELDS_CHRP_NOLEN );
level = fields_level( f1, i );
cnt = find_tag_level_matches( f2, tag, level, &matches );
/* ...no matches */
if ( cnt==0 ) continue;
for ( j=0; j<f2->n; ++j ) {
if ( intlist_get( &matches, j ) == 0 ) continue; /* not a potential match */
if ( intlist_get( &found2, j ) != -1 ) continue; /* already claimed */
if ( strcmp( data, fields_value( f2, j, FIELDS_CHRP_NOLEN ) ) ) continue; /* values don't match */
intlist_set( &found1, i, j );
intlist_set( &found2, j, i );
break;
}
}
cnt1 = 0;
for ( i=0; i<f1->n; ++i )
if ( intlist_get( &found1, i ) != -1 ) cnt1++;
cnt2 = 0;
for ( i=0; i<f2->n; ++i )
if ( intlist_get( &found2, i ) != -1 ) cnt2++;
if ( cnt1!=f1->n || cnt2!=f2->n ) {
printf( "reference %ld < %s > %s\n", n, fname1, fname2 );
for ( i=0; i<f1->n; ++i ) {
if ( intlist_get( &found1, i ) != -1 ) continue;
tag = fields_tag( f1, i, FIELDS_CHRP_NOLEN );
data = fields_value( f1, i, FIELDS_CHRP_NOLEN );
level = fields_level( f1, i );
printf( "< '%s' '%s' %d\n", tag, data, level );
}
for ( i=0; i<f2->n; ++i ) {
if ( intlist_get( &found2, i ) != -1 ) continue;
tag = fields_tag( f2, i, FIELDS_CHRP_NOLEN );
data = fields_value( f2, i, FIELDS_CHRP_NOLEN );
level = fields_level( f2, i );
printf( "> '%s' '%s' %d\n", tag, data, level );
}
}
intlist_free( &found1 );
intlist_free( &found2 );
intlist_free( &matches );
return diff;
}
int
compare_bibliographies( bibl *b1, const char *fname1, bibl *b2, const char *fname2 )
{
fields *f1, *f2;
long i;
if ( b1->n != b2->n ) {
printf( "%s: %s has %ld references and %s has %ld references\n", progname,
fname1, b1->n, fname2, b2->n );
return 1;
}
for ( i=0; i<b1->n; ++i ) {
f1 = b1->ref[i];
f2 = b2->ref[i];
if ( compare_references( f1, fname1, f2, fname2, i+1 ) ) return 1;
}
return 0;
}
void
version( void )
{
args_tellversion( progname );
exit( EXIT_FAILURE );
}
void
help( void )
{
args_tellversion( progname );
fprintf( stderr, "Compares references after reading them from their native formats\n\n" );
fprintf( stderr, "usage: %s ref1_file ref2_file\n\n", progname );
fprintf( stderr, "-h, --help display this help\n" );
fprintf( stderr, "-v, --version display version\n" );
fprintf( stderr, "-f1, --format1 FORMAT specify input format for ref1_file\n" );
fprintf( stderr, "-f2, --format2 FORMAT specify input format for ref1_file\n" );
fprintf( stderr, "\n" );
fprintf( stderr, "Valid format specifiers are 'bibtex', 'biblatex', 'copac', 'ebi', "
"'endnote', 'endnote-xml', 'medline', 'mods', 'nbib', 'ris', 'word2007'\n\n" );
exit( EXIT_FAILURE );
}
int
lookup_format( const char *format )
{
typedef struct flist_t { char *name; int code; } flist_t;
flist_t formats[] = {
{ "bibtex", BIBL_BIBTEXIN },
{ "biblatex", BIBL_BIBLATEXIN },
{ "copac", BIBL_COPACIN },
{ "ebi", BIBL_EBIIN },
{ "endnote", BIBL_ENDNOTEIN },
{ "endnote-xml", BIBL_ENDNOTEXMLIN },
{ "medline", BIBL_MEDLINEIN },
{ "mods", BIBL_MODSIN },
{ "nbib", BIBL_NBIBIN },
{ "ris", BIBL_RISIN },
{ "word2007", BIBL_WORDIN },
};
int i, nformats = sizeof( formats ) / sizeof( formats[0] );
for ( i=0; i<nformats; ++i ) {
if ( !strcasecmp( format, formats[i].name ) ) return formats[i].code;
}
fprintf( stderr, "%s: Cannot recognize format '%s'.\n", progname, format );
fprintf( stderr, "%s: Valid format specifiers are 'bibtex', 'biblatex', 'copac', 'ebi', "
"'endnote', 'endnote-xml', 'medline', 'mods', 'nbib', 'ris', 'word2007'\n", progname );
fprintf( stderr, "%s: Exiting.\n", progname );
exit( EXIT_FAILURE );
}
void
process_args( int *argc, char *argv[], int *format1, int *format2 )
{
int i, j, done, subtract;
char *f1, *f2;
for ( i=0; i<*argc; ++i )
if ( args_match( argv[i], "-h", "--help" ) ) help();
for ( i=0; i<*argc; ++i )
if ( args_match( argv[i], "-v", "--version" ) ) version();
done = 0;
i = 1;
while ( i < *argc && !done ) {
subtract = 0;
if ( args_match( argv[i], "-f1", "--format1" ) ) {
f1 = args_next( *argc, argv, i, progname, "-f1", "--format1" );
*format1 = lookup_format( f1 );
subtract = 2;
}
else if ( args_match( argv[i], "-f2", "--format2" ) ) {
f2 = args_next( *argc, argv, i, progname, "-f1", "--format1" );
*format2 = lookup_format( f2 );
subtract = 2;
}
else if ( !strcmp( argv[i], "--" ) ) {
done = 1;
subtract = 1;
}
else if ( !strncmp( argv[i], "-", 1 ) ) {
fprintf( stderr, "%s: Unrecognized command-line switch '%s'. Exiting.\n", progname, argv[i] );
exit( EXIT_FAILURE );
}
if ( subtract ) {
for ( j=i+subtract; j<*argc; ++j )
argv[j-subtract] = argv[j];
*argc -= subtract;
} else i++;
}
}
int
main( int argc, char *argv[] )
{
int format1 = BIBL_MODSIN;
int format2 = BIBL_MODSIN;
param p1, p2;
bibl b1, b2;
int status;
FILE *fp;
process_args( &argc, argv, &format1, &format2 );
if ( argc < 2 ) help();
bibl_initparams( &p1, format1, BIBL_MODSOUT, progname );
bibl_initparams( &p2, format2, BIBL_MODSOUT, progname );
fp = fopen( argv[1], "r" );
if ( !fp ) {
fprintf( stderr, "%s: Cannot open %s\n", progname, argv[1] );
return EXIT_SUCCESS;
}
bibl_init( &b1 );
status = bibl_read( &b1, fp, argv[1], &p1 );
fclose( fp );
if ( status!=BIBL_OK ) {
bibl_reporterr( status );
return EXIT_FAILURE;
}
fp = fopen( argv[2], "r" );
if ( !fp ) {
fprintf( stderr, "%s: Cannot open %s\n", progname, argv[2] );
return EXIT_SUCCESS;
}
bibl_init( &b2 );
status = bibl_read( &b2, fp, argv[2], &p2 );
fclose( fp );
if ( status!=BIBL_OK ) {
bibl_reporterr( status );
return EXIT_FAILURE;
}
compare_bibliographies( &b1, argv[1], &b2, argv[2] );
bibl_freeparams( &p1 );
bibl_freeparams( &p2 );
return EXIT_SUCCESS;
}
|