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
|
/*
* bibl.c
*
* Copyright (c) Chris Putnam 2005-2021
*
* Source code released under the GPL version 2
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bibdefs.h"
#include "bibl.h"
void
bibl_init( bibl *b )
{
b->n = b->max = 0L;
b->ref = NULL;
}
static int
bibl_alloc( bibl * b )
{
int alloc = 50;
b->ref = ( fields ** ) malloc( sizeof( fields* ) * alloc );
if ( !b->ref ) return BIBL_ERR_MEMERR;
b->max = alloc;
return BIBL_OK;
}
static int
bibl_realloc( bibl * b )
{
long alloc = b->max * 2;
fields **more;
more = ( fields ** ) realloc( b->ref, sizeof( fields* ) * alloc );
if ( !more ) return BIBL_ERR_MEMERR;
b->ref = more;
b->max = alloc;
return BIBL_OK;
}
int
bibl_addref( bibl *b, fields *ref )
{
int status = BIBL_OK;
if ( b->max==0 )
status = bibl_alloc( b );
else if ( b->n >= b->max )
status = bibl_realloc( b );
if ( status==BIBL_OK ) {
b->ref[ b->n ] = ref;
b->n++;
}
return status;
}
void
bibl_free( bibl *b )
{
long i;
for ( i=0; i<b->n; ++i )
fields_delete( b->ref[i] );
free( b->ref );
bibl_init( b );
}
/* bibl_copy()
*
* returns BIBL_OK on success, BIBL_ERR_MEMERR on failure
*/
int
bibl_copy( bibl *bout, bibl *bin )
{
fields *ref;
int status;
long i;
for ( i=0; i<bin->n; ++i ) {
ref = fields_dupl( bin->ref[i] );
if ( !ref ) return BIBL_ERR_MEMERR;
status = bibl_addref( bout, ref );
if ( status!=BIBL_OK ) return status;
}
return BIBL_OK;
}
/* bibl_findref()
*
* returns position of reference matching citekey, else -1
*/
long
bibl_findref( bibl *bin, const char *citekey )
{
long i;
int n;
for ( i=0; i<bin->n; ++i ) {
n = fields_find( bin->ref[i], "refnum", LEVEL_ANY );
if ( n==FIELDS_NOTFOUND ) continue;
if ( !strcmp( fields_value( bin->ref[i], n, FIELDS_CHRP_NOUSE ), citekey ) ) return i;
}
return -1;
}
|