changeset 34836:34c835d22130

Improve filter on file selector's file list. Sort the files globally, don't sort each block of files with the same extension separately. This is achieved by not using the glob() function for each extension separately, but only once and filtering in one pass which should improve the speed as well.
author ib
date Sun, 20 May 2012 08:54:50 +0000
parents d59e91d7e4b4
children b56cc3fd7b8a
files gui/ui/gtk/fileselect.c
diffstat 1 files changed, 39 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/gui/ui/gtk/fileselect.c	Fri May 18 20:52:12 2012 +0000
+++ b/gui/ui/gtk/fileselect.c	Sun May 20 08:54:50 2012 +0000
@@ -172,19 +172,6 @@
  return utf8dir;
 }
 
-static char * Filter( const char * name )
-{
- static char tmp[32];
- unsigned int  i,c;
- for ( i=0,c=0;i < strlen( name );i++ )
-  {
-   if ( ( name[i] >='a' )&&( name[i] <= 'z' ) ) { tmp[c++]='['; tmp[c++]=name[i]; tmp[c++]=name[i] - 32; tmp[c++]=']'; }
-    else tmp[c++]=name[i];
-  }
- tmp[c]=0;
- return tmp;
-}
-
 static void clist_append_fname(GtkWidget * list, char *fname,
                                GdkPixmap *pixmap, GdkPixmap *mask) {
   gint pos;
@@ -200,8 +187,9 @@
 static void CheckDir( GtkWidget * list )
 {
  struct stat     fs;
- int             i;
+ int             i, j, fn;
  glob_t          gg;
+ gchar          *filter, **fext;
 
  if ( !fsFilter[0] ) return;
 
@@ -218,30 +206,51 @@
    if( !S_ISDIR( fs.st_mode ) ) continue;
    clist_append_fname(list, gg.gl_pathv[i], dpixmap, dmask);
   }
- globfree( &gg );
+
+ for (fn = 1, i = 0; fsFilter[i]; i++)
+   if (fsFilter[i] == ',') fn++;
+
+ filter = g_strdup(fsFilter);
+ fext = calloc(fn, sizeof(gchar *));
 
- if ( strchr( fsFilter,',' ) )
-  {
-   char tmp[8];
-   int  i,c,glob_param = 0;
-   for ( i=0,c=0;i<(int)strlen( fsFilter ) + 1;i++,c++ )
-    {
-     tmp[c]=fsFilter[i];
-     if ( ( tmp[c] == ',' )||( tmp[c] == '\0' ) )
-      {
-       tmp[c]=0; c=-1;
-       glob( Filter( tmp ),glob_param,NULL,&gg );
-       glob_param=GLOB_APPEND;
-      }
-    }
-  } else glob( Filter( fsFilter ),0,NULL,&gg );
+ if (filter && fext)
+ {
+   for (j = 0, i = 0; filter[i]; i++)
+   {
+     if (filter[i] == '.') fext[j] = filter + i;
+
+     if (filter[i] == ',')
+     {
+       filter[i] = 0;
+       j++;
+     }
+   }
 
  for(  i=0;(unsigned)i<gg.gl_pathc;i++ )
   {
+     char *ext;
+
    stat( gg.gl_pathv[i],&fs );
    if(  S_ISDIR( fs.st_mode ) ) continue;
+
+     ext = strrchr(gg.gl_pathv[i], '.');
+
+     if (ext || !fext[0])
+     {
+       for (j = 0; j < fn; j++)
+       {
+         if (fext[j] == NULL || strcasecmp(fext[j], ext) == 0)
+         {
    clist_append_fname(list, gg.gl_pathv[i], fpixmap, fmask);
+           break;
+         }
+       }
+     }
   }
+ }
+
+ free(fext);
+ g_free(filter);
  globfree( &gg );
 
  gtk_clist_set_column_width( GTK_CLIST( list ),0,17 );