2 * "$Id: numericsort.c 8074 2010-12-20 13:45:26Z ianmacarthur $"
4 * Numeric sorting routine for the Fast Light Tool Kit (FLTK).
6 * Copyright 1998-2010 by Bill Spitzak and others.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * Please report all bugs and problems on the following page:
25 * http://www.fltk.org/str.php
28 /* My own scandir sorting function, useful for the film industry where
29 we have many files with numbers in their names: */
34 #include <sys/types.h>
36 #if !defined(WIN32) || defined(__CYGWIN__)
40 # define dirent direct
42 # include <sys/ndir.h>
43 # endif /* HAVE_SYS_NDIR_H */
46 # endif /* HAVE_SYS_DIR_H */
49 # endif /* HAVE_NDIR_H */
50 # endif /* HAVE_DIRENT_H */
51 #else /* For WIN32 variants */
52 # include <FL/filename.H>
53 #endif /* !WIN32 || __CYGWIN__ */
56 * 'numericsort()' - Compare two directory entries, possibly with
57 * a case-insensitive comparison...
60 static int numericsort(struct dirent
**A
, struct dirent
**B
, int cs
) {
61 const char* a
= (*A
)->d_name
;
62 const char* b
= (*B
)->d_name
;
65 if (isdigit(*a
& 255) && isdigit(*b
& 255)) {
67 while (*a
== '0') a
++;
68 while (*b
== '0') b
++;
69 while (isdigit(*a
& 255) && *a
== *b
) {a
++; b
++;}
70 diff
= (isdigit(*a
& 255) && isdigit(*b
& 255)) ? *a
- *b
: 0;
72 while (isdigit(*a
& 255)) {magdiff
++; a
++;}
73 while (isdigit(*b
& 255)) {magdiff
--; b
++;}
74 if (magdiff
) {ret
= magdiff
; break;} /* compare # of significant digits*/
75 if (diff
) {ret
= diff
; break;} /* compare first non-zero digit */
78 /* compare case-sensitive */
79 if ((ret
= *a
-*b
)) break;
81 /* compare case-insensitve */
82 if ((ret
= tolower(*a
& 255)-tolower(*b
& 255))) break;
90 else return (ret
< 0) ? -1 : 1;
94 * 'fl_casenumericsort()' - Compare directory entries with case-sensitivity.
97 int fl_casenumericsort(struct dirent
**A
, struct dirent
**B
) {
98 return numericsort(A
, B
, 0);
102 * 'fl_numericsort()' - Compare directory entries with case-sensitivity.
105 int fl_numericsort(struct dirent
**A
, struct dirent
**B
) {
106 return numericsort(A
, B
, 1);
110 * End of "$Id: numericsort.c 8074 2010-12-20 13:45:26Z ianmacarthur $".