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

numericsort.c

Go to the documentation of this file.
00001 /*
00002  * "$Id: numericsort.c 8074 2010-12-20 13:45:26Z ianmacarthur $"
00003  *
00004  * Numeric sorting routine for the Fast Light Tool Kit (FLTK).
00005  *
00006  * Copyright 1998-2010 by Bill Spitzak and others.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Library General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Library General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Library General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00021  * USA.
00022  *
00023  * Please report all bugs and problems on the following page:
00024  *
00025  *     http://www.fltk.org/str.php
00026  */
00027 
00028 /* My own scandir sorting function, useful for the film industry where
00029    we have many files with numbers in their names: */
00030 
00031 #include <config.h>
00032 #include <ctype.h>
00033 #include <stdlib.h>
00034 #include <sys/types.h>
00035 
00036 #if !defined(WIN32) || defined(__CYGWIN__)
00037 #  ifdef HAVE_DIRENT_H
00038 #    include <dirent.h>
00039 #  else
00040 #    define dirent direct
00041 #    if HAVE_SYS_NDIR_H
00042 #      include <sys/ndir.h>
00043 #    endif /* HAVE_SYS_NDIR_H */
00044 #    if HAVE_SYS_DIR_H
00045 #      include <sys/dir.h>
00046 #    endif /* HAVE_SYS_DIR_H */
00047 #    if HAVE_NDIR_H
00048 #      include <ndir.h>
00049 #    endif /* HAVE_NDIR_H */
00050 #  endif /* HAVE_DIRENT_H */
00051 #else /* For WIN32 variants */
00052 #  include <FL/filename.H>
00053 #endif /* !WIN32 || __CYGWIN__ */
00054 
00055 /*
00056  * 'numericsort()' - Compare two directory entries, possibly with
00057  *                   a case-insensitive comparison...
00058  */
00059 
00060 static int numericsort(struct dirent **A, struct dirent **B, int cs) {
00061   const char* a = (*A)->d_name;
00062   const char* b = (*B)->d_name;
00063   int ret = 0;
00064   for (;;) {
00065     if (isdigit(*a & 255) && isdigit(*b & 255)) {
00066       int diff,magdiff;
00067       while (*a == '0') a++;
00068       while (*b == '0') b++;
00069       while (isdigit(*a & 255) && *a == *b) {a++; b++;}
00070       diff = (isdigit(*a & 255) && isdigit(*b & 255)) ? *a - *b : 0;
00071       magdiff = 0;
00072       while (isdigit(*a & 255)) {magdiff++; a++;}
00073       while (isdigit(*b & 255)) {magdiff--; b++;}
00074       if (magdiff) {ret = magdiff; break;} /* compare # of significant digits*/
00075       if (diff) {ret = diff; break;}    /* compare first non-zero digit */
00076     } else {
00077       if (cs) {
00078         /* compare case-sensitive */
00079         if ((ret = *a-*b)) break;
00080       } else {
00081         /* compare case-insensitve */
00082         if ((ret = tolower(*a & 255)-tolower(*b & 255))) break;
00083       }
00084 
00085       if (!*a) break;
00086       a++; b++;
00087     }
00088   }
00089   if (!ret) return 0;
00090   else return (ret < 0) ? -1 : 1;
00091 }
00092 
00093 /*
00094  * 'fl_casenumericsort()' - Compare directory entries with case-sensitivity.
00095  */
00096 
00097 int fl_casenumericsort(struct dirent **A, struct dirent **B) {
00098   return numericsort(A, B, 0);
00099 }
00100 
00101 /*
00102  * 'fl_numericsort()' - Compare directory entries with case-sensitivity.
00103  */
00104 
00105 int fl_numericsort(struct dirent **A, struct dirent **B) {
00106   return numericsort(A, B, 1);
00107 }
00108 
00109 /*
00110  * End of "$Id: numericsort.c 8074 2010-12-20 13:45:26Z ianmacarthur $".
00111  */