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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
|
/*** record.c *****************************************************************
**
** This file is part of BibTool.
** It is distributed under the GNU General Public License.
** See the file COPYING for details.
**
** (c) 1996-2020 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.
**
******************************************************************************/
#include <bibtool/general.h>
#include <bibtool/error.h>
#include <bibtool/record.h>
#include <bibtool/symbols.h>
/*****************************************************************************/
/* Internal Programs */
/*===========================================================================*/
#ifdef __STDC__
#define _ARG(A) A
#else
#define _ARG(A) ()
#endif
Record copy_record _ARG((Record rec)); /* record.c */
Record new_record _ARG((int token,int size)); /* record.c */
Record record_gc _ARG((Record rec)); /* record.c */
Record unlink_record _ARG((Record rec)); /* record.c */
Symbol record_get _ARG((Record rec, Symbol key)); /* record.c */
WordList new_wordlist _ARG((Symbol sym)); /* record.c */
int count_record _ARG((Record rec)); /* record.c */
void add_sort_order _ARG((Symbol val)); /* record.c */
void free_1_record _ARG((Record rec)); /* record.c */
void free_record _ARG((Record rec)); /* record.c */
void provide_to_record _ARG((Record rec,Symbol s,Symbol t));/* record.c */
void push_to_record _ARG((Record rec,Symbol s,Symbol t, bool err));/* record.c*/
void sort_record _ARG((Record rec)); /* record.c */
/*****************************************************************************/
/* External Programs */
/*===========================================================================*/
/*---------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
** Function: copy_record()
** Purpose: Copy a record and return a new instance.
** If no memory is left then an error is raised and the
** program is terminated.
** Arguments:
** rec The record to copy.
** Returns: The new copy of |rec|.
**___________________________________________________ */
Record copy_record(rec) /* */
register Record rec; /* */
{ register Record new; /* */
register Symbol *new_heap, /* */
*old_heap; /* */
register int i; /* */
/* */
if ( (new=(Record)malloc(sizeof(SRecord))) == 0L/* */
|| (new_heap=(Symbol*)malloc(sizeof(Symbol)*(size_t)RecordFree(rec)))/* */
== 0L ) /* */
{ OUT_OF_MEMORY("Record"); } /* */
RecordSortkey(new) = sym_empty; /* */
RecordOldKey(new) = RecordOldKey(rec); /* */
NextRecord(new) = RecordNULL; /* */
PrevRecord(new) = RecordNULL; /* */
RecordType(new) = RecordType(rec); /* */
RecordFree(new) = RecordFree(rec); /* */
RecordComment(new) = RecordComment(rec); /* */
RecordSource(new) = RecordSource(rec); /* */
if (RecordSource(new)) { /* */
LinkSymbol(RecordSource(new)); /* */
} /* */
RecordLineno(new) = RecordLineno(rec); /* */
RecordHeap(new) = new_heap; /* */
RecordFlags(new) = RecordFlags(rec); /* */
for (i = 0, old_heap = RecordHeap(rec); /* */
i < RecordFree(new); /* */
++i) /* */
{ *(new_heap++) = *(old_heap++); } /* */
return (new); /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: new_record()
** Purpose: Create a new record and return it.
** If no memory is left then an error is raised and the
** program is terminated.
** Arguments:
** token The token type of the record.
** size The initial heap size.
** Returns: The new record.
**___________________________________________________ */
Record new_record(token,size) /* */
int token; /* */
int size; /* */
{ register Record new; /* */
register Symbol *new_heap; /* */
register int i; /* */
/* */
if ( size < 1 ) size = 1; /* */
if ( (new=(Record)malloc(sizeof(SRecord))) == 0L/* */
|| (new_heap=(Symbol*)malloc(sizeof(Symbol)*(size_t)(size)))/* */
== 0L ) /* */
{ OUT_OF_MEMORY("Record"); } /* */
RecordSortkey(new) = sym_empty; /* */
RecordOldKey(new) = RecordSortkey(new); /* */
NextRecord(new) = RecordNULL; /* */
PrevRecord(new) = RecordNULL; /* */
RecordType(new) = token; /* */
RecordFree(new) = size; /* */
RecordComment(new) = sym_empty; /* */
RecordSource(new) = sym_empty; /* */
RecordLineno(new) = -1; /* */
RecordFlags(new) = 0; /* */
RecordHeap(new) = new_heap; /* */
for (i = 0; i < RecordFree(new); ++i) /* */
{ *(new_heap++) = NO_SYMBOL; } /* */
return (new); /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: free_record()
** Purpose: Release a list of records. All records reachable through a
** previous/next chain are deallocated.
** Arguments:
** rec Arbitrary Record in the chain.
** Returns: nothing
**___________________________________________________ */
void free_record(rec) /* */
Record rec; /* */
{ Record r; /* */
/* */
if ( rec == RecordNULL ) return; /* */
/* */
while ( PrevRecord(rec) != RecordNULL ) /* rewind */
{ rec = PrevRecord(rec); } /* */
/* */
while ( rec != RecordNULL ) /* */
{ r = rec; /* */
rec = NextRecord(rec); /* */
free_1_record(r); /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: free_1_record()
** Purpose: Free the memory occupied by a single record.
** This does not ensure that there is no dangling pointer to
** the record. Thus beware!
** Arguments:
** rec record to free
** Returns: nothing
**___________________________________________________ */
void free_1_record(rec) /* */
Record rec; /* */
{ int i; /* */
/* */
if ( rec != RecordNULL ) /* */
{ /* */
UnlinkSymbol(RecordSortkey(rec)); /* */
UnlinkSymbol(RecordOldKey(rec)); /* */
UnlinkSymbol(RecordComment(rec)); /* */
UnlinkSymbol(RecordSource(rec)); /* */
if ( RecordHeap(rec) != NULL ) /* */
{ /* */
for (i = 0; i < RecordFree(rec); i++ ) /* */
{ UnlinkSymbol(RecordHeap(rec)[i]); } /* */
free(RecordHeap(rec)); /* */
} /* */
free(rec); /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: unlink_record()
** Purpose: Remove a record from a chain and free its memory. The
** chain is modified such that the freed Record is not
** referenced any more. A neighbor in the chain of the
** given record is returned or |NULL| if there is none.
** Arguments:
** rec Record to free.
** Returns: nothing
**___________________________________________________ */
Record unlink_record(rec) /* */
Record rec; /* */
{ Record r; /* */
/* */
if ( NextRecord(rec) ) /* */
{ PrevRecord(NextRecord(rec)) = PrevRecord(rec); /* */
if (PrevRecord(rec)) /* */
{ NextRecord(PrevRecord(rec)) = NextRecord(rec);/* */
} /* */
r = NextRecord(rec); /* */
} /* */
else if (PrevRecord(rec)) /* */
{ NextRecord(PrevRecord(rec)) = NextRecord(rec); /* */
r = PrevRecord(rec); /* */
} /* */
else /* */
{ r = RecordNULL; /* */
} /* */
free_1_record(rec); /* */
return r; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: record_gc()
** Purpose: Garbage collecting a record list. The entries marked
** as deleted are unlinked and the memory is freed. Any
** pointer to such a deleted entry becomes invalid.
**
** Be careful when using this function!
** Arguments:
** rec Pointer to any entry in the chain.
** Returns: Pointer to some entry in the cleared chain or
** |RecordNULL| if none is left.
**___________________________________________________ */
Record record_gc(rec) /* */
Record rec; /* */
{ Record ret = RecordNULL; /* */
/* */
if ( rec == RecordNULL ) return rec; /* */
/* */
while ( PrevRecord(rec) != RecordNULL ) /* Rewind */
{ rec = PrevRecord(rec); } /* */
/* */
while ( rec != RecordNULL ) /* */
{ /* */
if ( RecordIsDELETED(rec) ) /* */
{ rec = unlink_record(rec); /* */
} /* */
else /* */
{ ret = rec; /* */
rec = NextRecord(rec); /* */
} /* */
} /* */
/* */
return ret; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: record_get()
** Type: String
** Purpose:
**
** Arguments:
** rec the record
** key the key
** Returns:
**___________________________________________________ */
Symbol record_get(rec, key) /* */
Record rec; /* */
Symbol key; /* */
{ int i; /* */
register Symbol *hp; /* */
register Symbol s, t; /* */
/* */
for (i = RecordFree(rec), hp = RecordHeap(rec); /* visit all fields */
i > 0; /* */
i -= 2) /* */
{ s = *hp++; /* */
t = *hp++; /* */
if (s == key && t != NO_SYMBOL) /* */
{ return t; } /* */
} /* */
/* */
return NO_SYMBOL; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: count_record()
** Type: int
** Purpose:
**
** Arguments:
** rec the record
** Returns:
**___________________________________________________ */
int count_record(rec) /* */
Record rec; /* */
{ int len = 0; /* */
int i; /* */
register Symbol *hp; /* */
register Symbol t; /* */
/* */
for (i = RecordFree(rec), hp = RecordHeap(rec); /* visit all fields */
i > 0; /* */
i -= 2) /* */
{ t = *hp++; /* */
if (t != NO_SYMBOL) { len++; } /* */
} /* */
/* */
return len; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: push_to_record()
** Purpose: Put an equation s=t onto the heap of a record.
** If a field s is already there then the value is
** overwritten. The arguments are expected to be
** symbols. Thus it is not necessary to make private
** copies and it is possible to avoid expensive string
** comparisons.
** Arguments:
** rec the record
** s Left hand side of the equation.
** t Right hand side of the equation.
** err indicator that a warning for double fields should be issued.
** Returns: nothing
**___________________________________________________ */
void push_to_record(rec, s, t, err) /* */
register Record rec; /* */
register Symbol s; /* */
register Symbol t; /* */
bool err; /* */
{ register int i; /* */
/* */
if (s == sym_crossref || s == sym_xdata) /* */
{ SetRecordXREF(rec); } /* */
/* */
for (i = 2; i < RecordFree(rec); i += 2) /* search the field */
{ if (RecordHeap(rec)[i] == s) /* if found then */
{ sym_unlink(RecordHeap(rec)[i + 1]); /* */
RecordHeap(rec)[i + 1] = t; /* overwrite the value */
if (err) { /* */
error(ERR_WARNING|ERR_FILE, /* */
(String)"Duplicate field `", s, /* */
(String)"' overwritten", NULL, 0, /* */
RecordLineno(rec), RecordSource(rec));/* */
} /* */
return; /* */
} /* */
} /* */
for (i = 2; i < RecordFree(rec); i += 2) /* search empty field */
{ if (RecordHeap(rec)[i] == NO_SYMBOL) /* if found then */
{ RecordHeap(rec)[i++] = s; /* add the new item */
RecordHeap(rec)[i] = t; /* */
return; /* */
} /* */
} /* */
i = RecordFree(rec); /* */
RecordFree(rec) += 2; /* */
if ( (RecordHeap(rec) /* enlarge the heap */
=(Symbol*)realloc(RecordHeap(rec), /* */
RecordFree(rec)*sizeof(Symbol)))/* */
== (Symbol*)NULL ) /* */
{ OUT_OF_MEMORY("heap"); } /* */
/* */
RecordHeap(rec)[i++] = s; /* */
RecordHeap(rec)[i] = t; /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: provide_to_record()
** Purpose: Put an equation s=t onto the heap of a record if the key s
** is not defined already.
** If a field s is already there then the value is
** ignored. The arguments are expected to be
** symbols. Thus it is not necessary to make private
** copies and it is possible to avoid expensive string
** comparisons.
** Arguments:
** s Left hand side of the equation.
** t Right hand side of the equation.
** Returns: nothing
**___________________________________________________ */
void provide_to_record(rec,s,t) /* */
register Record rec; /* */
register Symbol s; /* */
register Symbol t; /* */
{ register int i; /* */
/* */
if (s == sym_crossref || s == sym_xdata) /* */
{ SetRecordXREF(rec); } /* */
/* */
for (i = 2; i < RecordFree(rec); i += 2 ) /* search the field */
{ if ( RecordHeap(rec)[i] == s ) /* if found then */
{ return; } /* done */
} /* */
for (i = 2; i < RecordFree(rec); i += 2 ) /* search empty field */
{ if ( RecordHeap(rec)[i] == NO_SYMBOL ) /* if found then */
{ RecordHeap(rec)[i++] = s; /* add the new item */
RecordHeap(rec)[i] = t; /* */
return; /* */
} /* */
} /* */
i = RecordFree(rec); /* */
RecordFree(rec) += 2; /* */
if ( (RecordHeap(rec) /* enlarge the heap */
=(Symbol*)realloc(RecordHeap(rec), /* */
RecordFree(rec)*sizeof(Symbol)))/* */
== (Symbol*)NULL ) /* */
{ OUT_OF_MEMORY("heap"); } /* */
/* */
RecordHeap(rec)[i++] = s; /* */
RecordHeap(rec)[i] = t; /* */
} /*------------------------*/
/*****************************************************************************/
/*** ***/
/*****************************************************************************/
#include <bibtool/wordlist.h>
#include <bibtool/s_parse.h>
#include <bibtool/entry.h>
/*-----------------------------------------------------------------------------
** Function: new_wordlist()
** Purpose: Allocate a WordList and fill its slots.
** Arguments:
** s Initial string to fill in the WordList structure
** Returns:
**___________________________________________________ */
WordList new_wordlist(s) /* */
Symbol s; /* */
{ register WordList wl; /* */
if ( (wl=(WordList)malloc(sizeof(SWordList))) == WordNULL )/* */
{ OUT_OF_MEMORY("WordList"); } /* */
ThisWord(wl) = s; /* */
NextWord(wl) = WordNULL; /* */
return wl; /* */
} /*------------------------*/
typedef struct oRDERLIST /* */
{ int ol_type; /* */
WordList ol_val; /* */
struct oRDERLIST *ol_next; /* */
} SOrderList, *OrderList; /* */
#define OrderType(OL) ((OL)->ol_type)
#define OrderVal(OL) ((OL)->ol_val)
#define NextOrder(OL) ((OL)->ol_next)
#define OrderNULL ((OrderList)0)
static OrderList order = OrderNULL; /* */
/*-----------------------------------------------------------------------------
** Function: add_sort_order()
** Purpose: Insert the sort order into the order list.
**
**
** Arguments:
** val string resource of the order.
** Returns: nothing
**___________________________________________________ */
void add_sort_order(val) /* */
Symbol val; /* */
{ Symbol sym; /* */
rec_type type; /* */
OrderList ol; /* */
WordList *wlp = NULL; /* */
WordList wl, wl_next; /* */
String s = SymbolValue(val); /* */
/* */
sp_skip(&s); /* */
if (*s == '*') /* */
{ sym = NO_SYMBOL; /* */
type = BIB_NOOP; /* */
s++; /* */
} /* */
else if ((sym=SParseSymbol(&s)) == NO_SYMBOL) /* */
{ return; } /* */
else if ((type=find_entry_type(SymbolValue(sym))) == BIB_NOOP)/* */
{ Err("Undefined entry type for sort order"); /* */
return; /* */
} /* */
/* */
for (ol = order; /* */
ol != OrderNULL && OrderType(ol) != type; /* */
ol = NextOrder(ol)) {} /* */
/* */
if ( ol ) /* */
{ wlp = &OrderVal(ol); /* */
} /* */
else if ( (ol=(OrderList)malloc(sizeof(SOrderList))) == OrderNULL )/* */
{ OUT_OF_MEMORY("OrderList"); } /* */
else /* */
{ OrderType(ol) = type; /* */
OrderVal(ol) = WordNULL; /* */
NextOrder(ol) = order; /* */
order = ol; /* */
wlp = &OrderVal(ol); /* */
} /* */
/* */
sp_skip(&s); /* */
/* */
while ( *s ) /* */
{ if ( (sym=SParseSymbol(&s)) != NO_SYMBOL ) /* */
{ if ( *wlp ) { ThisWord(*wlp) = sym; } /* */
else { *wlp = new_wordlist(sym); } /* */
wlp = &NextWord(*wlp); /* */
} /* */
else break; /* */
sp_skip(&s); /* */
} /* */
/* */
wl = *wlp; /* */
*wlp = WordNULL; /* */
while ( wl ) /* Free remaining word */
{ wl_next = NextWord(wl); /* nodes at the end. */
free((char*)wl); /* */
wl = wl_next; /* */
} /* */
} /*------------------------*/
/*-----------------------------------------------------------------------------
** Function: sort_record()
** Purpose: The heap is reordered according to the sorting order
** determined by the record type.
** For this purpose a copy of the original record is made and the
** original record is overwritten. The copy is released at the
** end.
** Memory management is easy since all strings are in fact
** symbols, i.e. they must not be freed and comparison is done
** by pointer comparison.
** Arguments:
** rec Record to sort
** Returns: nothing
**___________________________________________________ */
void sort_record(rec) /* */
register Record rec; /* */
{ Record r; /* Temp. record */
OrderList ol; /* */
int i, ptr; /* */
int type = RecordType(rec); /* */
WordList wl; /* */
/* */
for (ol = order; /* Search for an order */
ol && OrderType(ol) != type; /* for this type of */
ol = NextOrder(ol) ) {} /* record first. */
if ( ol == OrderNULL ) /* If none was found then */
{ for (ol = order; /* Search for an order */
ol && OrderType(ol) != BIB_NOOP; /* applicable for all */
ol = NextOrder(ol) ) {} /* record types. */
if ( ol == OrderNULL ) return; /* No order then return. */
} /* */
/* */
if ( (wl=OrderVal(ol)) == WordNULL ) return; /* Empty order found. Done*/
/* */
r = copy_record(rec); /* */
ptr = 2; /* Heap pointer for rec */
/* */
while ( wl ) /* For all words */
{ for ( i = 2; i < RecordFree(r); i += 2 ) /* Find the word on the */
{ if ( RecordHeap(r)[i] == ThisWord(wl) ) /* heap and */
{ RecordHeap(rec)[ptr++] = RecordHeap(r)[i]; /* transfer it into the */
RecordHeap(rec)[ptr++] = RecordHeap(r)[i+1];/* original record. */
RecordHeap(r)[i] = NULL; /* Delete the copy. */
i = RecordFree(r); /* */
} /* */
} /* */
wl = NextWord(wl); /* Look at next word. */
} /* */
for ( i = 2; i < RecordFree(r); i += 2 ) /* Transfer all remaining */
{ if ( RecordHeap(r)[i] ) /* elements of the heap */
{ RecordHeap(rec)[ptr++] = RecordHeap(r)[i]; /* which are not */
RecordHeap(rec)[ptr++] = RecordHeap(r)[i+1]; /* deleted. */
} /* */
} /* */
while ( ptr < RecordFree(r) ) /* Clear the remaining */
{ RecordHeap(rec)[ptr++] = NULL; /* elements of the heap. */
} /* */
/* */
free((char*)RecordHeap(r)); /* Free the temp. record. */
free((char*)r); /* */
} /*------------------------*/
/*---------------------------------------------------------------------------*/
|