[a20d9b9] | 1 | /* namecmp.c */ |
---|
| 2 | /* Ordering function for station names */ |
---|
[0580c6a] | 3 | /* Copyright (C) 1991-2002,2004 Olly Betts |
---|
[a20d9b9] | 4 | * |
---|
| 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
| 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
| 14 | * |
---|
| 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
[ecbc6c18] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[a20d9b9] | 18 | */ |
---|
| 19 | |
---|
| 20 | #ifdef HAVE_CONFIG_H |
---|
| 21 | # include <config.h> |
---|
| 22 | #endif |
---|
| 23 | |
---|
| 24 | #include <string.h> |
---|
| 25 | #include <ctype.h> |
---|
| 26 | |
---|
[156dc16] | 27 | #include "namecmp.h" |
---|
| 28 | |
---|
[a20d9b9] | 29 | int |
---|
[ee1ec59] | 30 | name_cmp(const char *a, const char *b, int separator) |
---|
[a20d9b9] | 31 | { |
---|
[e35e7b3] | 32 | while (1) { |
---|
[0580c6a] | 33 | int cha = (unsigned char)*a, chb = (unsigned char)*b; |
---|
[e35e7b3] | 34 | |
---|
| 35 | /* done if end of either first string */ |
---|
| 36 | if (!cha || !chb) return cha - chb; |
---|
| 37 | |
---|
| 38 | /* check for end of non-numeric prefix */ |
---|
| 39 | if (isdigit(cha)) { |
---|
| 40 | /* sort numbers numerically and before non-numbers */ |
---|
| 41 | const char *sa, *sb, *ea, *eb; |
---|
| 42 | int res; |
---|
| 43 | |
---|
[ee1ec59] | 44 | if (!isdigit(chb)) return chb == separator ? 1 : -1; |
---|
[e35e7b3] | 45 | |
---|
| 46 | sa = a; |
---|
| 47 | while (*sa == '0') sa++; |
---|
| 48 | ea = sa; |
---|
[abd126e] | 49 | while (isdigit((unsigned char)*ea)) ea++; |
---|
[e35e7b3] | 50 | |
---|
| 51 | sb = b; |
---|
| 52 | while (*sb == '0') sb++; |
---|
| 53 | eb = sb; |
---|
[abd126e] | 54 | while (isdigit((unsigned char)*eb)) eb++; |
---|
[e35e7b3] | 55 | |
---|
| 56 | /* shorter sorts first */ |
---|
| 57 | res = (ea - sa) - (eb - sb); |
---|
| 58 | /* same length, all digits, so memcmp() sorts numerically */ |
---|
| 59 | if (!res) res = memcmp(sa, sb, ea - sa); |
---|
| 60 | /* more leading zeros sorts first */ |
---|
| 61 | if (!res) res = (sb - b) - (sa - a); |
---|
[4bddffe] | 62 | if (res) return res; |
---|
[e35e7b3] | 63 | |
---|
| 64 | /* if numbers match, sort by suffix */ |
---|
| 65 | a = ea; |
---|
| 66 | b = eb; |
---|
| 67 | continue; |
---|
[a20d9b9] | 68 | } |
---|
| 69 | |
---|
[e35e7b3] | 70 | if (cha != chb) { |
---|
[ee1ec59] | 71 | if (cha == separator) return -1; |
---|
| 72 | if (isdigit(chb) || chb == separator) return 1; |
---|
[e35e7b3] | 73 | return cha - chb; |
---|
| 74 | } |
---|
[4bddffe] | 75 | |
---|
[a20d9b9] | 76 | a++; |
---|
| 77 | b++; |
---|
| 78 | } |
---|
| 79 | } |
---|