1998-10-19 20:46:58 +00:00
|
|
|
/*
|
2005-02-24 21:55:12 +00:00
|
|
|
* "$Id$"
|
1998-10-19 20:46:58 +00:00
|
|
|
*
|
|
|
|
|
* Numeric sorting routine for the Fast Light Tool Kit (FLTK).
|
|
|
|
|
*
|
2016-01-31 04:33:54 +00:00
|
|
|
* Copyright 1998-2016 by Bill Spitzak and others.
|
1998-10-19 20:46:58 +00:00
|
|
|
*
|
2011-07-19 04:49:30 +00:00
|
|
|
* This library is free software. Distribution and use rights are outlined in
|
|
|
|
|
* the file "COPYING" which should have been included with this file. If this
|
|
|
|
|
* file is missing or damaged, see the license at:
|
1998-10-19 20:46:58 +00:00
|
|
|
*
|
2011-07-19 04:49:30 +00:00
|
|
|
* http://www.fltk.org/COPYING.php
|
1998-10-19 20:46:58 +00:00
|
|
|
*
|
2005-04-16 00:13:17 +00:00
|
|
|
* Please report all bugs and problems on the following page:
|
|
|
|
|
*
|
|
|
|
|
* http://www.fltk.org/str.php
|
1998-10-19 20:46:58 +00:00
|
|
|
*/
|
|
|
|
|
|
1998-10-06 19:14:55 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <stdlib.h>
|
2016-04-07 15:10:30 +00:00
|
|
|
#include <FL/platform_types.h>
|
2016-04-09 10:15:42 +00:00
|
|
|
#include <FL/filename.H>
|
2014-08-21 12:29:48 +00:00
|
|
|
|
2002-05-02 11:11:01 +00:00
|
|
|
/*
|
|
|
|
|
* 'numericsort()' - Compare two directory entries, possibly with
|
|
|
|
|
* a case-insensitive comparison...
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int numericsort(struct dirent **A, struct dirent **B, int cs) {
|
1998-10-06 19:14:55 +00:00
|
|
|
const char* a = (*A)->d_name;
|
|
|
|
|
const char* b = (*B)->d_name;
|
1998-12-02 15:47:30 +00:00
|
|
|
int ret = 0;
|
1998-10-06 19:14:55 +00:00
|
|
|
for (;;) {
|
2002-08-09 03:17:30 +00:00
|
|
|
if (isdigit(*a & 255) && isdigit(*b & 255)) {
|
1998-12-02 15:47:30 +00:00
|
|
|
int diff,magdiff;
|
|
|
|
|
while (*a == '0') a++;
|
|
|
|
|
while (*b == '0') b++;
|
2002-08-09 03:17:30 +00:00
|
|
|
while (isdigit(*a & 255) && *a == *b) {a++; b++;}
|
|
|
|
|
diff = (isdigit(*a & 255) && isdigit(*b & 255)) ? *a - *b : 0;
|
1998-12-02 15:47:30 +00:00
|
|
|
magdiff = 0;
|
2002-08-09 03:17:30 +00:00
|
|
|
while (isdigit(*a & 255)) {magdiff++; a++;}
|
|
|
|
|
while (isdigit(*b & 255)) {magdiff--; b++;}
|
1998-12-02 15:47:30 +00:00
|
|
|
if (magdiff) {ret = magdiff; break;} /* compare # of significant digits*/
|
|
|
|
|
if (diff) {ret = diff; break;} /* compare first non-zero digit */
|
|
|
|
|
} else {
|
2002-05-02 11:11:01 +00:00
|
|
|
if (cs) {
|
|
|
|
|
/* compare case-sensitive */
|
|
|
|
|
if ((ret = *a-*b)) break;
|
|
|
|
|
} else {
|
|
|
|
|
/* compare case-insensitve */
|
2002-08-09 03:17:30 +00:00
|
|
|
if ((ret = tolower(*a & 255)-tolower(*b & 255))) break;
|
2002-05-02 11:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
1998-12-02 15:47:30 +00:00
|
|
|
if (!*a) break;
|
|
|
|
|
a++; b++;
|
1998-11-09 16:25:59 +00:00
|
|
|
}
|
1998-10-06 19:14:55 +00:00
|
|
|
}
|
1998-12-02 15:47:30 +00:00
|
|
|
if (!ret) return 0;
|
|
|
|
|
else return (ret < 0) ? -1 : 1;
|
1998-10-06 19:14:55 +00:00
|
|
|
}
|
1998-10-19 20:46:58 +00:00
|
|
|
|
1998-10-20 23:58:32 +00:00
|
|
|
/*
|
2002-05-02 11:11:01 +00:00
|
|
|
* 'fl_casenumericsort()' - Compare directory entries with case-sensitivity.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
int fl_casenumericsort(struct dirent **A, struct dirent **B) {
|
|
|
|
|
return numericsort(A, B, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 'fl_numericsort()' - Compare directory entries with case-sensitivity.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
int fl_numericsort(struct dirent **A, struct dirent **B) {
|
|
|
|
|
return numericsort(A, B, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2005-02-24 21:55:12 +00:00
|
|
|
* End of "$Id$".
|
1998-10-20 23:58:32 +00:00
|
|
|
*/
|