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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/*** crossref.c ***************************************************************
**
** This file is part of BibTool.
** It is distributed under the GNU General Public License.
** See the file COPYING for details.
**
** (c) 2007-2017 Gerd Neugebauer
**
** Net: gene@gerd-neugebauer.de
**
** 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
**
**-----------------------------------------------------------------------------
** Description:
** This module contains functions to expand crossref entries.
**
******************************************************************************/
#include <bibtool/general.h>
#include <bibtool/error.h>
#include <bibtool/crossref.h>
#include <bibtool/symbols.h>
#include <bibtool/s_parse.h>
#include <bibtool/database.h>
#include <bibtool/sbuffer.h>
#include <bibtool/rsc.h>
#include <bibtool/entry.h>
/*****************************************************************************/
/* Internal Programs */
/*===========================================================================*/
/*****************************************************************************/
/* External Programs */
/*===========================================================================*/
/*---------------------------------------------------------------------------*/
typedef struct mAP /* */
{ rec_type src_rec; /*source record type index*/
Symbol src_field; /* */
rec_type dest_rec; /*target record type index*/
Symbol dest_field; /* */
struct mAP * next_map; /* */
} *Map, SMap; /* */
#define SourceRecord(M) ((M)->src_rec)
#define SourceField(M) ((M)->src_field)
#define DestinationRecord(M) ((M)->dest_rec)
#define DestinationField(M) ((M)->dest_field)
#define NextMap(M) ((M)->next_map)
/*-----------------------------------------------------------------------------
** Function*: new_map()
** Type: Map
** Purpose: allocate a new Map structure
** Arguments:
** sr source record type index
** sf source field name
** dr destination record type index
** df destination field name
** Returns: the new map
**___________________________________________________ */
static Map new_map(sr,sf,dr,df) /* */
rec_type sr; /* */
Symbol sf; /* */
rec_type dr; /* */
Symbol df; /* */
{ Map m; /* */
if ( (m = (Map)malloc(sizeof(SMap))) == (Map)NULL)/* */
{ OUT_OF_MEMORY("map"); } /* */
/* */
SourceRecord(m) = sr; /* */
SourceField(m) = sf; /* */
DestinationRecord(m) = dr; /* */
DestinationField(m) = df; /* */
NextMap(m) = NULL; /* */
return m; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function*: free_map()
** Type: void
** Purpose: Free a chain of map structures
** Arguments:
** m the map
** Returns: nothing
**___________________________________________________ */
static void free_map(m) /* */
Map m; /* */
{ Map nxt; /* */
while (m) /* */
{ nxt = NextMap(m); /* */
free(m); /* */
m = nxt; /* */
} /* */
} /*------------------------*/
#define MAP_SIZE 101
static Map map[MAP_SIZE]; /* */
#define MAP_INDEX(SR,SF,DR) \
(int)((((SR) % 73) + \
((long)(SF) & 0xfff) + \
((DR) % 1023)) % MAP_SIZE);
/*-----------------------------------------------------------------------------
** Function: clear_map()
** Type: void
** Purpose: Reset the map to it's initial state where no elements are
** contained.
** Arguments: none
** Returns: nothing
**___________________________________________________ */
void clear_map() /* */
{ register int i; /* */
/* */
for (i = 0; i < MAP_SIZE; i++) /* */
{ free_map(map[i]); /* */
map[i] = NULL; /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: map_add()
** Type: int
** Purpose: Add or overwrite a filed name mapping.
**
** Arguments:
** s_rec the index of the source record type
** s_fld the source field name
** d_rec the index of the destination record type
** d_fld the destination field name
** Returns: nothing
**___________________________________________________ */
void map_add(s_rec,s_fld,d_rec,d_fld) /* */
rec_type s_rec; /* */
Symbol s_fld; /* */
rec_type d_rec; /* */
Symbol d_fld; /* */
{ int idx; /* */
Map m; /* */
/* */
idx = MAP_INDEX(s_rec, s_fld, d_rec); /* */
m = map[idx]; /* */
if (m == NULL ) /* */
{ map[idx] = new_map(s_rec, s_fld, d_rec, d_fld);/* */
return; /* */
} /* */
/* */
for (;;) /* */
{ if (SourceRecord(m) == s_rec && /* */
SourceField(m) == s_fld && /* */
DestinationRecord(m) == d_rec ) /* */
{ DestinationField(m) = d_fld; /* */
return; /* */
} /* */
if (NextMap(m) == NULL ) /* */
{ NextMap(m) = new_map(s_rec, s_fld, /* */
d_rec, d_fld); /* */
return; /* */
} /* */
m = NextMap(m); /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: map_get()
** Type: Symbol
** Purpose: Getter for a map element.
** Arguments:
** s_rec the index of the source entry type
** s_fld the name of the source field
** d_rec the index of the destination entry type
** Returns: the new field name or |NO_SYMBOL|
**___________________________________________________ */
Symbol map_get(s_rec, s_fld, d_rec) /* */
rec_type s_rec; /* */
Symbol s_fld; /* */
rec_type d_rec; /* */
{ int i = MAP_INDEX(s_rec, s_fld, d_rec); /* */
Map m = map[i]; /* */
/* */
for (; m; m = NextMap(m)) /* */
{ if (SourceRecord(m) == s_rec && /* */
SourceField(m) == s_fld && /* */
DestinationRecord(m) == d_rec ) /* */
{ return DestinationField(m); } /* */
} /* */
return NO_SYMBOL; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: crossref_map()
** Type: int
** Purpose:
**
** Arguments:
** spec the argument
** Returns: nothing
**___________________________________________________ */
void crossref_map(spec) /* */
String spec; /* */
{ Symbol *src, *dest; /* */
Symbol src_field, dest_field; /* */
Symbol *sp, *dp; /* */
rec_type s_rec, d_rec; /* */
/* */
sp_open(spec); /* */
if ((src = sp_symbols(&spec)) == NULL) return; /* */
sp_skip(&spec); /* */
if ((src_field = SParseSymbol(&spec)) == NULL) return;/* */
sp_skip(&spec); /* */
if ((dest = sp_symbols(&spec)) == NULL) return; /* */
sp_skip(&spec); /* */
if ((dest_field = SParseSymbol(&spec)) == NULL) return;/* */
sp_eos(&spec); /* */
sp_close(); /* */
/* */
for (sp = src; *sp; sp++) /* */
{ s_rec = find_entry_type(SymbolValue(*sp)); /* */
if (s_rec == BIB_NOOP) /* */
{ WARNING3("Unknown source entry type `", /* */
SymbolValue(*sp), /* */
"'. Mapping ignored."); /* */
continue; /* */
} /* */
/* */
for (dp = dest; *dp; dp++) /* */
{ d_rec = find_entry_type(SymbolValue(*dp)); /* */
if (d_rec == BIB_NOOP) /* */
{ WARNING3("Unknown destination entry type `",/* */
SymbolValue(*dp), /* */
"'. Mapping ignored."); /* */
continue; /* */
} /* */
/* */
map_add(s_rec, src_field, d_rec, dest_field);/* */
} /* */
} /* */
/* */
#ifndef COMPLEX_SYMBOL
free(src); /* */
free(dest); /* */
#endif
} /*------------------------*/
static Symbol NONE = (Symbol)"x"; /* TODO */
/*-----------------------------------------------------------------------------
** Function*: insert_record()
** Type: bool
** Purpose: Find a record and insert some new entryies.
** Arguments:
** db the database
** rec the record
** hp the heap pointer
** s the key of the entry
** msg the message prefix for an unknown entry message
** Returns: |true| iff the record could be inserted
**___________________________________________________ */
static bool insert_record(db, rec, hp, s, msg) /* */
DB db; /* */
Record rec; /* */
register Symbol *hp; /* */
Symbol s; /* */
String msg; /* */
{ Record r; /* */
Symbol t, ms; /* */
int i; /* */
/* */
if ((r = db_find(db, s)) == RecordNULL) /* */
{ ERROR3(msg," entry not found: ", /* */
(char*)SymbolValue(s)); /* */
return false; /* */
} /* */
/* */
for (i = RecordFree(r), hp = RecordHeap(r); /* visit all fields */
i > 0; /* */
i -= 2) /* */
{ s = *hp++; /* */
t = *hp++; /* */
if (t != NO_SYMBOL) /* */
{ ms = map_get(RecordType(r), s, /* */
RecordType(rec)); /* */
provide_to_record(rec, ms ? ms : s, t); /* */
} /* */
} /* */
return true; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: expand_crossref()
** Purpose: Expand all items inherited via a crossref.
** Arguments:
** db Database containing the entries.
** rec The record to expand
** Returns: |false| iff an error has occured
**___________________________________________________ */
bool expand_crossref(db, rec) /* */
DB db; /* */
Record rec; /* */
{ register Symbol *hp; /* */
register int i; /* */
String x; /* */
Symbol s; /* */
Record r = rec; /* */
int limit = rsc_xref_limit; /* */
Symbol xdata = (rsc_expand_xdata /* */
? sym_xdata /* */
: NONE); /* */
Symbol crossref = (rsc_expand_crossref /* */
? sym_crossref /* */
: NONE); /* */
/* */
DebugPrint1("expand_crossref"); /* */
/* */
while (RecordIsXREF(r) && limit-- >= 0) /* */
{ /* */
for (i = RecordFree(r), hp = RecordHeap(r); /* search crossref field */
i > 0 /* */
&& (*hp != crossref && *hp != xdata); /* */
i -= 2, hp += 2) /* */
{ } /* */
/* */
if (i <= 0) /* */
{ DebugPrint1("*** No crossref found."); /* */
return false; /* */
} /* */
else if (*hp == sym_crossref) /* ---------------------- */
{ /* */
if (rec == r) { *hp = NULL; } /* Delete the first xref */
x = SymbolValue(*++hp); /* */
x++; /* */
sp_open(x); /* Try to extract */
if ((s = SParseSymbol(&x)) == NO_SYMBOL) /* the crossref as symbol*/
{ return false; } /* */
/* */
insert_record(db, rec, hp, s, "Crossref"); /* */
} /* */
else if (*hp == sym_xdata) /* ---------------------- */
{ /* */
*hp = NULL; /* Delete the first xref */
x = SymbolValue(*++hp); /* */
x++; /* */
sp_open(x); /* Try to extract */
/* */
if (sp_expect(&x, (String)"}", 0) ) /* */
return false; /* */
for (;;) /* */
{ if ((s = SParseSymbol(&x)) == NO_SYMBOL) /* */
{ return true; } /* */
/* */
insert_record(db, rec, hp, s, "XData"); /* */
/* */
if (sp_expect(&x, (String)"}", 0) ) break; /* */
sp_expect(&x, (String)",", 1); /* */
} /* */
} /* */
} /* */
return false; /* */
} /*------------------------*/
|