| 1 |
/* |
| 2 |
* |
| 3 |
* This file is part of |
| 4 |
* MakeIndex - A formatter and format independent index processor |
| 5 |
* |
| 6 |
* Copyright (C) 1998-2012 by the TeX Live project. |
| 7 |
* Copyright (C) 1989 by Chen & Harrison International Systems, Inc. |
| 8 |
* Copyright (C) 1988 by Olivetti Research Center |
| 9 |
* Copyright (C) 1987 by Regents of the University of California |
| 10 |
* |
| 11 |
* Author: |
| 12 |
* Pehong Chen |
| 13 |
* Chen & Harrison International Systems, Inc. |
| 14 |
* Palo Alto, California |
| 15 |
* USA |
| 16 |
* |
| 17 |
* Contributors: |
| 18 |
* Please refer to the CONTRIB file that comes with this release |
| 19 |
* for a list of people who have contributed to this and/or previous |
| 20 |
* release(s) of MakeIndex. |
| 21 |
* |
| 22 |
* All rights reserved by the copyright holders. See the copyright |
| 23 |
* notice distributed with this software for a complete description of |
| 24 |
* the conditions under which it is made available. |
| 25 |
* |
| 26 |
*/ |
| 27 |
|
| 28 |
#include "mkind.h" |
| 29 |
#include "qsort.h" |
| 30 |
|
| 31 |
#ifdef HAVE_LOCALE_H |
| 32 |
#include <locale.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
static long idx_gc; |
| 36 |
|
| 37 |
static int check_mixsym (const char *x, const char *y); |
| 38 |
static int compare (const void *va, const void *vb); |
| 39 |
static int compare_one (const char *x, const char *y); |
| 40 |
static int compare_page (const FIELD_PTR *a, const FIELD_PTR *b); |
| 41 |
static int compare_string (const unsigned char *a, const unsigned char *b); |
| 42 |
static int new_strcmp (const unsigned char *a, const unsigned char *b, |
| 43 |
int option); |
| 44 |
|
| 45 |
void |
| 46 |
sort_idx(void) |
| 47 |
{ |
| 48 |
#ifdef HAVE_SETLOCALE |
| 49 |
char *prev_locale; |
| 50 |
#endif |
| 51 |
|
| 52 |
MESSAGE("Sorting entries..."); |
| 53 |
#ifdef HAVE_SETLOCALE |
| 54 |
prev_locale = setlocale(LC_COLLATE, NULL); |
| 55 |
setlocale(LC_COLLATE, ""); |
| 56 |
#endif |
| 57 |
idx_dc = 0; |
| 58 |
idx_gc = 0L; |
| 59 |
qqsort(idx_key, (size_t)idx_gt, sizeof(FIELD_PTR), compare); |
| 60 |
#ifdef HAVE_SETLOCALE |
| 61 |
setlocale(LC_COLLATE, prev_locale); |
| 62 |
#endif |
| 63 |
MESSAGE1("done (%ld comparisons).\n", idx_gc); |
| 64 |
} |
| 65 |
|
| 66 |
static int |
| 67 |
compare(const void *va, const void *vb) |
| 68 |
{ |
| 69 |
const FIELD_PTR *a = va; |
| 70 |
const FIELD_PTR *b = vb; |
| 71 |
int i; |
| 72 |
int dif; |
| 73 |
|
| 74 |
idx_gc++; |
| 75 |
IDX_DOT(CMP_MAX); |
| 76 |
|
| 77 |
for (i = 0; i < FIELD_MAX; i++) { |
| 78 |
/* compare the sort fields */ |
| 79 |
if ((dif = compare_one((*a)->sf[i], (*b)->sf[i])) != 0) |
| 80 |
break; |
| 81 |
|
| 82 |
/* compare the actual fields */ |
| 83 |
if ((dif = compare_one((*a)->af[i], (*b)->af[i])) != 0) |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
/* both key aggregates are identical, compare page numbers */ |
| 88 |
if (i == FIELD_MAX) |
| 89 |
dif = compare_page(a, b); |
| 90 |
return (dif); |
| 91 |
} |
| 92 |
|
| 93 |
static int |
| 94 |
compare_one(const char *x, const char *y) |
| 95 |
{ |
| 96 |
int m; |
| 97 |
int n; |
| 98 |
|
| 99 |
if ((x[0] == NUL) && (y[0] == NUL)) |
| 100 |
return (0); |
| 101 |
|
| 102 |
if (x[0] == NUL) |
| 103 |
return (-1); |
| 104 |
|
| 105 |
if (y[0] == NUL) |
| 106 |
return (1); |
| 107 |
|
| 108 |
m = group_type(x); |
| 109 |
n = group_type(y); |
| 110 |
|
| 111 |
/* both pure digits */ |
| 112 |
if ((m >= 0) && (n >= 0)) |
| 113 |
return (m - n); |
| 114 |
|
| 115 |
/* x digit, y non-digit */ |
| 116 |
if (m >= 0) { |
| 117 |
if (german_sort) |
| 118 |
return (1); |
| 119 |
else |
| 120 |
return ((n == -1) ? 1 : -1); |
| 121 |
} |
| 122 |
/* x non-digit, y digit */ |
| 123 |
if (n >= 0) { |
| 124 |
if (german_sort) |
| 125 |
return (-1); |
| 126 |
else |
| 127 |
return ((m == -1) ? -1 : 1); |
| 128 |
} |
| 129 |
/* strings started with a symbol (including digit) */ |
| 130 |
if ((m == SYMBOL) && (n == SYMBOL)) |
| 131 |
return (check_mixsym(x, y)); |
| 132 |
|
| 133 |
/* x symbol, y non-symbol */ |
| 134 |
if (m == SYMBOL) |
| 135 |
return (-1); |
| 136 |
|
| 137 |
/* x non-symbol, y symbol */ |
| 138 |
if (n == SYMBOL) |
| 139 |
return (1); |
| 140 |
|
| 141 |
/* strings with a leading letter, the ALPHA type */ |
| 142 |
return (compare_string((const unsigned char*)x, (const unsigned char*)y)); |
| 143 |
} |
| 144 |
|
| 145 |
static int |
| 146 |
check_mixsym(const char *x, const char *y) |
| 147 |
{ |
| 148 |
int m; |
| 149 |
int n; |
| 150 |
|
| 151 |
m = ISDIGIT(x[0]); |
| 152 |
n = ISDIGIT(y[0]); |
| 153 |
|
| 154 |
if (m && !n) |
| 155 |
return (1); |
| 156 |
|
| 157 |
if (!m && n) |
| 158 |
return (-1); |
| 159 |
|
| 160 |
return (locale_sort ? strcoll(x, y) : strcmp(x, y)); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
static int |
| 165 |
compare_string(const unsigned char *a, const unsigned char *b) |
| 166 |
{ |
| 167 |
int i = 0; |
| 168 |
int j = 0; |
| 169 |
int al; |
| 170 |
int bl; |
| 171 |
|
| 172 |
if (locale_sort) return strcoll((const char *)a, (const char *)b); |
| 173 |
|
| 174 |
while ((a[i] != NUL) || (b[j] != NUL)) { |
| 175 |
if (a[i] == NUL) |
| 176 |
return (-1); |
| 177 |
if (b[j] == NUL) |
| 178 |
return (1); |
| 179 |
if (letter_ordering) { |
| 180 |
if (a[i] == SPC) |
| 181 |
i++; |
| 182 |
if (b[j] == SPC) |
| 183 |
j++; |
| 184 |
} |
| 185 |
al = TOLOWER(a[i]); |
| 186 |
bl = TOLOWER(b[j]); |
| 187 |
|
| 188 |
if (al != bl) |
| 189 |
return (al - bl); |
| 190 |
i++; |
| 191 |
j++; |
| 192 |
} |
| 193 |
if (german_sort) |
| 194 |
return (new_strcmp(a, b, GERMAN)); |
| 195 |
else |
| 196 |
return (strcmp((const char*)a, (const char*)b)); |
| 197 |
} |
| 198 |
|
| 199 |
static int |
| 200 |
compare_page(const FIELD_PTR *a, const FIELD_PTR *b) |
| 201 |
{ |
| 202 |
int m = 0; |
| 203 |
short i = 0; |
| 204 |
|
| 205 |
while ((i < (*a)->count) && (i < (*b)->count) && |
| 206 |
((m = (*a)->npg[i] - (*b)->npg[i]) == 0)) |
| 207 |
{ |
| 208 |
i++; |
| 209 |
} |
| 210 |
if (m == 0) |
| 211 |
{ /* common leading page numbers match */ |
| 212 |
if ((i == (*a)->count) && (i == (*b)->count)) |
| 213 |
{ /* all page numbers match */ |
| 214 |
/*********************************************************** |
| 215 |
We have identical entries, except possibly in encap fields. |
| 216 |
The ordering is tricky here. Consider the following input |
| 217 |
sequence of index names, encaps, and page numbers: |
| 218 |
|
| 219 |
foo|( 2 |
| 220 |
foo|) 6 |
| 221 |
foo|( 6 |
| 222 |
foo|) 10 |
| 223 |
|
| 224 |
This might legimately occur when a page range ends, and |
| 225 |
subsequently, a new range starts, on the same page. If we |
| 226 |
just order by range_open and range_close (here, parens), |
| 227 |
then we will produce |
| 228 |
|
| 229 |
foo|( 2 |
| 230 |
foo|( 6 |
| 231 |
foo|) 6 |
| 232 |
foo|) 10 |
| 233 |
|
| 234 |
This will later generate the index entry |
| 235 |
|
| 236 |
foo, 2--6, \({6}, 10 |
| 237 |
|
| 238 |
which is not only wrong, but has also introduced an illegal |
| 239 |
LaTeX macro, \({6}, because the merging step treated this |
| 240 |
like a \see{6} entry. |
| 241 |
|
| 242 |
The solution is to preserve the original input order, which |
| 243 |
we can do by treating range_open and range_close as equal, |
| 244 |
and then ordering by input line number. This will then |
| 245 |
generate the correct index entry |
| 246 |
|
| 247 |
foo, 2--10 |
| 248 |
|
| 249 |
Ordering inconsistencies from missing range open or close |
| 250 |
entries, or mixing roman and arabic page numbers, will be |
| 251 |
detected later. |
| 252 |
***********************************************************/ |
| 253 |
|
| 254 |
#define isrange(c) ( ((c) == idx_ropen) || ((c) == idx_rclose) ) |
| 255 |
|
| 256 |
/* Order two range values by input line number */ |
| 257 |
|
| 258 |
if (isrange(*(*a)->encap) && isrange(*(*b)->encap)) |
| 259 |
m = (*a)->lc - (*b)->lc; |
| 260 |
|
| 261 |
/* Handle identical encap fields; neither is a range delimiter */ |
| 262 |
|
| 263 |
else if (STREQ((*a)->encap, (*b)->encap)) |
| 264 |
{ |
| 265 |
/* If neither are yet marked duplicate, mark the second |
| 266 |
of them to be ignored. */ |
| 267 |
if (((*a)->type != DUPLICATE) && |
| 268 |
((*b)->type != DUPLICATE)) |
| 269 |
(*b)->type = DUPLICATE; |
| 270 |
/* leave m == 0 to show equality */ |
| 271 |
} |
| 272 |
|
| 273 |
/* Encap fields differ: only one may be a range delimiter, */ |
| 274 |
/* or else neither of them is. If either of them is a range */ |
| 275 |
/* delimiter, order by input line number; otherwise, order */ |
| 276 |
/* by name. */ |
| 277 |
|
| 278 |
else |
| 279 |
{ |
| 280 |
if ( isrange(*(*a)->encap) || isrange(*(*b)->encap) ) |
| 281 |
m = (*a)->lc - (*b)->lc; /* order by input line number */ |
| 282 |
else /* order non-range items by */ |
| 283 |
/* their encap strings */ |
| 284 |
m = compare_string((const unsigned char*)((*a)->encap), |
| 285 |
(const unsigned char*)((*b)->encap)); |
| 286 |
} |
| 287 |
} |
| 288 |
else if ((i == (*a)->count) && (i < (*b)->count)) |
| 289 |
m = -1; |
| 290 |
else if ((i < (*a)->count) && (i == (*b)->count)) |
| 291 |
m = 1; |
| 292 |
} |
| 293 |
return (m); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
static int |
| 298 |
new_strcmp(const unsigned char *s1, const unsigned char *s2, int option) |
| 299 |
{ |
| 300 |
int i; |
| 301 |
|
| 302 |
i = 0; |
| 303 |
while (s1[i] == s2[i]) |
| 304 |
if (s1[i++] == NUL) |
| 305 |
return (0); |
| 306 |
if (option) /* ASCII */ |
| 307 |
return (isupper(s1[i]) ? -1 : 1); |
| 308 |
else /* GERMAN */ |
| 309 |
return (isupper(s1[i]) ? 1 : -1); |
| 310 |
} |