| 1 | /*** record.c ***************************************************************** |
| 2 | ** |
| 3 | ** This file is part of BibTool. |
| 4 | ** It is distributed under the GNU General Public License. |
| 5 | ** See the file COPYING for details. |
| 6 | ** |
| 7 | ** (c) 1996-2020 Gerd Neugebauer |
| 8 | ** |
| 9 | ** Net: [email protected] |
| 10 | ** |
| 11 | ** This program is free software; you can redistribute it and/or modify |
| 12 | ** it under the terms of the GNU General Public License as published by |
| 13 | ** the Free Software Foundation; either version 2, or (at your option) |
| 14 | ** any later version. |
| 15 | ** |
| 16 | ** This program is distributed in the hope that it will be useful, |
| 17 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | ** GNU General Public License for more details. |
| 20 | ** |
| 21 | ** You should have received a copy of the GNU General Public License |
| 22 | ** along with this program; if not, write to the Free Software |
| 23 | ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 24 | ** |
| 25 | ******************************************************************************/ |
| 26 | |
| 27 | #include <bibtool/general.h> |
| 28 | #include <bibtool/error.h> |
| 29 | #include <bibtool/record.h> |
| 30 | #include <bibtool/symbols.h> |
| 31 | |
| 32 | /*****************************************************************************/ |
| 33 | /* Internal Programs */ |
| 34 | /*===========================================================================*/ |
| 35 | |
| 36 | #ifdef __STDC__ |
| 37 | #define _ARG(A) A |
| 38 | #else |
| 39 | #define _ARG(A) () |
| 40 | #endif |
| 41 | Record copy_record _ARG((Record rec)); /* record.c */ |
| 42 | Record new_record _ARG((int token,int size)); /* record.c */ |
| 43 | Record record_gc _ARG((Record rec)); /* record.c */ |
| 44 | Record unlink_record _ARG((Record rec)); /* record.c */ |
| 45 | Symbol record_get _ARG((Record rec, Symbol key)); /* record.c */ |
| 46 | WordList new_wordlist _ARG((Symbol sym)); /* record.c */ |
| 47 | int count_record _ARG((Record rec)); /* record.c */ |
| 48 | void add_sort_order _ARG((Symbol val)); /* record.c */ |
| 49 | void free_1_record _ARG((Record rec)); /* record.c */ |
| 50 | void free_record _ARG((Record rec)); /* record.c */ |
| 51 | void provide_to_record _ARG((Record rec,Symbol s,Symbol t));/* record.c */ |
| 52 | void push_to_record _ARG((Record rec,Symbol s,Symbol t, bool err));/* record.c*/ |
| 53 | void sort_record _ARG((Record rec)); /* record.c */ |
| 54 | |
| 55 | /*****************************************************************************/ |
| 56 | /* External Programs */ |
| 57 | /*===========================================================================*/ |
| 58 | |
| 59 | /*---------------------------------------------------------------------------*/ |
| 60 | |
| 61 | /*----------------------------------------------------------------------------- |
| 62 | ** Function: copy_record() |
| 63 | ** Purpose: Copy a record and return a new instance. |
| 64 | ** If no memory is left then an error is raised and the |
| 65 | ** program is terminated. |
| 66 | ** Arguments: |
| 67 | ** rec The record to copy. |
| 68 | ** Returns: The new copy of |rec|. |
| 69 | **___________________________________________________ */ |
| 70 | Record copy_record(rec) /* */ |
| 71 | register Record rec; /* */ |
| 72 | { register Record new; /* */ |
| 73 | register Symbol *new_heap, /* */ |
| 74 | *old_heap; /* */ |
| 75 | register int i; /* */ |
| 76 | /* */ |
| 77 | if ( (new=(Record)malloc(sizeof(SRecord))) == 0L/* */ |
| 78 | || (new_heap=(Symbol*)malloc(sizeof(Symbol)*(size_t)RecordFree(rec)))/* */ |
| 79 | == 0L ) /* */ |
| 80 | { OUT_OF_MEMORY("Record"); } /* */ |
| 81 | RecordSortkey(new) = sym_empty; /* */ |
| 82 | RecordOldKey(new) = RecordOldKey(rec); /* */ |
| 83 | NextRecord(new) = RecordNULL; /* */ |
| 84 | PrevRecord(new) = RecordNULL; /* */ |
| 85 | RecordType(new) = RecordType(rec); /* */ |
| 86 | RecordFree(new) = RecordFree(rec); /* */ |
| 87 | RecordComment(new) = RecordComment(rec); /* */ |
| 88 | RecordSource(new) = RecordSource(rec); /* */ |
| 89 | if (RecordSource(new)) { /* */ |
| 90 | LinkSymbol(RecordSource(new)); /* */ |
| 91 | } /* */ |
| 92 | RecordLineno(new) = RecordLineno(rec); /* */ |
| 93 | RecordHeap(new) = new_heap; /* */ |
| 94 | RecordFlags(new) = RecordFlags(rec); /* */ |
| 95 | for (i = 0, old_heap = RecordHeap(rec); /* */ |
| 96 | i < RecordFree(new); /* */ |
| 97 | ++i) /* */ |
| 98 | { *(new_heap++) = *(old_heap++); } /* */ |
| 99 | return (new); /* */ |
| 100 | } /*------------------------*/ |
| 101 | |
| 102 | /*----------------------------------------------------------------------------- |
| 103 | ** Function: new_record() |
| 104 | ** Purpose: Create a new record and return it. |
| 105 | ** If no memory is left then an error is raised and the |
| 106 | ** program is terminated. |
| 107 | ** Arguments: |
| 108 | ** token The token type of the record. |
| 109 | ** size The initial heap size. |
| 110 | ** Returns: The new record. |
| 111 | **___________________________________________________ */ |
| 112 | Record new_record(token,size) /* */ |
| 113 | int token; /* */ |
| 114 | int size; /* */ |
| 115 | { register Record new; /* */ |
| 116 | register Symbol *new_heap; /* */ |
| 117 | register int i; /* */ |
| 118 | /* */ |
| 119 | if ( size < 1 ) size = 1; /* */ |
| 120 | if ( (new=(Record)malloc(sizeof(SRecord))) == 0L/* */ |
| 121 | || (new_heap=(Symbol*)malloc(sizeof(Symbol)*(size_t)(size)))/* */ |
| 122 | == 0L ) /* */ |
| 123 | { OUT_OF_MEMORY("Record"); } /* */ |
| 124 | RecordSortkey(new) = sym_empty; /* */ |
| 125 | RecordOldKey(new) = RecordSortkey(new); /* */ |
| 126 | NextRecord(new) = RecordNULL; /* */ |
| 127 | PrevRecord(new) = RecordNULL; /* */ |
| 128 | RecordType(new) = token; /* */ |
| 129 | RecordFree(new) = size; /* */ |
| 130 | RecordComment(new) = sym_empty; /* */ |
| 131 | RecordSource(new) = sym_empty; /* */ |
| 132 | RecordLineno(new) = -1; /* */ |
| 133 | RecordFlags(new) = 0; /* */ |
| 134 | RecordHeap(new) = new_heap; /* */ |
| 135 | for (i = 0; i < RecordFree(new); ++i) /* */ |
| 136 | { *(new_heap++) = NO_SYMBOL; } /* */ |
| 137 | return (new); /* */ |
| 138 | } /*------------------------*/ |
| 139 | |
| 140 | /*----------------------------------------------------------------------------- |
| 141 | ** Function: free_record() |
| 142 | ** Purpose: Release a list of records. All records reachable through a |
| 143 | ** previous/next chain are deallocated. |
| 144 | ** Arguments: |
| 145 | ** rec Arbitrary Record in the chain. |
| 146 | ** Returns: nothing |
| 147 | **___________________________________________________ */ |
| 148 | void free_record(rec) /* */ |
| 149 | Record rec; /* */ |
| 150 | { Record r; /* */ |
| 151 | /* */ |
| 152 | if ( rec == RecordNULL ) return; /* */ |
| 153 | /* */ |
| 154 | while ( PrevRecord(rec) != RecordNULL ) /* rewind */ |
| 155 | { rec = PrevRecord(rec); } /* */ |
| 156 | /* */ |
| 157 | while ( rec != RecordNULL ) /* */ |
| 158 | { r = rec; /* */ |
| 159 | rec = NextRecord(rec); /* */ |
| 160 | free_1_record(r); /* */ |
| 161 | } /* */ |
| 162 | } /*------------------------*/ |
| 163 | |
| 164 | /*----------------------------------------------------------------------------- |
| 165 | ** Function: free_1_record() |
| 166 | ** Purpose: Free the memory occupied by a single record. |
| 167 | ** This does not ensure that there is no dangling pointer to |
| 168 | ** the record. Thus beware! |
| 169 | ** Arguments: |
| 170 | ** rec record to free |
| 171 | ** Returns: nothing |
| 172 | **___________________________________________________ */ |
| 173 | void free_1_record(rec) /* */ |
| 174 | Record rec; /* */ |
| 175 | { int i; /* */ |
| 176 | /* */ |
| 177 | if ( rec != RecordNULL ) /* */ |
| 178 | { /* */ |
| 179 | UnlinkSymbol(RecordSortkey(rec)); /* */ |
| 180 | UnlinkSymbol(RecordOldKey(rec)); /* */ |
| 181 | UnlinkSymbol(RecordComment(rec)); /* */ |
| 182 | UnlinkSymbol(RecordSource(rec)); /* */ |
| 183 | if ( RecordHeap(rec) != NULL ) /* */ |
| 184 | { /* */ |
| 185 | for (i = 0; i < RecordFree(rec); i++ ) /* */ |
| 186 | { UnlinkSymbol(RecordHeap(rec)[i]); } /* */ |
| 187 | free(RecordHeap(rec)); /* */ |
| 188 | } /* */ |
| 189 | free(rec); /* */ |
| 190 | } /* */ |
| 191 | } /*------------------------*/ |
| 192 | |
| 193 | /*----------------------------------------------------------------------------- |
| 194 | ** Function: unlink_record() |
| 195 | ** Purpose: Remove a record from a chain and free its memory. The |
| 196 | ** chain is modified such that the freed Record is not |
| 197 | ** referenced any more. A neighbor in the chain of the |
| 198 | ** given record is returned or |NULL| if there is none. |
| 199 | ** Arguments: |
| 200 | ** rec Record to free. |
| 201 | ** Returns: nothing |
| 202 | **___________________________________________________ */ |
| 203 | Record unlink_record(rec) /* */ |
| 204 | Record rec; /* */ |
| 205 | { Record r; /* */ |
| 206 | /* */ |
| 207 | if ( NextRecord(rec) ) /* */ |
| 208 | { PrevRecord(NextRecord(rec)) = PrevRecord(rec); /* */ |
| 209 | if (PrevRecord(rec)) /* */ |
| 210 | { NextRecord(PrevRecord(rec)) = NextRecord(rec);/* */ |
| 211 | } /* */ |
| 212 | r = NextRecord(rec); /* */ |
| 213 | } /* */ |
| 214 | else if (PrevRecord(rec)) /* */ |
| 215 | { NextRecord(PrevRecord(rec)) = NextRecord(rec); /* */ |
| 216 | r = PrevRecord(rec); /* */ |
| 217 | } /* */ |
| 218 | else /* */ |
| 219 | { r = RecordNULL; /* */ |
| 220 | } /* */ |
| 221 | free_1_record(rec); /* */ |
| 222 | return r; /* */ |
| 223 | } /*------------------------*/ |
| 224 | |
| 225 | /*----------------------------------------------------------------------------- |
| 226 | ** Function: record_gc() |
| 227 | ** Purpose: Garbage collecting a record list. The entries marked |
| 228 | ** as deleted are unlinked and the memory is freed. Any |
| 229 | ** pointer to such a deleted entry becomes invalid. |
| 230 | ** |
| 231 | ** Be careful when using this function! |
| 232 | ** Arguments: |
| 233 | ** rec Pointer to any entry in the chain. |
| 234 | ** Returns: Pointer to some entry in the cleared chain or |
| 235 | ** |RecordNULL| if none is left. |
| 236 | **___________________________________________________ */ |
| 237 | Record record_gc(rec) /* */ |
| 238 | Record rec; /* */ |
| 239 | { Record ret = RecordNULL; /* */ |
| 240 | /* */ |
| 241 | if ( rec == RecordNULL ) return rec; /* */ |
| 242 | /* */ |
| 243 | while ( PrevRecord(rec) != RecordNULL ) /* Rewind */ |
| 244 | { rec = PrevRecord(rec); } /* */ |
| 245 | /* */ |
| 246 | while ( rec != RecordNULL ) /* */ |
| 247 | { /* */ |
| 248 | if ( RecordIsDELETED(rec) ) /* */ |
| 249 | { rec = unlink_record(rec); /* */ |
| 250 | } /* */ |
| 251 | else /* */ |
| 252 | { ret = rec; /* */ |
| 253 | rec = NextRecord(rec); /* */ |
| 254 | } /* */ |
| 255 | } /* */ |
| 256 | /* */ |
| 257 | return ret; /* */ |
| 258 | } /*------------------------*/ |
| 259 | |
| 260 | /*----------------------------------------------------------------------------- |
| 261 | ** Function: record_get() |
| 262 | ** Type: String |
| 263 | ** Purpose: |
| 264 | ** |
| 265 | ** Arguments: |
| 266 | ** rec the record |
| 267 | ** key the key |
| 268 | ** Returns: |
| 269 | **___________________________________________________ */ |
| 270 | Symbol record_get(rec, key) /* */ |
| 271 | Record rec; /* */ |
| 272 | Symbol key; /* */ |
| 273 | { int i; /* */ |
| 274 | register Symbol *hp; /* */ |
| 275 | register Symbol s, t; /* */ |
| 276 | /* */ |
| 277 | for (i = RecordFree(rec), hp = RecordHeap(rec); /* visit all fields */ |
| 278 | i > 0; /* */ |
| 279 | i -= 2) /* */ |
| 280 | { s = *hp++; /* */ |
| 281 | t = *hp++; /* */ |
| 282 | if (s == key && t != NO_SYMBOL) /* */ |
| 283 | { return t; } /* */ |
| 284 | } /* */ |
| 285 | /* */ |
| 286 | return NO_SYMBOL; /* */ |
| 287 | } /*------------------------*/ |
| 288 | |
| 289 | /*----------------------------------------------------------------------------- |
| 290 | ** Function: count_record() |
| 291 | ** Type: int |
| 292 | ** Purpose: |
| 293 | ** |
| 294 | ** Arguments: |
| 295 | ** rec the record |
| 296 | ** Returns: |
| 297 | **___________________________________________________ */ |
| 298 | int count_record(rec) /* */ |
| 299 | Record rec; /* */ |
| 300 | { int len = 0; /* */ |
| 301 | int i; /* */ |
| 302 | register Symbol *hp; /* */ |
| 303 | register Symbol t; /* */ |
| 304 | /* */ |
| 305 | for (i = RecordFree(rec), hp = RecordHeap(rec); /* visit all fields */ |
| 306 | i > 0; /* */ |
| 307 | i -= 2) /* */ |
| 308 | { t = *hp++; /* */ |
| 309 | if (t != NO_SYMBOL) { len++; } /* */ |
| 310 | } /* */ |
| 311 | /* */ |
| 312 | return len; /* */ |
| 313 | } /*------------------------*/ |
| 314 | |
| 315 | /*----------------------------------------------------------------------------- |
| 316 | ** Function: push_to_record() |
| 317 | ** Purpose: Put an equation s=t onto the heap of a record. |
| 318 | ** If a field s is already there then the value is |
| 319 | ** overwritten. The arguments are expected to be |
| 320 | ** symbols. Thus it is not necessary to make private |
| 321 | ** copies and it is possible to avoid expensive string |
| 322 | ** comparisons. |
| 323 | ** Arguments: |
| 324 | ** rec the record |
| 325 | ** s Left hand side of the equation. |
| 326 | ** t Right hand side of the equation. |
| 327 | ** err indicator that a warning for double fields should be issued. |
| 328 | ** Returns: nothing |
| 329 | **___________________________________________________ */ |
| 330 | void push_to_record(rec, s, t, err) /* */ |
| 331 | register Record rec; /* */ |
| 332 | register Symbol s; /* */ |
| 333 | register Symbol t; /* */ |
| 334 | bool err; /* */ |
| 335 | { register int i; /* */ |
| 336 | /* */ |
| 337 | if (s == sym_crossref || s == sym_xdata) /* */ |
| 338 | { SetRecordXREF(rec); } /* */ |
| 339 | /* */ |
| 340 | for (i = 2; i < RecordFree(rec); i += 2) /* search the field */ |
| 341 | { if (RecordHeap(rec)[i] == s) /* if found then */ |
| 342 | { sym_unlink(RecordHeap(rec)[i + 1]); /* */ |
| 343 | RecordHeap(rec)[i + 1] = t; /* overwrite the value */ |
| 344 | if (err) { /* */ |
| 345 | error(ERR_WARNING|ERR_FILE, /* */ |
| 346 | (String)"Duplicate field `", s, /* */ |
| 347 | (String)"' overwritten", NULL, 0, /* */ |
| 348 | RecordLineno(rec), RecordSource(rec));/* */ |
| 349 | } /* */ |
| 350 | return; /* */ |
| 351 | } /* */ |
| 352 | } /* */ |
| 353 | for (i = 2; i < RecordFree(rec); i += 2) /* search empty field */ |
| 354 | { if (RecordHeap(rec)[i] == NO_SYMBOL) /* if found then */ |
| 355 | { RecordHeap(rec)[i++] = s; /* add the new item */ |
| 356 | RecordHeap(rec)[i] = t; /* */ |
| 357 | return; /* */ |
| 358 | } /* */ |
| 359 | } /* */ |
| 360 | i = RecordFree(rec); /* */ |
| 361 | RecordFree(rec) += 2; /* */ |
| 362 | if ( (RecordHeap(rec) /* enlarge the heap */ |
| 363 | =(Symbol*)realloc(RecordHeap(rec), /* */ |
| 364 | RecordFree(rec)*sizeof(Symbol)))/* */ |
| 365 | == (Symbol*)NULL ) /* */ |
| 366 | { OUT_OF_MEMORY("heap"); } /* */ |
| 367 | /* */ |
| 368 | RecordHeap(rec)[i++] = s; /* */ |
| 369 | RecordHeap(rec)[i] = t; /* */ |
| 370 | } /*------------------------*/ |
| 371 | |
| 372 | /*----------------------------------------------------------------------------- |
| 373 | ** Function: provide_to_record() |
| 374 | ** Purpose: Put an equation s=t onto the heap of a record if the key s |
| 375 | ** is not defined already. |
| 376 | ** If a field s is already there then the value is |
| 377 | ** ignored. The arguments are expected to be |
| 378 | ** symbols. Thus it is not necessary to make private |
| 379 | ** copies and it is possible to avoid expensive string |
| 380 | ** comparisons. |
| 381 | ** Arguments: |
| 382 | ** s Left hand side of the equation. |
| 383 | ** t Right hand side of the equation. |
| 384 | ** Returns: nothing |
| 385 | **___________________________________________________ */ |
| 386 | void provide_to_record(rec,s,t) /* */ |
| 387 | register Record rec; /* */ |
| 388 | register Symbol s; /* */ |
| 389 | register Symbol t; /* */ |
| 390 | { register int i; /* */ |
| 391 | /* */ |
| 392 | if (s == sym_crossref || s == sym_xdata) /* */ |
| 393 | { SetRecordXREF(rec); } /* */ |
| 394 | /* */ |
| 395 | for (i = 2; i < RecordFree(rec); i += 2 ) /* search the field */ |
| 396 | { if ( RecordHeap(rec)[i] == s ) /* if found then */ |
| 397 | { return; } /* done */ |
| 398 | } /* */ |
| 399 | for (i = 2; i < RecordFree(rec); i += 2 ) /* search empty field */ |
| 400 | { if ( RecordHeap(rec)[i] == NO_SYMBOL ) /* if found then */ |
| 401 | { RecordHeap(rec)[i++] = s; /* add the new item */ |
| 402 | RecordHeap(rec)[i] = t; /* */ |
| 403 | return; /* */ |
| 404 | } /* */ |
| 405 | } /* */ |
| 406 | i = RecordFree(rec); /* */ |
| 407 | RecordFree(rec) += 2; /* */ |
| 408 | if ( (RecordHeap(rec) /* enlarge the heap */ |
| 409 | =(Symbol*)realloc(RecordHeap(rec), /* */ |
| 410 | RecordFree(rec)*sizeof(Symbol)))/* */ |
| 411 | == (Symbol*)NULL ) /* */ |
| 412 | { OUT_OF_MEMORY("heap"); } /* */ |
| 413 | /* */ |
| 414 | RecordHeap(rec)[i++] = s; /* */ |
| 415 | RecordHeap(rec)[i] = t; /* */ |
| 416 | } /*------------------------*/ |
| 417 | |
| 418 | |
| 419 | /*****************************************************************************/ |
| 420 | /*** ***/ |
| 421 | /*****************************************************************************/ |
| 422 | |
| 423 | #include <bibtool/wordlist.h> |
| 424 | #include <bibtool/s_parse.h> |
| 425 | #include <bibtool/entry.h> |
| 426 | |
| 427 | |
| 428 | /*----------------------------------------------------------------------------- |
| 429 | ** Function: new_wordlist() |
| 430 | ** Purpose: Allocate a WordList and fill its slots. |
| 431 | ** Arguments: |
| 432 | ** s Initial string to fill in the WordList structure |
| 433 | ** Returns: |
| 434 | **___________________________________________________ */ |
| 435 | WordList new_wordlist(s) /* */ |
| 436 | Symbol s; /* */ |
| 437 | { register WordList wl; /* */ |
| 438 | if ( (wl=(WordList)malloc(sizeof(SWordList))) == WordNULL )/* */ |
| 439 | { OUT_OF_MEMORY("WordList"); } /* */ |
| 440 | ThisWord(wl) = s; /* */ |
| 441 | NextWord(wl) = WordNULL; /* */ |
| 442 | return wl; /* */ |
| 443 | } /*------------------------*/ |
| 444 | |
| 445 | typedef struct oRDERLIST /* */ |
| 446 | { int ol_type; /* */ |
| 447 | WordList ol_val; /* */ |
| 448 | struct oRDERLIST *ol_next; /* */ |
| 449 | } SOrderList, *OrderList; /* */ |
| 450 | |
| 451 | #define OrderType(OL) ((OL)->ol_type) |
| 452 | #define OrderVal(OL) ((OL)->ol_val) |
| 453 | #define NextOrder(OL) ((OL)->ol_next) |
| 454 | #define OrderNULL ((OrderList)0) |
| 455 | |
| 456 | static OrderList order = OrderNULL; /* */ |
| 457 | |
| 458 | /*----------------------------------------------------------------------------- |
| 459 | ** Function: add_sort_order() |
| 460 | ** Purpose: Insert the sort order into the order list. |
| 461 | ** |
| 462 | ** |
| 463 | ** Arguments: |
| 464 | ** val string resource of the order. |
| 465 | ** Returns: nothing |
| 466 | **___________________________________________________ */ |
| 467 | void add_sort_order(val) /* */ |
| 468 | Symbol val; /* */ |
| 469 | { Symbol sym; /* */ |
| 470 | rec_type type; /* */ |
| 471 | OrderList ol; /* */ |
| 472 | WordList *wlp = NULL; /* */ |
| 473 | WordList wl, wl_next; /* */ |
| 474 | String s = SymbolValue(val); /* */ |
| 475 | /* */ |
| 476 | sp_skip(&s); /* */ |
| 477 | if (*s == '*') /* */ |
| 478 | { sym = NO_SYMBOL; /* */ |
| 479 | type = BIB_NOOP; /* */ |
| 480 | s++; /* */ |
| 481 | } /* */ |
| 482 | else if ((sym=SParseSymbol(&s)) == NO_SYMBOL) /* */ |
| 483 | { return; } /* */ |
| 484 | else if ((type=find_entry_type(SymbolValue(sym))) == BIB_NOOP)/* */ |
| 485 | { Err("Undefined entry type for sort order"); /* */ |
| 486 | return; /* */ |
| 487 | } /* */ |
| 488 | /* */ |
| 489 | for (ol = order; /* */ |
| 490 | ol != OrderNULL && OrderType(ol) != type; /* */ |
| 491 | ol = NextOrder(ol)) {} /* */ |
| 492 | /* */ |
| 493 | if ( ol ) /* */ |
| 494 | { wlp = &OrderVal(ol); /* */ |
| 495 | } /* */ |
| 496 | else if ( (ol=(OrderList)malloc(sizeof(SOrderList))) == OrderNULL )/* */ |
| 497 | { OUT_OF_MEMORY("OrderList"); } /* */ |
| 498 | else /* */ |
| 499 | { OrderType(ol) = type; /* */ |
| 500 | OrderVal(ol) = WordNULL; /* */ |
| 501 | NextOrder(ol) = order; /* */ |
| 502 | order = ol; /* */ |
| 503 | wlp = &OrderVal(ol); /* */ |
| 504 | } /* */ |
| 505 | /* */ |
| 506 | sp_skip(&s); /* */ |
| 507 | /* */ |
| 508 | while ( *s ) /* */ |
| 509 | { if ( (sym=SParseSymbol(&s)) != NO_SYMBOL ) /* */ |
| 510 | { if ( *wlp ) { ThisWord(*wlp) = sym; } /* */ |
| 511 | else { *wlp = new_wordlist(sym); } /* */ |
| 512 | wlp = &NextWord(*wlp); /* */ |
| 513 | } /* */ |
| 514 | else break; /* */ |
| 515 | sp_skip(&s); /* */ |
| 516 | } /* */ |
| 517 | /* */ |
| 518 | wl = *wlp; /* */ |
| 519 | *wlp = WordNULL; /* */ |
| 520 | while ( wl ) /* Free remaining word */ |
| 521 | { wl_next = NextWord(wl); /* nodes at the end. */ |
| 522 | free((char*)wl); /* */ |
| 523 | wl = wl_next; /* */ |
| 524 | } /* */ |
| 525 | } /*------------------------*/ |
| 526 | |
| 527 | /*----------------------------------------------------------------------------- |
| 528 | ** Function: sort_record() |
| 529 | ** Purpose: The heap is reordered according to the sorting order |
| 530 | ** determined by the record type. |
| 531 | ** For this purpose a copy of the original record is made and the |
| 532 | ** original record is overwritten. The copy is released at the |
| 533 | ** end. |
| 534 | ** Memory management is easy since all strings are in fact |
| 535 | ** symbols, i.e. they must not be freed and comparison is done |
| 536 | ** by pointer comparison. |
| 537 | ** Arguments: |
| 538 | ** rec Record to sort |
| 539 | ** Returns: nothing |
| 540 | **___________________________________________________ */ |
| 541 | void sort_record(rec) /* */ |
| 542 | register Record rec; /* */ |
| 543 | { Record r; /* Temp. record */ |
| 544 | OrderList ol; /* */ |
| 545 | int i, ptr; /* */ |
| 546 | int type = RecordType(rec); /* */ |
| 547 | WordList wl; /* */ |
| 548 | /* */ |
| 549 | for (ol = order; /* Search for an order */ |
| 550 | ol && OrderType(ol) != type; /* for this type of */ |
| 551 | ol = NextOrder(ol) ) {} /* record first. */ |
| 552 | if ( ol == OrderNULL ) /* If none was found then */ |
| 553 | { for (ol = order; /* Search for an order */ |
| 554 | ol && OrderType(ol) != BIB_NOOP; /* applicable for all */ |
| 555 | ol = NextOrder(ol) ) {} /* record types. */ |
| 556 | if ( ol == OrderNULL ) return; /* No order then return. */ |
| 557 | } /* */ |
| 558 | /* */ |
| 559 | if ( (wl=OrderVal(ol)) == WordNULL ) return; /* Empty order found. Done*/ |
| 560 | /* */ |
| 561 | r = copy_record(rec); /* */ |
| 562 | ptr = 2; /* Heap pointer for rec */ |
| 563 | /* */ |
| 564 | while ( wl ) /* For all words */ |
| 565 | { for ( i = 2; i < RecordFree(r); i += 2 ) /* Find the word on the */ |
| 566 | { if ( RecordHeap(r)[i] == ThisWord(wl) ) /* heap and */ |
| 567 | { RecordHeap(rec)[ptr++] = RecordHeap(r)[i]; /* transfer it into the */ |
| 568 | RecordHeap(rec)[ptr++] = RecordHeap(r)[i+1];/* original record. */ |
| 569 | RecordHeap(r)[i] = NULL; /* Delete the copy. */ |
| 570 | i = RecordFree(r); /* */ |
| 571 | } /* */ |
| 572 | } /* */ |
| 573 | wl = NextWord(wl); /* Look at next word. */ |
| 574 | } /* */ |
| 575 | for ( i = 2; i < RecordFree(r); i += 2 ) /* Transfer all remaining */ |
| 576 | { if ( RecordHeap(r)[i] ) /* elements of the heap */ |
| 577 | { RecordHeap(rec)[ptr++] = RecordHeap(r)[i]; /* which are not */ |
| 578 | RecordHeap(rec)[ptr++] = RecordHeap(r)[i+1]; /* deleted. */ |
| 579 | } /* */ |
| 580 | } /* */ |
| 581 | while ( ptr < RecordFree(r) ) /* Clear the remaining */ |
| 582 | { RecordHeap(rec)[ptr++] = NULL; /* elements of the heap. */ |
| 583 | } /* */ |
| 584 | /* */ |
| 585 | free((char*)RecordHeap(r)); /* Free the temp. record. */ |
| 586 | free((char*)r); /* */ |
| 587 | } /*------------------------*/ |
| 588 | |
| 589 | /*---------------------------------------------------------------------------*/ |