Update of /cvsroot/pure-data/externals/hcs In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25799
Modified Files: folder_list-help.pd folder_list.c Log Message: fully working version based on glob.h
Index: folder_list-help.pd =================================================================== RCS file: /cvsroot/pure-data/externals/hcs/folder_list-help.pd,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** folder_list-help.pd 26 Mar 2006 06:02:23 -0000 1.1 --- folder_list-help.pd 26 Mar 2006 06:08:39 -0000 1.2 *************** *** 1,30 **** ! #N canvas 254 219 474 457 10; ! #X obj 100 214 print OUTPUT; ! #X msg 32 49 bang; ! #X msg 100 100 symbol /; ! #X text 69 48 immediate listing of current directory; ! #X text 160 99 immediate listing of /; ! #X text 180 133 set current dir without no output; ! #X obj 100 180 folder_list /var; ! #X obj 57 392 print OUTPUT; ! #X msg 56 328 bang; ! #X text 182 357 defaults to $HOME; ! #X msg 203 152 symbol /usr; ! #X obj 63 280 print ===================; ! #X msg 63 252 bang; ! #X obj 82 204 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 -1; ! #X obj 40 377 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 -1; ! #X obj 57 358 folder_list; ! #X symbolatom 73 79 10 0 0 0 - - -; ! #X connect 1 0 6 0; ! #X connect 2 0 6 0; ! #X connect 6 0 0 0; ! #X connect 6 0 13 0; ! #X connect 8 0 15 0; ! #X connect 10 0 6 1; ! #X connect 12 0 11 0; ! #X connect 15 0 7 0; ! #X connect 15 0 14 0; ! #X connect 16 0 6 0; --- 1,30 ---- ! #N canvas 254 219 494 477 10; ! #X msg 74 72 bang; ! #X msg 101 336 bang; ! #X text 227 365 defaults to $HOME; ! #X obj 124 227 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 -1; ! #X obj 85 385 bng 15 250 50 0 empty empty empty 0 -6 0 8 -262144 -1 -1; ! #X msg 235 178 symbol /usr/*; ! #X text 36 25 Get a listing of files based on a "glob" pattern.; ! #X text 110 74 list current directory; ! #X text 219 160 set pattern without no output; ! #X text 241 111 all files with a dot in your Home; ! #X obj 101 367 folder_list; ! #X obj 142 203 folder_list /*; ! #X obj 143 238 print; ! #X text 34 49 left/hot inlet gives immediate output using stored path ! ; ! #X text 38 279 For UNIX people , [folder_list] behaves just like "ls ! -d1A"; ! #X obj 102 400 print; ! #X msg 142 109 symbol ~/*.*; ! #X connect 0 0 11 0; ! #X connect 1 0 10 0; ! #X connect 5 0 11 1; ! #X connect 10 0 4 0; ! #X connect 10 0 15 0; ! #X connect 11 0 3 0; ! #X connect 11 0 12 0; ! #X connect 16 0 11 0;
Index: folder_list.c =================================================================== RCS file: /cvsroot/pure-data/externals/hcs/folder_list.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** folder_list.c 26 Mar 2006 06:02:23 -0000 1.1 --- folder_list.c 26 Mar 2006 06:08:39 -0000 1.2 *************** *** 1,10 **** #include <m_pd.h> - #include <unistd.h> #include <stdlib.h> ! #include <stdio.h> ! #include <dirent.h> ! #include <fnmatch.h> ! #include <string.h> ! #include <sys/stat.h>
static char *version = "$Revision$"; --- 1,5 ---- #include <m_pd.h> #include <stdlib.h> ! #include <glob.h>
static char *version = "$Revision$"; *************** *** 20,26 **** typedef struct _folder_list { t_object x_obj; - t_symbol *x_folder_name; t_symbol *x_pattern; ! DIR *x_folder_pointer; } t_folder_list;
--- 15,20 ---- typedef struct _folder_list { t_object x_obj; t_symbol *x_pattern; ! glob_t x_glob; } t_folder_list;
*************** *** 30,73 ****
- static t_int folder_list_open(t_folder_list* x) - { - DEBUG(post("folder_list_rewind");); - - if((x->x_folder_pointer = opendir(x->x_folder_name->s_name)) == NULL) { - error("cannot open directory: %s\n", x->x_folder_name->s_name); - return(0); - } - return(1); - } - - - static void folder_list_close(t_folder_list* x) - { - DEBUG(post("folder_list_close");); - - closedir(x->x_folder_pointer); - } - - /* - static void folder_list_rewind(t_folder_list* x) - { - DEBUG(post("folder_list_rewind");); - rewinddir(x->x_folder_pointer); - } - */ - static void folder_list_output(t_folder_list* x) { DEBUG(post("folder_list_output");); ! struct dirent *entry;
! if( !folder_list_open(x) ) return; ! while( ( entry = readdir( x->x_folder_pointer ) ) != NULL) { ! if( fnmatch( x->x_folder_name, entry->d_name, 0 ) ) ! if( strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..") ) ! outlet_symbol(x->x_obj.ob_outlet, gensym(entry->d_name)); ! } ! folder_list_close(x); }
--- 24,44 ----
static void folder_list_output(t_folder_list* x) { DEBUG(post("folder_list_output");); ! t_int i;
! DEBUG(post("globbing %s",x->x_pattern->s_name);); ! switch( glob( x->x_pattern->s_name, GLOB_TILDE, NULL, &(x->x_glob) ) ) { ! case GLOB_NOSPACE: ! error("[folder_list] out of memory"); break; ! case GLOB_ABORTED: ! error("[folder_list] aborted"); break; ! case GLOB_NOMATCH: ! error("[folder_list] no match"); break; ! } ! for(i = 0; i < x->x_glob.gl_matchc; i++) ! outlet_symbol( x->x_obj.ob_outlet, gensym(x->x_glob.gl_pathv[i]) ); }
*************** *** 75,79 **** static void folder_list_symbol(t_folder_list *x, t_symbol *s) { ! x->x_folder_name = s; folder_list_output(x); } --- 46,50 ---- static void folder_list_symbol(t_folder_list *x, t_symbol *s) { ! x->x_pattern = s; folder_list_output(x); } *************** *** 83,87 **** { DEBUG(post("folder_list_set");); ! x->x_folder_name = s; }
--- 54,58 ---- { DEBUG(post("folder_list_set");); ! x->x_pattern = s; }
*************** *** 91,94 **** --- 62,66 ---- DEBUG(post("folder_list_free"););
+ globfree( &(x->x_glob) ); }
*************** *** 101,114 **** post("[folder_list] %s, written by Hans-Christoph Steiner hans@at.or.at",version); ! ! /* init vars */ ! x->x_folder_name = gensym(getenv("HOME"));
! symbolinlet_new(&x->x_obj, &x->x_folder_name); outlet_new(&x->x_obj, &s_symbol); /* set to the value from the object argument, if that exists */ if (s != &s_) ! x->x_folder_name = s; return (x); --- 73,85 ---- post("[folder_list] %s, written by Hans-Christoph Steiner hans@at.or.at",version); ! /* set HOME as default */ ! x->x_pattern = gensym(getenv("HOME"));
! symbolinlet_new(&x->x_obj, &x->x_pattern); outlet_new(&x->x_obj, &s_symbol); /* set to the value from the object argument, if that exists */ if (s != &s_) ! x->x_pattern = s; return (x); *************** *** 126,206 **** 0); /* add inlet datatype methods */ ! // class_addfloat(folder_list_class,(t_method) folder_list_float); class_addbang(folder_list_class,(t_method) folder_list_output); class_addsymbol(folder_list_class,(t_method) folder_list_symbol); /* add inlet message methods */ - /* class_addmethod(folder_list_class, - (t_method) folder_list_rewind, - gensym("rewind"), - 0); class_addmethod(folder_list_class,(t_method) folder_list_set,gensym("set"), ! A_DEFSYM, 0);*/ ! } ! ! ! ! /* ! * WINDOWS EXAMPLE ! http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnucmg/html... ! ! #include <windows.h> ! #include <stdio.h> ! ! void ScanDir(char *dirname, int indent) ! { ! BOOL fFinished; ! HANDLE hList; ! TCHAR szDir[MAX_PATH+1]; ! TCHAR szSubDir[MAX_PATH+1]; ! WIN32_FIND_DATA FileData; ! ! // Get the proper directory path ! sprintf(szDir, "%s\*", dirname); ! ! // Get the first file ! hList = FindFirstFile(szDir, &FileData); ! if (hList == INVALID_HANDLE_VALUE) ! { ! printf("No files found\n\n"); ! } ! else ! { ! // Traverse through the directory structure ! fFinished = FALSE; ! while (!fFinished) ! { ! // Check the object is a directory or not ! if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ! { ! if ((strcmp(FileData.cFileName, ".") != 0) && ! (strcmp(FileData.cFileName, "..") != 0)) ! { ! printf("%*s%s\\n", indent, "", ! FileData.cFileName); ! ! // Get the full path for sub directory ! sprintf(szSubDir, "%s\%s", dirname, ! FileData.cFileName); ! ! ScanDir(szSubDir, indent + 4); ! } ! } ! else ! printf("%*s%s\n", indent, "", FileData.cFileName); ! ! ! if (!FindNextFile(hList, &FileData)) ! { ! if (GetLastError() == ERROR_NO_MORE_FILES) ! { ! fFinished = TRUE; ! } ! } ! } ! } ! ! FindClose(hList); }
- */ --- 97,107 ---- 0); /* add inlet datatype methods */ ! // class_addfloat(folder_list_class,(t_method) glob_float); class_addbang(folder_list_class,(t_method) folder_list_output); class_addsymbol(folder_list_class,(t_method) folder_list_symbol); /* add inlet message methods */ class_addmethod(folder_list_class,(t_method) folder_list_set,gensym("set"), ! A_DEFSYM, 0); }