Max 5 API Reference
00001 /** 00002 @file 00003 folder - list the files in a specific folder 00004 00005 updated 3/22/09 ajm: new API 00006 00007 @ingroup examples 00008 */ 00009 00010 #include "ext.h" 00011 #include "ext_obex.h" 00012 #include "ext_common.h" 00013 #include "ext_strings.h" 00014 00015 00016 void *folder_class; 00017 00018 /* first argument is folder name : is subdir of Max, otherwise starts with disk. 00019 subsequent arguments are file types to search for (default is TEXT and maxb) 00020 */ 00021 00022 #define MAXTYPES 128 00023 00024 typedef struct _folder { 00025 t_object f_ob; 00026 short f_path; 00027 t_symbol *f_input; 00028 OSType f_types[MAXTYPES]; 00029 long f_numtypes; 00030 void *f_countout; 00031 long f_outcount; 00032 } t_folder; 00033 00034 void folder_bang(t_folder *x); 00035 void folder_types(t_folder *x, t_symbol *s, short ac, t_atom *av); 00036 void folder_anything(t_folder *x, t_symbol *s, short ac, t_atom *av); 00037 void folder_assist(t_folder *x,void *b,long m,long a,char *s); 00038 void *folder_new(t_symbol *s, short ac, t_atom *av); 00039 void folder_lookup(t_folder *x); 00040 void folder_enumerate(t_folder *x); 00041 void folder_action(t_folder *x); 00042 00043 t_symbol *ps_clear,*ps_append; 00044 long debug; 00045 00046 int main() 00047 { 00048 t_class *c; 00049 00050 c = class_new("folder", (method)folder_new, 0L, (short)sizeof(t_folder), 0L, A_GIMME, 0); 00051 class_addmethod(c, (method)folder_bang, "int", A_LONG, 0); 00052 class_addmethod(c, (method)folder_bang, "bang", 0); 00053 class_addmethod(c, (method)folder_anything, "anything", A_GIMME, 0); 00054 class_addmethod(c, (method)folder_anything, "symbol", A_SYM, 0); 00055 class_addmethod(c, (method)folder_types, "types", A_GIMME, 0); 00056 class_addmethod(c, (method)folder_assist, "assist", A_CANT, 0); 00057 class_register(CLASS_BOX, c); 00058 folder_class = c; 00059 00060 ps_clear = gensym("clear"); 00061 ps_append = gensym("append"); 00062 debug = 0; 00063 00064 return 0; 00065 } 00066 00067 void folder_action(t_folder *x) 00068 { 00069 x->f_outcount = 0; 00070 if (x->f_path == 0) { 00071 folder_lookup(x); 00072 if (x->f_path == 0) 00073 goto bag; 00074 } 00075 folder_enumerate(x); 00076 bag: 00077 outlet_int(x->f_countout,x->f_outcount); 00078 } 00079 00080 void folder_bang(t_folder *x) 00081 { 00082 defer(x,(method)folder_action,0L,0,0L); 00083 } 00084 00085 void folder_assist(t_folder *x,void *b,long m,long a,char *s) 00086 { 00087 if (m==1) { 00088 switch (a) { 00089 case 0: sprintf(s,"Path Name of Folder to List"); break; 00090 } 00091 } 00092 else if (m==2) { 00093 switch (a) { 00094 case 0: sprintf(s,"File Name List, Connect to Menu"); break; 00095 case 1: sprintf(s,"Count of Items Listed"); break; 00096 } 00097 } 00098 } 00099 00100 void folder_lookup(t_folder *x) 00101 { 00102 short path,err; 00103 char sourcename[512],filename[256]; 00104 00105 if (x->f_input) { 00106 strcpy(sourcename,x->f_input->s_name); 00107 err = path_frompathname(sourcename, &path, filename); 00108 if (err || filename[0]) { 00109 object_error((t_object *)x, "%s: not a folder",x->f_input->s_name); 00110 x->f_path = 0; 00111 } 00112 else { 00113 x->f_path = path; 00114 } 00115 } 00116 } 00117 00118 void folder_anything(t_folder *x, t_symbol *s, short ac, t_atom *av) 00119 { 00120 x->f_input = s; 00121 x->f_path = 0; 00122 folder_bang(x); 00123 } 00124 00125 void folder_types(t_folder *x, t_symbol *s, short ac, t_atom *av) 00126 { 00127 Byte type[4]; 00128 long len=0,i; 00129 00130 x->f_numtypes = 0; 00131 while (ac--) { 00132 if (atom_gettype(av) == A_SYM) { 00133 len = strlen(atom_getsym(av)->s_name); 00134 for (i=0;i<4;i++) { 00135 type[i] = (i>=len) ? ' ' : atom_getsym(av)->s_name[i]; 00136 } 00137 STR_TO_FOURCC(*((long *)type)); 00138 sysmem_copyptr(type,&x->f_types[x->f_numtypes++],4L); 00139 } 00140 av++; 00141 } 00142 } 00143 00144 void *folder_new(t_symbol *s, short ac, t_atom *av) 00145 { 00146 t_folder *x; 00147 00148 x = object_alloc(folder_class); 00149 x->f_countout = intout((t_object *)x); 00150 outlet_new((t_object *)x,0); 00151 x->f_input = 0; 00152 x->f_numtypes = 0; 00153 x->f_path = 0; 00154 x->f_outcount = 0; 00155 if (ac) { 00156 if (atom_gettype(av) == A_SYM) 00157 { 00158 x->f_input = atom_getsym(av); 00159 } 00160 if (ac > 1) { 00161 av++; 00162 folder_types(x,s,ac-1,av); // ddz 06/27/05 ac -> ac-1 bug fix 00163 } 00164 } 00165 if (!x->f_numtypes) { 00166 short numtypes; 00167 00168 typelist_make((long *)x->f_types, TYPELIST_MAXFILES, &numtypes); 00169 x->f_numtypes = numtypes; 00170 } 00171 if (x->f_input) 00172 folder_lookup(x); 00173 return x; 00174 } 00175 00176 void folder_enumerate(t_folder *x) 00177 { 00178 void *fold; 00179 Boolean cleared = false, match; 00180 t_atom a; 00181 long type,i; 00182 char name[256]; 00183 short savelock; 00184 t_fileinfo info; 00185 00186 if (!x->f_path) 00187 return; 00188 fold = path_openfolder(x->f_path); 00189 if (!fold) 00190 return; 00191 00192 while (path_foldernextfile(fold,&type,name,false)) { 00193 if (x->f_numtypes==0) 00194 match = true; 00195 else { 00196 match = false; 00197 if (!type) { 00198 if (!path_extendedfileinfo(name,x->f_path,&info,(long *)x->f_types,x->f_numtypes,true)) 00199 type = info.type; 00200 } 00201 for (i = 0; i < x->f_numtypes; i++) { 00202 if (type == x->f_types[i]) { 00203 match = true; 00204 break; 00205 } 00206 } 00207 } 00208 if (match) { 00209 if (!cleared) { 00210 savelock = lockout_set(true); 00211 outlet_anything(x->f_ob.o_outlet,ps_clear,0,0); 00212 lockout_set(savelock); 00213 cleared = true; 00214 } 00215 atom_setsym(&a, gensym(name)); 00216 savelock = lockout_set(true); 00217 outlet_anything(x->f_ob.o_outlet,ps_append,1,&a); 00218 lockout_set(savelock); 00219 x->f_outcount++; 00220 } 00221 } 00222 path_closefolder(fold); 00223 00224 }
Copyright © 2008, Cycling '74