| 1 | /*** check.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) 2019-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 | ** Description: |
| 27 | ** |
| 28 | ** |
| 29 | ******************************************************************************/ |
| 30 | |
| 31 | #include <bibtool/general.h> |
| 32 | #include <bibtool/rsc.h> |
| 33 | #include <bibtool/error.h> |
| 34 | #include <bibtool/check.h> |
| 35 | #include <bibtool/wordlist.h> |
| 36 | #include <bibtool/sbuffer.h> |
| 37 | #include "config.h" |
| 38 | |
| 39 | /*****************************************************************************/ |
| 40 | /* Internal Programs */ |
| 41 | /*===========================================================================*/ |
| 42 | |
| 43 | #ifdef __STDC__ |
| 44 | #define _ARG(A) A |
| 45 | #else |
| 46 | #define _ARG(A) () |
| 47 | #endif |
| 48 | static bool do_checks _ARG((DB db,Record rec)); |
| 49 | |
| 50 | /*****************************************************************************/ |
| 51 | /* External Programs and Variables */ |
| 52 | /*===========================================================================*/ |
| 53 | |
| 54 | /*---------------------------------------------------------------------------*/ |
| 55 | |
| 56 | static WordList unique_fields = WordNULL; /* */ |
| 57 | |
| 58 | bool need_sort_key = false; |
| 59 | |
| 60 | /*----------------------------------------------------------------------------- |
| 61 | ** Function: add_unique_field() |
| 62 | ** Type: void |
| 63 | ** Purpose: A unique constraint for a field. |
| 64 | ** |
| 65 | ** Arguments: |
| 66 | ** key the key to check |
| 67 | ** Returns: nothing |
| 68 | **___________________________________________________ */ |
| 69 | void add_unique_field(key) /* */ |
| 70 | Symbol key; /* */ |
| 71 | { StringBuffer *sb = sbopen(); /* */ |
| 72 | sbputs((char*)SymbolValue(key), sb); /* */ |
| 73 | key = symbol(lower((String)sbflush(sb))); /* */ |
| 74 | add_word(key, &unique_fields); /* */ |
| 75 | (void)sbclose(sb); /* */ |
| 76 | if (key == sym_sortkey) { need_sort_key = true; }/* */ |
| 77 | } /*------------------------*/ |
| 78 | |
| 79 | /*----------------------------------------------------------------------------- |
| 80 | ** Function: apply_checks() |
| 81 | ** Type: void |
| 82 | ** Purpose: |
| 83 | ** |
| 84 | ** Arguments: |
| 85 | ** db |
| 86 | ** Returns: nothing |
| 87 | **___________________________________________________ */ |
| 88 | void apply_checks(db) /* */ |
| 89 | DB db; /* */ |
| 90 | { /* */ |
| 91 | if (rsc_double_check || unique_fields) /* Maybe look for doubles */ |
| 92 | { db_forall(db, do_checks); } /* or apply unique checks */ |
| 93 | } /*------------------------*/ |
| 94 | |
| 95 | #define equal_records(R1,R2) RecordSortkey(R1) == RecordSortkey(R2) |
| 96 | |
| 97 | /*----------------------------------------------------------------------------- |
| 98 | ** Function*: do_checks() |
| 99 | ** Purpose: Check whether the given record has a double. |
| 100 | ** |
| 101 | ** Arguments: |
| 102 | ** db the database |
| 103 | ** rec the record |
| 104 | ** Returns: |false| |
| 105 | **___________________________________________________ */ |
| 106 | static bool do_checks(db,rec) /* */ |
| 107 | DB db; /* */ |
| 108 | Record rec; /* */ |
| 109 | { register Record prev; /* */ |
| 110 | register Record rec2; /* */ |
| 111 | WordList wl; /* */ |
| 112 | Symbol k1 = *RecordHeap(rec); /* */ |
| 113 | if (k1 == NO_SYMBOL) k1 = sym_empty; /* */ |
| 114 | /* */ |
| 115 | for (rec2 = PrevRecord(rec); /* */ |
| 116 | rec2 != RecordNULL; /* */ |
| 117 | rec2 = prev) /* */ |
| 118 | { Symbol k2 = *RecordHeap(rec2); /* */ |
| 119 | if (k2 == NO_SYMBOL) k2 = sym_empty; /* */ |
| 120 | /* */ |
| 121 | prev = PrevRecord(rec2); /* */ |
| 122 | /* */ |
| 123 | if (rsc_double_check && equal_records(rec,rec2))/* */ |
| 124 | { /* */ |
| 125 | if (!rsc_quiet) /* */ |
| 126 | { ErrPrint("*** BibTool WARNING"); /* */ |
| 127 | err_location(RecordLineno(rec), /* */ |
| 128 | RecordSource(rec), /* */ |
| 129 | ": Possible double entry discovered to");/* */ |
| 130 | err_location(RecordLineno(rec2), /* */ |
| 131 | RecordSource(rec2), NULL); /* */ |
| 132 | ErrPrintF(" `%s'\n", (char*)k2); /* */ |
| 133 | /* */ |
| 134 | DebugPrintF3("***\t%s =?= %s\n", /* */ |
| 135 | (char*)SymbolValue(k2), /* */ |
| 136 | (char*)SymbolValue(k1)); /* */ |
| 137 | DebugPrintF2("***\tsort key: %s\n", /* */ |
| 138 | (char*)SymbolValue(RecordSortkey(rec))); /* */ |
| 139 | } /* */ |
| 140 | if (rsc_del_dbl) /* */ |
| 141 | { delete_record(db,rec); } /* */ |
| 142 | else /* */ |
| 143 | { SetRecordDELETED(rec); } /* */ |
| 144 | } /* */ |
| 145 | /* */ |
| 146 | for (wl = unique_fields; wl; wl = NextWord(wl))/* */ |
| 147 | { String key = ThisWord(wl); /* */ |
| 148 | Symbol val; /* */ |
| 149 | switch (*(char*)key) /* */ |
| 150 | { case '$': /* */ |
| 151 | if (key == sym_key) /* */ |
| 152 | { if (k1 == NO_SYMBOL || k1 != k2) /* */ |
| 153 | { continue; } /* */ |
| 154 | val = k1; /* */ |
| 155 | break; /* */ |
| 156 | } else if (key == sym_sortkey) /* */ |
| 157 | { val = RecordSortkey(rec); /* */ |
| 158 | if (val == NO_SYMBOL || /* */ |
| 159 | val != RecordSortkey(rec2)) /* */ |
| 160 | { continue; } /* */ |
| 161 | break; /* */ |
| 162 | } /* */ |
| 163 | default: /* */ |
| 164 | val = record_get(rec, ThisWord(wl)); /* */ |
| 165 | if (val == NO_SYMBOL || /* */ |
| 166 | val != record_get(rec2, ThisWord(wl)))/* */ |
| 167 | { continue; } /* */ |
| 168 | } /* */ |
| 169 | /* */ |
| 170 | ErrPrint("*** BibTool WARNING"); /* */ |
| 171 | err_location(RecordLineno(rec2), /* */ |
| 172 | RecordSource(rec2), " and"); /* */ |
| 173 | err_location(RecordLineno(rec), /* */ |
| 174 | RecordSource(rec), NULL); /* */ |
| 175 | ErrPrintF2(": field `%s' is not unique: %s\n",/* */ |
| 176 | (char*)ThisWord(wl), (char*)val); /* */ |
| 177 | } /* */ |
| 178 | } /* */ |
| 179 | /* */ |
| 180 | return false; /* */ |
| 181 | } /*------------------------*/ |
| 182 |