Revision: 19961
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19961
Author:   ton
Date:     2009-04-28 19:25:50 +0200 (Tue, 28 Apr 2009)

Log Message:
-----------
Bugfix #18637

Sorting of files didn't take numbers into account propertly, so it
ordered files like;

10.jpg
300.jpg
89.jpg
9.jpg

Quite simple to solve, almost moved report to the 'todo', but too
fun to leave this. :)

function name is BLI_natstrcmp() "natural string compare".

Modified Paths:
--------------
    trunk/blender/source/blender/blenlib/BLI_blenlib.h
    trunk/blender/source/blender/blenlib/intern/storage.c
    trunk/blender/source/blender/blenlib/intern/util.c
    trunk/blender/source/blender/src/filesel.c

Modified: trunk/blender/source/blender/blenlib/BLI_blenlib.h
===================================================================
--- trunk/blender/source/blender/blenlib/BLI_blenlib.h  2009-04-28 15:28:02 UTC 
(rev 19960)
+++ trunk/blender/source/blender/blenlib/BLI_blenlib.h  2009-04-28 17:25:50 UTC 
(rev 19961)
@@ -387,6 +387,7 @@
 char *BLI_strcasestr(const char *s, const char *find);
 int BLI_strcasecmp(const char *s1, const char *s2);
 int BLI_strncasecmp(const char *s1, const char *s2, int n);
+int BLI_natstrcmp(const char *s1, const char *s2);
 void BLI_timestr(double _time, char *str); /* time var is global */
 
 /** 

Modified: trunk/blender/source/blender/blenlib/intern/storage.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/storage.c       2009-04-28 
15:28:02 UTC (rev 19960)
+++ trunk/blender/source/blender/blenlib/intern/storage.c       2009-04-28 
17:25:50 UTC (rev 19961)
@@ -153,7 +153,7 @@
        if( strcmp(entry2->relname, ".")==0 ) return (1);
        if( strcmp(entry1->relname, "..")==0 ) return (-1);
        
-       return (BLI_strcasecmp(entry1->relname,entry2->relname));
+       return (BLI_natstrcmp(entry1->relname,entry2->relname));
 }
 
 

Modified: trunk/blender/source/blender/blenlib/intern/util.c
===================================================================
--- trunk/blender/source/blender/blenlib/intern/util.c  2009-04-28 15:28:02 UTC 
(rev 19960)
+++ trunk/blender/source/blender/blenlib/intern/util.c  2009-04-28 17:25:50 UTC 
(rev 19961)
@@ -1985,7 +1985,56 @@
        return 0;
 }
 
+/* natural string compare, keeping numbers in order */
+int BLI_natstrcmp(const char *s1, const char *s2)
+{
+       int d1= 0, d2= 0;
+       
+       /* if both chars are numeric, to a strtol().
+          then increase string deltas as long they are 
+          numeric, else do a tolower and char compare */
+       
+       while(1) {
+               char c1 = tolower(s1[d1]);
+               char c2 = tolower(s2[d2]);
+               
+               if( isdigit(c1) && isdigit(c2) ) {
+                       int val1, val2;
+                       
+                       val1= (int)strtol(s1+d1, (char **)NULL, 10);
+                       val2= (int)strtol(s2+d2, (char **)NULL, 10);
+                       
+                       if (val1<val2) {
+                               return -1;
+                       } else if (val1>val2) {
+                               return 1;
+                       }
+                       d1++;
+                       while( isdigit(s1[d1]) )
+                               d1++;
+                       d2++;
+                       while( isdigit(s2[d2]) )
+                               d2++;
+                       
+                       c1 = tolower(s1[d1]);
+                       c2 = tolower(s2[d2]);
+               }
+               
+               if (c1<c2) {
+                       return -1;
+               } else if (c1>c2) {
+                       return 1;
+               } else if (c1==0) {
+                       break;
+               }
+               d1++;
+               d2++;
+       }
+       
+}
 
+
+
 #ifdef WITH_ICONV
 #include "iconv.h"
 #include "localcharset.h"

Modified: trunk/blender/source/blender/src/filesel.c
===================================================================
--- trunk/blender/source/blender/src/filesel.c  2009-04-28 15:28:02 UTC (rev 
19960)
+++ trunk/blender/source/blender/src/filesel.c  2009-04-28 17:25:50 UTC (rev 
19961)
@@ -196,7 +196,7 @@
        if( strcmp(entry2->relname, ".")==0 ) return (1);
        if( strcmp(entry1->relname, "..")==0 ) return (-1);
        
-       return (BLI_strcasecmp(entry1->relname,entry2->relname));
+       return (BLI_natstrcmp(entry1->relname,entry2->relname));
 }
 
 static int compare_date(const void *a1, const void *a2)        


_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to