source: git/src/namecmp.c

walls-data
Last change on this file was 4c83f84, checked in by Olly Betts <olly@…>, 6 days ago

Don't check HAVE_CONFIG_H in most cases

This check is only useful for img.c, which is intended to be usable
outside of Survex (and had fallbacks for functions which may not be
available which will get used if built in a non-autotools project).
For all the other source files it's just useless boilerplate.

  • Property mode set to 100644
File size: 2.0 KB
Line 
1/* namecmp.c */
2/* Ordering function for station names */
3/* Copyright (C) 1991-2002,2004 Olly Betts
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
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include <config.h>
21
22#include <string.h>
23#include <ctype.h>
24
25#include "namecmp.h"
26
27int
28name_cmp(const char *a, const char *b, int separator)
29{
30   while (1) {
31      int cha = (unsigned char)*a, chb = (unsigned char)*b;
32
33      /* done if end of either first string */
34      if (!cha || !chb) return cha - chb;
35
36      /* check for end of non-numeric prefix */
37      if (isdigit(cha)) {
38         /* sort numbers numerically and before non-numbers */
39         const char *sa, *sb, *ea, *eb;
40         int res;
41
42         if (!isdigit(chb)) return chb == separator ? 1 : -1;
43
44         sa = a;
45         while (*sa == '0') sa++;
46         ea = sa;
47         while (isdigit((unsigned char)*ea)) ea++;
48
49         sb = b;
50         while (*sb == '0') sb++;
51         eb = sb;
52         while (isdigit((unsigned char)*eb)) eb++;
53
54         /* shorter sorts first */
55         res = (ea - sa) - (eb - sb);
56         /* same length, all digits, so memcmp() sorts numerically */
57         if (!res) res = memcmp(sa, sb, ea - sa);
58         /* more leading zeros sorts first */
59         if (!res) res = (sb - b) - (sa - a);
60         if (res) return res;
61
62         /* if numbers match, sort by suffix */
63         a = ea;
64         b = eb;
65         continue;
66      }
67
68      if (cha != chb) {
69         if (cha == separator) return -1;
70         if (isdigit(chb) || chb == separator) return 1;
71         return cha - chb;
72      }
73
74      a++;
75      b++;
76   }
77}
Note: See TracBrowser for help on using the repository browser.