blob: e3455a8cf03faf53a0cf5ce17ec3ccd8b9cbcb21 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
/*
* "$Id$"
*
* Numeric sorting routine for the Fast Light Tool Kit (FLTK).
*
* Copyright 1998-2016 by Bill Spitzak and others.
*
* 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:
*
* http://www.fltk.org/COPYING.php
*
* Please report all bugs and problems on the following page:
*
* http://www.fltk.org/str.php
*/
#include <ctype.h>
#include <stdlib.h>
#include <FL/platform_types.h>
/*
* 'numericsort()' - Compare two directory entries, possibly with
* a case-insensitive comparison...
*/
static int numericsort(struct dirent **A, struct dirent **B, int cs) {
const char* a = (*A)->d_name;
const char* b = (*B)->d_name;
int ret = 0;
for (;;) {
if (isdigit(*a & 255) && isdigit(*b & 255)) {
int diff,magdiff;
while (*a == '0') a++;
while (*b == '0') b++;
while (isdigit(*a & 255) && *a == *b) {a++; b++;}
diff = (isdigit(*a & 255) && isdigit(*b & 255)) ? *a - *b : 0;
magdiff = 0;
while (isdigit(*a & 255)) {magdiff++; a++;}
while (isdigit(*b & 255)) {magdiff--; b++;}
if (magdiff) {ret = magdiff; break;} /* compare # of significant digits*/
if (diff) {ret = diff; break;} /* compare first non-zero digit */
} else {
if (cs) {
/* compare case-sensitive */
if ((ret = *a-*b)) break;
} else {
/* compare case-insensitve */
if ((ret = tolower(*a & 255)-tolower(*b & 255))) break;
}
if (!*a) break;
a++; b++;
}
}
if (!ret) return 0;
else return (ret < 0) ? -1 : 1;
}
/*
* '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);
}
/*
* End of "$Id$".
*/
|