modified: src1/input.c
[GalaxyCodeBases.git] / c_cpp / lib / natsort / textutils.diff
blobd342707e4d3830024f38d142174d0d6c1fc39dd0
1 --- textutils-2.0/lib/Makefile.am Sun Jul 4 06:16:53 1999
2 +++ textutils/lib/Makefile.am Wed Mar 8 18:44:08 2000
3 @@ -10,7 +10,8 @@
4 getopt1.c hard-locale.c human.c \
5 linebuffer.c long-options.c md5.c memcasecmp.c memcoll.c \
6 obstack.c quotearg.c readtokens.c safe-read.c \
7 - version-etc.c xmalloc.c xstrdup.c xstrtod.c xstrtol.c xstrtoul.c xstrtoumax.c
8 + version-etc.c xmalloc.c xstrdup.c xstrtod.c xstrtol.c xstrtoul.c xstrtoumax.c \
9 + strnatcmp.c
11 libtu_a_LIBADD = @LIBOBJS@ @ALLOCA@
12 libtu_a_DEPENDENCIES = $(libtu_a_LIBADD)
13 @@ -21,7 +22,8 @@
14 lchown.h linebuffer.h long-options.h md5.h \
15 memcasecmp.h memcoll.h obstack.h quotearg.h \
16 readtokens.h regex.h safe-read.h \
17 - version-etc.h xalloc.h xstrtod.h xstrtol.h
18 + version-etc.h xalloc.h xstrtod.h xstrtol.h \
19 + strnatcmp.h
21 BUILT_SOURCES = lstat.c stat.c
23 --- textutils-2.0/src/sort.c Thu Aug 5 03:42:49 1999
24 +++ textutils/src/sort.c Fri Mar 10 15:00:35 2000
25 @@ -1,5 +1,5 @@
26 /* sort - sort lines of text (with all kinds of options).
27 - Copyright (C) 88, 1991-1999 Free Software Foundation, Inc.
28 + Copyright (C) 1988, 1991-2000 Free Software Foundation, Inc.
30 This program is free software; you can redistribute it and/or modify
31 it under the terms of the GNU General Public License as published by
32 @@ -19,7 +19,9 @@
33 The author may be reached (Email) at the address mike@gnu.ai.mit.edu,
34 or (US mail) as Mike Haertel c/o Free Software Foundation.
36 - Ørn E. Hansen added NLS support in 1997. */
37 + Ørn E. Hansen added NLS support in 1997.
39 + Martin Pool <mbp@humbug.org.au> added natural order sorting in 2000. */
41 #include <config.h>
43 @@ -33,17 +35,18 @@
44 #include "hard-locale.h"
45 #include "memcoll.h"
46 #include "xalloc.h"
47 +#include "strnatcmp.h"
49 /* The official name of this program (e.g., no `g' prefix). */
50 #define PROGRAM_NAME "sort"
52 #define AUTHORS "Mike Haertel"
54 -#if defined ENABLE_NLS && HAVE_LANGINFO_H
55 +#if defined(ENABLE_NLS) && HAVE_LANGINFO_H
56 # include <langinfo.h>
57 #endif
59 -#if HAVE_PATHCONF && defined _PC_NAME_MAX
60 +#if HAVE_PATHCONF && defined(_PC_NAME_MAX)
61 # define NAME_MAX_IN_DIR(Dir) pathconf (Dir, _PC_NAME_MAX)
62 #else
63 # define NAME_MAX_IN_DIR(Dir) 255
64 @@ -150,6 +153,8 @@
65 Handle numbers in exponential notation. */
66 int month; /* Flag for comparison by month name. */
67 int reverse; /* Reverse the sense of comparison. */
68 + int natural; /* Natural sort that handles strings
69 + containing integer substrings. */
70 struct keyfield *next; /* Next keyfield to try. */
73 @@ -177,7 +182,7 @@
75 #define MONTHS_PER_YEAR 12
77 -#if defined ENABLE_NLS && HAVE_NL_LANGINFO
78 +#if defined(ENABLE_NLS) && HAVE_NL_LANGINFO
79 # define MONTHTAB_CONST /* empty */
80 #else
81 # define MONTHTAB_CONST const
82 @@ -274,6 +279,7 @@
83 -m merge already sorted files, do not sort\n\
84 -M compare (unknown) < `JAN' < ... < `DEC', imply -b\n\
85 -n compare according to string numerical value, imply -b\n\
86 + -N natural sort order for numbers in strings\n\
87 -o FILE write result on FILE instead of standard output\n\
88 -r reverse the result of comparisons\n\
89 -s stabilize sort by disabling last resort comparison\n\
90 @@ -290,8 +296,8 @@
91 printf (_("\
92 POS is F[.C][OPTS], where F is the field number and C the character position\n\
93 in the field, both counted from one with -k, from zero with the obsolescent\n\
94 -form. OPTS is made up of one or more of Mbdfinr; this effectively disables\n\
95 -global -Mbdfinr settings for that key. If no key is given, use the entire\n\
96 +form. OPTS is made up of one or more of MbdfinrN; this effectively disables\n\
97 +global -MbdfinrN settings for that key. If no key is given, use the entire\n\
98 line as the key. With no FILE, or when FILE is -, read standard input.\n\
101 @@ -497,7 +503,7 @@
102 fold_toupper[i] = i;
105 -#if defined ENABLE_NLS && HAVE_NL_LANGINFO
106 +#if defined(ENABLE_NLS) && HAVE_NL_LANGINFO
107 /* If we're not in the "C" locale, read different names for months. */
108 if (hard_LC_TIME)
110 @@ -1039,6 +1045,44 @@
111 : memcmp ((char *) &a, (char *) &b, sizeof a));
115 +static int
116 +natural_compare (const char *texta, int lena, const char *textb, int lenb,
117 + int const *ignore, char const *translate)
119 + if (ignore || translate)
121 + char *copy_a = (char *) alloca (lena + 1);
122 + char *copy_b = (char *) alloca (lenb + 1);
123 + int new_len_a, new_len_b, i;
125 + /* Ignore and/or translate chars before comparing. */
126 + for (new_len_a = new_len_b = i = 0; i < max (lena, lenb); i++)
128 + if (i < lena)
130 + copy_a[new_len_a] = (translate
131 + ? translate[UCHAR (texta[i])]
132 + : texta[i]);
133 + if (!ignore || !ignore[UCHAR (texta[i])])
134 + ++new_len_a;
136 + if (i < lenb)
138 + copy_b[new_len_b] = (translate
139 + ? translate[UCHAR (textb[i])]
140 + : textb [i]);
141 + if (!ignore || !ignore[UCHAR (textb[i])])
142 + ++new_len_b;
146 + return memcoll (copy_a, new_len_a, copy_b, new_len_b);
148 + else
149 + return strnatcmp (texta, textb);
152 /* Return an integer in 1..12 of the month name S with length LEN.
153 Return 0 if the name in S is not recognized. */
155 @@ -1178,6 +1222,16 @@
156 return key->reverse ? -diff : diff;
157 continue;
159 + else if (key->natural)
161 + diff = natural_compare (texta, lena, textb, lenb, ignore, translate);
162 + if (diff)
163 + return key->reverse ? -diff : diff;
165 + /* if this didn't match, then we continue on to try other
166 + fields. */
167 + continue;
169 #ifdef ENABLE_NLS
171 /* Sorting like this may become slow, so in a simple locale the user
172 @@ -1872,6 +1926,9 @@
173 case 'n':
174 key->numeric = 1;
175 break;
176 + case 'N':
177 + key->natural = 1;
178 + break;
179 case 'r':
180 key->reverse = 1;
181 break;
182 @@ -1981,6 +2038,7 @@
183 gkey.ignore = NULL;
184 gkey.translate = NULL;
185 gkey.numeric = gkey.general_numeric = gkey.month = gkey.reverse = 0;
186 + gkey.natural = 0;
187 gkey.skipsblanks = gkey.skipeblanks = 0;
189 files = (char **) xmalloc (sizeof (char *) * argc);
190 @@ -2239,7 +2297,7 @@
191 /* Inheritance of global options to individual keys. */
192 for (key = keyhead.next; key; key = key->next)
193 if (!key->ignore && !key->translate && !key->skipsblanks && !key->reverse
194 - && !key->skipeblanks && !key->month && !key->numeric
195 + && !key->skipeblanks && !key->month && !key->numeric && !key->natural
196 && !key->general_numeric)
198 key->ignore = gkey.ignore;
199 @@ -2250,11 +2308,12 @@
200 key->numeric = gkey.numeric;
201 key->general_numeric = gkey.general_numeric;
202 key->reverse = gkey.reverse;
203 + key->natural = gkey.natural;
206 if (!keyhead.next && (gkey.ignore || gkey.translate || gkey.skipsblanks
207 || gkey.skipeblanks || gkey.month || gkey.numeric
208 - || gkey.general_numeric))
209 + || gkey.natural || gkey.general_numeric))
210 insertkey (&gkey);
211 reverse = gkey.reverse;