File: biblio.c

package info (click to toggle)
latex2rtf 2.3.18a-5
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,392 kB
  • sloc: ansic: 20,424; makefile: 660; sh: 478; perl: 22
file content (189 lines) | stat: -rwxr-xr-x 5,076 bytes parent folder | download | duplicates (5)
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
/* Generalised bibliography handling

Copyright (C) 1995-2002 The Free Software Foundation

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

This file is available from http://sourceforge.net/projects/latex2rtf/
 
Authors:
    1995      Fernando Dorner, Andreas Granzer, Freidrich Polzer, Gerhard Trisko
    1995-1997 Ralf Schlatterbeck
    1998-2000 Georg Lehner
    2001-2002 Scott Prahl
*/

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "main.h"
#include "utils.h"
#include "parser.h"
#include "auxfile.h"
#include "biblio.h"

/*
 * *********************************************
 * A try at unifying the citation infrastructure
 * *********************************************
 */
static biblioElem *biblioTable = NULL;
static int         biblioCount = 0;

static biblioElem *newBiblio(char *newKey)
{
    biblioElem *result =
        (biblioElem *)realloc(biblioTable,
                              sizeof(biblioElem) * (biblioCount+1));
    
    if (NULL != result) {
        biblioTable = result;
        result = &result[biblioCount++];
        result->biblioKey  = newKey;
        result->biblioN    = NULL;
        result->biblioFull = NULL;
        result->biblioAbbr = NULL;
        result->biblioYear = NULL;
    }
    return result;
}

static void printTable(void)
{
#ifdef VERBOSE_BIBLIO
    int i;
    for (i=0; i< biblioCount; i++) {
        biblioElem *p = &biblioTable[i];
        diagnostics(WARNING,"biblioTable[%d]",i);
        diagnostics(WARNING,"%s %s %s %s %s %d",
                    p->biblioKey,
                    p->biblioN,
                    p->biblioFull,
                    p->biblioAbbr,
                    p->biblioYear,
                    p->biblioType);
    }
#endif
}

biblioElem *getBiblio(char *key)
{
    int i;
    LoadAuxFile();      /* load auxfile if not already loaded */
    for (i=0; i< biblioCount; i++) {
        if (streq(biblioTable[i].biblioKey,key)) {
            return &biblioTable[i];
        }
    }
    return NULL;
}

/* for normal bibliographic references: */
/* \bibcite{key}{ref}                   */
/* returns ref                          */
char *getBiblioRef(char *key) {
    biblioElem *result = getBiblio(key);
    if (result != NULL) {
        return strdup(result->biblioN);
    }
    return NULL;
}

/* for extended bibliographic references: */
/* \bibcite{key}{{first}{}{}...}          */
/* returns first                          */

char *getBiblioFirst(char *key) {
    biblioElem *result = getBiblio(key);
    if (result != NULL) {
        char *ref = strdup(result->biblioN);
        char *res = NULL;
        if (NULL != ref) {
            PushSource(NULL,ref);
            res = getBraceParam();
            PopSource();
        }
        safe_free(ref);
        return res;
    }
    return NULL;
}
biblioElem *newBibCite(char *cite, char *tag)
{
    biblioElem *newCite = newBiblio(cite);
    if (NULL != newCite) {
        newCite->biblioN    = tag;
        newCite->biblioType = BIBLIO_BIBCITE;
        printTable();
    }
    return newCite;
}

biblioElem *newHarvardCite(char *cite,char *full,char* abbr,char *year)
{
    biblioElem *newCite = newBiblio(cite);
    if (NULL != newCite) {
        newCite->biblioFull = full;
        newCite->biblioAbbr = abbr;
        newCite->biblioYear = year;
        newCite->biblioType = BIBLIO_HARVARD;
    }
    return newCite;
}

biblioElem *newNatBibCite(char *cite,char *full,char* abbr,char *year,char *n)
{
    biblioElem *newCite = newBiblio(cite);
    if (NULL != newCite) {
        newCite->biblioFull = full;
        newCite->biblioAbbr = abbr;
        newCite->biblioYear = year;
        newCite->biblioN    = n;
        newCite->biblioType = BIBLIO_NATBIB;
    }
    return newCite;
}

/*
 * Interface to the AUX file parser
 */

/*  \bibcite{rfc2328}{18} */

void CmdBibCite(int code)
{
    char *p1 = getBraceParam();
    char *p2 = getBraceParam();

    if (NULL == newBibCite(p1,p2)) {
        diagnostics(WARNING,"memory exhausted for \\bibcite{%s}{%s}",p1,p2);
        free(p1);
        free(p2);
    } 
}

/*  \harvardcite{latex}{Lamport}{Lamport}{1986} */

void CmdAuxHarvardCite(int code)
{
    char *key = getBraceParam();
    char *full = getBraceParam();
    char *abbr = getBraceParam();
    char *year = getBraceParam();
   
    if (NULL == newHarvardCite(key,full,abbr,year)) {
        diagnostics(ERROR,"Memory overflow defining \\harvardcite(%s)",key);
    }
}