From 6cdf3911055f04f1a2587483513483e1894bf6e1 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Sun, 26 Mar 2006 23:32:24 +0000 Subject: ported to windows, and some UNIX-side cleanups and fixes as well. Now its time to test it on Mac OS X and try to get envvar expansion working svn path=/trunk/externals/hcs/; revision=4776 --- folder_list-help.pd | 109 ++++++++++++++++++++++++++++------------------------ folder_list.c | 107 +++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 137 insertions(+), 79 deletions(-) diff --git a/folder_list-help.pd b/folder_list-help.pd index a2eeee4..6d270c6 100644 --- a/folder_list-help.pd +++ b/folder_list-help.pd @@ -1,51 +1,58 @@ -#N canvas 254 219 504 539 10; -#X msg 74 72 bang; -#X msg 101 320 bang; -#X obj 124 267 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 218 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 200 set pattern without no output; -#X text 241 111 all files with a dot in your Home; -#X obj 101 351 folder_list; -#X obj 142 243 folder_list /*; -#X obj 143 278 print; -#X text 34 49 left/hot inlet gives immediate output using stored path -; -#X text 49 298 For UNIX people \, [folder_list] behaves just like "ls --d1A"; -#X obj 102 384 print; -#X msg 142 109 symbol ~/*.*; -#X text 153 381 WARNING: the default will change in the future!; -#X text 156 131 patterns default to path Pd launched from; -#X msg 156 148 symbol *; -#X text 258 174 relative paths work; -#X msg 173 175 symbol ../*; -#X obj 51 440 getdir; -#X obj 51 482 folder_list; -#X msg 51 420 bang; -#X obj 51 504 print; -#X obj 51 460 makefilename %s/*; -#X symbolatom 186 456 0 0 0 0 - - -; -#X symbolatom 186 476 0 0 0 0 - - -; -#X text 128 425 What's in the same folder as this patch?; -#X text 186 350 defaults to /; -#X connect 0 0 10 0; -#X connect 1 0 9 0; -#X connect 4 0 10 1; -#X connect 9 0 3 0; -#X connect 9 0 14 0; -#X connect 10 0 2 0; -#X connect 10 0 11 0; -#X connect 15 0 10 0; -#X connect 18 0 10 0; -#X connect 20 0 10 0; -#X connect 21 0 25 0; -#X connect 21 0 26 0; -#X connect 22 0 24 0; -#X connect 23 0 21 0; -#X connect 25 0 22 0; -#X connect 25 0 27 0; +#N canvas 254 219 528 563 10; +#X msg 19 48 bang; +#X msg 101 320 bang; +#X obj 64 267 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 175 218 symbol /usr/*; +#X text 30 5 Get a listing of files based on a "glob" pattern.; +#X text 55 50 list current directory; +#X text 159 200 set pattern without no output; +#X obj 101 351 folder_list; +#X obj 82 243 folder_list /*; +#X obj 83 278 print; +#X text 28 29 left/hot inlet gives immediate output using stored path +; +#X text 49 298 For UNIX people \, [folder_list] behaves just like "ls +-d1A"; +#X obj 102 384 print; +#X msg 91 126 symbol ~/*.*; +#X text 153 381 WARNING: the default will change in the future!; +#X text 153 151 patterns default to path Pd launched from; +#X msg 97 153 symbol *; +#X text 187 177 relative paths work; +#X msg 102 178 symbol ../*; +#X obj 51 440 getdir; +#X obj 51 482 folder_list; +#X msg 51 420 bang; +#X obj 51 504 print; +#X obj 51 460 makefilename %s/*; +#X symbolatom 186 456 0 0 0 0 - - -; +#X symbolatom 186 476 0 0 0 0 - - -; +#X text 128 425 What's in the same folder as this patch?; +#X text 186 350 defaults to /; +#X text 144 97 drive letters work on ReactOS/Windows; +#X msg 69 97 symbol C:/*; +#X text 190 128 all files with a dot in your home; +#X msg 47 73 symbol %USERPROFILE%/*; +#X text 185 73 On ReactOS/Windows you can use Environment variables +; +#X connect 0 0 9 0; +#X connect 1 0 8 0; +#X connect 4 0 9 1; +#X connect 8 0 3 0; +#X connect 8 0 13 0; +#X connect 9 0 2 0; +#X connect 9 0 10 0; +#X connect 14 0 9 0; +#X connect 17 0 9 0; +#X connect 19 0 9 0; +#X connect 20 0 24 0; +#X connect 20 0 25 0; +#X connect 21 0 23 0; +#X connect 22 0 20 0; +#X connect 24 0 21 0; +#X connect 24 0 26 0; +#X connect 30 0 9 0; +#X connect 32 0 9 0; diff --git a/folder_list.c b/folder_list.c index d1863af..9adee97 100644 --- a/folder_list.c +++ b/folder_list.c @@ -1,7 +1,15 @@ #include + +#ifdef _WIN32 +#define _WIN32_WINNT 0x0400 +#include +#include +#else +#include #include +#endif -static char *version = "$Revision: 1.4 $"; +static char *version = "$Revision: 1.5 $"; #define DEBUG(x) //#define DEBUG(x) x @@ -12,55 +20,95 @@ static char *version = "$Revision: 1.4 $"; static t_class *folder_list_class; typedef struct _folder_list { - t_object x_obj; - t_symbol *x_pattern; - glob_t x_glob; + t_object x_obj; + t_symbol *x_pattern; } t_folder_list; /*------------------------------------------------------------------------------ * IMPLEMENTATION */ +// TODO: regexp ~ to USERPROFILE for Windows +// TODO: make FindFirstFile display when its just a dir static void folder_list_output(t_folder_list* x) { DEBUG(post("folder_list_output");); - unsigned int i; +#ifdef _WIN32 + WIN32_FIND_DATA FindFileData; + HANDLE hFind; + DWORD errorNumber; + LPVOID lpErrorMessage; + + hFind = FindFirstFile(x->x_pattern->s_name, &FindFileData); + if (hFind == INVALID_HANDLE_VALUE) + { + errorNumber = GetLastError(); + switch (errorNumber) + { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + error("[folder_list] nothing found for \"%s\"",x->x_pattern->s_name); + break; + default: + FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + errorNumber, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR) &lpErrorMessage, + 0, NULL ); + error("[folder_list] %s", lpErrorMessage); + } + return; + } + do { + outlet_symbol( x->x_obj.ob_outlet, gensym(FindFileData.cFileName) ); + } while (FindNextFile(hFind, &FindFileData) != 0); + FindClose(hFind); +#else + unsigned int i; + glob_t glob_buffer; + DEBUG(post("globbing %s",x->x_pattern->s_name);); - switch( glob( x->x_pattern->s_name, GLOB_TILDE, NULL, &(x->x_glob) ) ) + switch( glob( x->x_pattern->s_name, GLOB_TILDE, NULL, &glob_buffer ) ) { - 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; + case GLOB_NOSPACE: + error("[folder_list] out of memory for \"%s\"",x->x_pattern->s_name); + break; + case GLOB_ABORTED: + error("[folder_list] aborted \"%s\"",x->x_pattern->s_name); + break; + case GLOB_NOMATCH: + error("[folder_list] nothing found for \"%s\"",x->x_pattern->s_name); + break; } - for(i = 0; i < x->x_glob.gl_pathc; i++) - outlet_symbol( x->x_obj.ob_outlet, gensym(x->x_glob.gl_pathv[i]) ); -} - - -static void folder_list_symbol(t_folder_list *x, t_symbol *s) -{ - x->x_pattern = s; - folder_list_output(x); + for(i = 0; i < glob_buffer.gl_pathc; i++) + outlet_symbol( x->x_obj.ob_outlet, gensym(glob_buffer.gl_pathv[i]) ); + globfree( &(glob_buffer) ); +#endif } static void folder_list_set(t_folder_list* x, t_symbol *s) { DEBUG(post("folder_list_set");); +#ifdef _WIN32 + char string_buffer[MAX_PATH]; + ExpandEnvironmentStrings(s->s_name, string_buffer, MAX_PATH); + x->x_pattern = gensym(string_buffer); +#else x->x_pattern = s; +#endif } -static void folder_list_free(t_folder_list* x) +static void folder_list_symbol(t_folder_list *x, t_symbol *s) { - DEBUG(post("folder_list_free");); - - globfree( &(x->x_glob) ); + folder_list_set(x,s); + folder_list_output(x); } @@ -72,7 +120,11 @@ static void *folder_list_new(t_symbol *s) post("[folder_list] %s, written by Hans-Christoph Steiner ",version); /* TODO set current dir of patch as default */ - x->x_pattern = gensym("/"); +#ifdef _WIN32 + x->x_pattern = gensym(getenv("USERPROFILE")); +#else + x->x_pattern = gensym(getenv("HOME")); +#endif symbolinlet_new(&x->x_obj, &x->x_pattern); outlet_new(&x->x_obj, &s_symbol); @@ -89,13 +141,12 @@ void folder_list_setup(void) DEBUG(post("folder_list_setup");); folder_list_class = class_new(gensym("folder_list"), (t_newmethod)folder_list_new, - (t_method)folder_list_free, + 0, sizeof(t_folder_list), 0, A_DEFSYM, 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); -- cgit v1.2.1