fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

numericsort.c
Go to the documentation of this file.
1 /*
2  * "$Id$"
3  *
4  * Numeric sorting routine for the Fast Light Tool Kit (FLTK).
5  *
6  * Copyright 1998-2016 by Bill Spitzak and others.
7  *
8  * This library is free software. Distribution and use rights are outlined in
9  * the file "COPYING" which should have been included with this file. If this
10  * file is missing or damaged, see the license at:
11  *
12  * http://www.fltk.org/COPYING.php
13  *
14  * Please report all bugs and problems on the following page:
15  *
16  * http://www.fltk.org/str.php
17  */
18 
19 /* My own scandir sorting function, useful for the film industry where
20  we have many files with numbers in their names: */
21 
22 #include <config.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26 
27 #if !defined(WIN32) || defined(__CYGWIN__)
28 # ifdef HAVE_DIRENT_H
29 # include <dirent.h>
30 # else
31 # define dirent direct
32 # ifdef HAVE_SYS_NDIR_H
33 # include <sys/ndir.h>
34 # endif /* HAVE_SYS_NDIR_H */
35 # ifdef HAVE_SYS_DIR_H
36 # include <sys/dir.h>
37 # endif /* HAVE_SYS_DIR_H */
38 # ifdef HAVE_NDIR_H
39 # include <ndir.h>
40 # endif /* HAVE_NDIR_H */
41 # endif /* HAVE_DIRENT_H */
42 #endif /* !WIN32 || __CYGWIN__ */
43 
44 #include <FL/filename.H>
45 
46 /*
47  * 'numericsort()' - Compare two directory entries, possibly with
48  * a case-insensitive comparison...
49  */
50 
51 static int numericsort(struct dirent **A, struct dirent **B, int cs) {
52  const char* a = (*A)->d_name;
53  const char* b = (*B)->d_name;
54  int ret = 0;
55  for (;;) {
56  if (isdigit(*a & 255) && isdigit(*b & 255)) {
57  int diff,magdiff;
58  while (*a == '0') a++;
59  while (*b == '0') b++;
60  while (isdigit(*a & 255) && *a == *b) {a++; b++;}
61  diff = (isdigit(*a & 255) && isdigit(*b & 255)) ? *a - *b : 0;
62  magdiff = 0;
63  while (isdigit(*a & 255)) {magdiff++; a++;}
64  while (isdigit(*b & 255)) {magdiff--; b++;}
65  if (magdiff) {ret = magdiff; break;} /* compare # of significant digits*/
66  if (diff) {ret = diff; break;} /* compare first non-zero digit */
67  } else {
68  if (cs) {
69  /* compare case-sensitive */
70  if ((ret = *a-*b)) break;
71  } else {
72  /* compare case-insensitve */
73  if ((ret = tolower(*a & 255)-tolower(*b & 255))) break;
74  }
75 
76  if (!*a) break;
77  a++; b++;
78  }
79  }
80  if (!ret) return 0;
81  else return (ret < 0) ? -1 : 1;
82 }
83 
84 /*
85  * 'fl_casenumericsort()' - Compare directory entries with case-sensitivity.
86  */
87 
88 int fl_casenumericsort(struct dirent **A, struct dirent **B) {
89  return numericsort(A, B, 0);
90 }
91 
92 /*
93  * 'fl_numericsort()' - Compare directory entries with case-sensitivity.
94  */
95 
96 int fl_numericsort(struct dirent **A, struct dirent **B) {
97  return numericsort(A, B, 1);
98 }
99 
100 /*
101  * End of "$Id$".
102  */
numericsort
static int numericsort(struct direct **A, struct direct **B, int cs)
Definition: numericsort.c:51
fl_casenumericsort
int fl_casenumericsort(struct direct **A, struct direct **B)
Definition: numericsort.c:88
filename.H
b
long b
Definition: jpegint.h:397
dirent.h
fl_numericsort
int fl_numericsort(struct direct **A, struct direct **B)
Definition: numericsort.c:96
dirent
#define dirent
Definition: numericsort.c:31