Tweak themes for more color consistency.
[ntk.git] / src / numericsort.c
blobe780b892a2daa826da5d75b09782b1e4936e0a09
1 /*
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
21 * USA.
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: */
31 #include <config.h>
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
36 #if !defined(WIN32) || defined(__CYGWIN__)
37 # ifdef HAVE_DIRENT_H
38 # include <dirent.h>
39 # else
40 # define dirent direct
41 # if HAVE_SYS_NDIR_H
42 # include <sys/ndir.h>
43 # endif /* HAVE_SYS_NDIR_H */
44 # if HAVE_SYS_DIR_H
45 # include <sys/dir.h>
46 # endif /* HAVE_SYS_DIR_H */
47 # if HAVE_NDIR_H
48 # include <ndir.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;
63 int ret = 0;
64 for (;;) {
65 if (isdigit(*a & 255) && isdigit(*b & 255)) {
66 int diff,magdiff;
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;
71 magdiff = 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 */
76 } else {
77 if (cs) {
78 /* compare case-sensitive */
79 if ((ret = *a-*b)) break;
80 } else {
81 /* compare case-insensitve */
82 if ((ret = tolower(*a & 255)-tolower(*b & 255))) break;
85 if (!*a) break;
86 a++; b++;
89 if (!ret) return 0;
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 $".