Max 5 API Reference
00001 /** 00002 @file 00003 filein - read in a file of binary data and output the data at various points in the file 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_path.h" 00013 00014 00015 void *filein_class; 00016 00017 typedef struct filein { 00018 t_object f_ob; 00019 t_filehandle f_fh; 00020 short f_open; /* spool flag */ 00021 short f_spool; 00022 Byte **f_data; /* read in data */ 00023 long f_size; 00024 void *f_out; 00025 void *f_eof; 00026 void *f_readdone; 00027 } t_filein; 00028 00029 t_symbol *ps_nothing,*ps_spool,*ps_int,*ps_in1,*ps_in2; 00030 00031 void filein_access(t_filein *x, t_symbol *s, short ac, t_atom *av); 00032 void filein_int(t_filein *x, long n); 00033 void filein_in1(t_filein *x, long n); 00034 void filein_in2(t_filein *x, long n); 00035 void filein_close(t_filein *x); 00036 void filein_open(t_filein *x, char *name); 00037 void filein_doread(t_filein *x, t_symbol *s); 00038 void filein_spool(t_filein *x, t_symbol *s); 00039 void filein_read(t_filein *x, t_symbol *s); 00040 void filein_free(t_filein *x); 00041 void filein_assist(t_filein *x, void *b, long m, long a, char *s); 00042 void *filein_new(t_symbol *fn, t_symbol *spoolFlag); 00043 void ctopcpy(unsigned char *p1, char *p2); 00044 char *strcpy(char *s1, const char *s2); 00045 00046 int main() 00047 { 00048 t_class *c; 00049 00050 c = class_new("filein", (method)filein_new, (method)filein_free, (short)sizeof(t_filein), 00051 0L, A_DEFSYM, A_DEFSYM, 0); 00052 class_addmethod(c, (method)filein_int, "int", A_LONG, 0); 00053 class_addmethod(c, (method)filein_in1, "in1", A_LONG, 0); 00054 class_addmethod(c, (method)filein_in2, "in2", A_LONG, 0); 00055 class_addmethod(c, (method)filein_close, "fclose", 0); 00056 class_addmethod(c, (method)filein_spool, "spool", A_DEFSYM,0); 00057 class_addmethod(c, (method)filein_read, "read", A_DEFSYM,0); 00058 class_addmethod(c, (method)filein_assist, "assist", A_CANT,0); 00059 class_register(CLASS_BOX, c); 00060 filein_class = c; 00061 00062 ps_nothing = gensym(""); 00063 ps_spool = gensym("spool"); 00064 ps_int = gensym("int"); 00065 ps_in1 = gensym("in1"); 00066 ps_in2 = gensym("in2"); 00067 00068 return (0); 00069 } 00070 00071 void filein_access(t_filein *x, t_symbol *s, short ac, t_atom *av) 00072 { 00073 OSErr err; 00074 Byte data[8]; 00075 long count; 00076 unsigned long dummy; 00077 00078 if (s==ps_int) 00079 count = 1; 00080 else if (s==ps_in1) 00081 count = 2; 00082 else 00083 count = 4; 00084 err = sysfile_setpos(x->f_fh,SYSFILE_FROMSTART,av->a_w.w_long); 00085 if (err) 00086 object_error((t_object *)x, "seek err %d",err); 00087 else { 00088 err = sysfile_read(x->f_fh,&count,data); 00089 if (err) 00090 object_error((t_object *)x, "read err %d",err); 00091 else { 00092 if (count==1) 00093 dummy = data[0]; 00094 else 00095 sysmem_copyptr(data,&dummy,count); 00096 outlet_int(x->f_out,dummy); 00097 } 00098 } 00099 } 00100 00101 void filein_int(t_filein *x, long n) /* byte access */ 00102 { 00103 t_atom info; 00104 Byte data[16]; 00105 long count; 00106 OSErr err; 00107 00108 if (x->f_open) { 00109 if (isr()) { 00110 atom_setlong(&info,n); 00111 defer(x,(method)filein_access,ps_int,1,&info); 00112 } else { 00113 err = sysfile_setpos(x->f_fh,SYSFILE_FROMSTART,n); 00114 if (err) 00115 object_error((t_object *)x, "seek err %d",err); 00116 else { 00117 count = 1; 00118 err = sysfile_read(x->f_fh,&count,data); 00119 if (err) 00120 object_error((t_object *)x, "read err %d",err); 00121 else { 00122 count = data[0]; 00123 outlet_int(x->f_out,count); 00124 } 00125 } 00126 } 00127 } else if (x->f_data) { 00128 if (n < 0) 00129 object_error((t_object *)x, "access out of range"); 00130 else if (n >= x->f_size) 00131 outlet_bang(x->f_eof); 00132 else { 00133 count = *((*(x->f_data))+n); 00134 outlet_int(x->f_out,count); 00135 } 00136 } 00137 } 00138 00139 void filein_in1(t_filein *x, long n) /* int access */ 00140 { 00141 t_atom info; 00142 unsigned short data[4]; 00143 long count; 00144 OSErr err; 00145 00146 if (x->f_open) { 00147 if (isr()) { 00148 atom_setlong(&info,n); 00149 defer(x,(method)filein_access,ps_in1,1,&info); 00150 } else { 00151 err = sysfile_setpos(x->f_fh,SYSFILE_FROMSTART,n); 00152 if (err) 00153 object_error((t_object *)x, "seek err %d",err); 00154 else { 00155 count = 2; 00156 err = sysfile_read(x->f_fh,&count,data); 00157 if (err) 00158 object_error((t_object *)x, "read err %d",err); 00159 else { 00160 count = data[0]; 00161 outlet_int(x->f_out,count); 00162 } 00163 } 00164 } 00165 } else if (x->f_data) { 00166 if (n < 0) 00167 object_error((t_object *)x, "access out of range"); 00168 else if (n >= x->f_size) 00169 outlet_bang(x->f_eof); 00170 else { 00171 sysmem_copyptr(*(x->f_data)+n,data,2L); 00172 outlet_int(x->f_out,(unsigned long)data[0]); 00173 } 00174 } 00175 } 00176 00177 void filein_in2(t_filein *x, long n) /* long access */ 00178 { 00179 t_atom info; 00180 unsigned long data[4]; 00181 long count; 00182 OSErr err; 00183 00184 if (x->f_open) { 00185 if (isr()) { 00186 atom_setlong(&info,n); 00187 defer(x,(method)filein_access,ps_in2,1,&info); 00188 } else { 00189 err = sysfile_setpos(x->f_fh,SYSFILE_FROMSTART,n); 00190 if (err) 00191 object_error((t_object *)x, "seek err %d",err); 00192 else { 00193 count = 4; 00194 err = sysfile_read(x->f_fh,&count,data); 00195 if (err) 00196 object_error((t_object *)x, "read err %d",err); 00197 else { 00198 count = data[0]; 00199 outlet_int(x->f_out,count); 00200 } 00201 } 00202 } 00203 } else if (x->f_data) { 00204 if (n < 0) 00205 object_error((t_object *)x, "access out of range"); 00206 else if (n >= x->f_size) 00207 outlet_bang(x->f_eof); 00208 else { 00209 sysmem_copyptr(*(x->f_data)+n,data,4L); 00210 outlet_int(x->f_out,data[0]); 00211 } 00212 } 00213 } 00214 00215 void filein_close(t_filein *x) 00216 { 00217 if (x->f_open) { 00218 sysfile_close(x->f_fh); 00219 x->f_fh = 0; 00220 x->f_open = FALSE; 00221 } 00222 if (x->f_data) { 00223 sysmem_lockhandle((t_handle)x->f_data,0); 00224 sysmem_freehandle((t_handle)x->f_data); 00225 x->f_data = 0; 00226 } 00227 } 00228 00229 void filein_open(t_filein *x, char *name) 00230 { 00231 long size; 00232 00233 if (x->f_spool) 00234 x->f_open = TRUE; 00235 else { 00236 sysfile_geteof(x->f_fh,&size); 00237 if (!(x->f_data = (Byte **)sysmem_newhandle(size))) 00238 object_error((t_object *)x, "%s too big to read",name); 00239 else { 00240 sysmem_lockhandle((t_handle)x->f_data,1); 00241 sysfile_read(x->f_fh,&size,*x->f_data); 00242 x->f_size = size; 00243 } 00244 sysfile_close(x->f_fh); 00245 } 00246 x->f_spool = FALSE; 00247 } 00248 00249 void filein_doread(t_filein *x, t_symbol *s) 00250 { 00251 short vol,err; 00252 char ps[MAX_PATH_CHARS]; 00253 long type; 00254 short savelock; 00255 00256 filein_close(x); 00257 if (s==ps_nothing) { 00258 if (open_dialog(ps,&vol,&type,0L,0)) 00259 return; 00260 } else { 00261 strcpy(ps,s->s_name); 00262 if (locatefile_extended(ps,&vol,&type,&type,-1)) { 00263 object_error((t_object *)x, "%s: can't find file",ps); 00264 return; 00265 } 00266 } 00267 err = path_opensysfile(ps,vol,&x->f_fh,READ_PERM); 00268 if (err) { 00269 object_error((t_object *)x, "%s: error %d opening file",ps,err); 00270 return; 00271 } 00272 filein_open(x,ps); 00273 savelock = lockout_set(1); 00274 outlet_bang(x->f_readdone); 00275 lockout_set(savelock); 00276 } 00277 00278 void filein_spool(t_filein *x, t_symbol *s) 00279 { 00280 x->f_spool = TRUE; 00281 defer(x,(method)filein_doread,s,0,0L); 00282 } 00283 00284 void filein_read(t_filein *x, t_symbol *s) 00285 { 00286 x->f_spool = FALSE; 00287 defer(x,(method)filein_doread,s,0,0L); 00288 } 00289 00290 void filein_free(t_filein *x) 00291 { 00292 filein_close(x); 00293 } 00294 00295 void filein_assist(t_filein *x, void *b, long m, long a, char *s) 00296 { 00297 // this system eliminates the need for a STR# resource 00298 if (m==1) { 00299 switch (a) { 00300 case 0: sprintf(s,"Read Byte from File"); break; 00301 case 1: sprintf(s,"Read Word from File"); break; 00302 case 2: sprintf(s,"Read Long from File"); break; 00303 } 00304 } 00305 else if (m==2) { 00306 switch (a) { 00307 case 0: sprintf(s,"File Data Output"); break; 00308 case 1: sprintf(s,"bang On End of File"); break; 00309 case 2: sprintf(s,"bang When Read/Spool Finished"); break; 00310 } 00311 } 00312 } 00313 00314 00315 void *filein_new(t_symbol *fn, t_symbol *spoolFlag) 00316 { 00317 t_filein *x; 00318 00319 x = object_alloc(filein_class); 00320 x->f_readdone = bangout(x); 00321 x->f_eof = bangout(x); 00322 x->f_out = intout(x); 00323 intin(x,2); 00324 intin(x,1); 00325 x->f_open = FALSE; 00326 x->f_fh = 0; 00327 x->f_spool = 0; 00328 x->f_data = 0; 00329 if (fn != ps_nothing) { 00330 if (spoolFlag==ps_spool) 00331 x->f_spool = TRUE; 00332 else 00333 x->f_spool = FALSE; 00334 filein_doread(x,fn); 00335 } 00336 return (x); 00337 } 00338 00339 #ifndef WIN_VERSION 00340 #ifndef TARGET_RT_MAC_MACHO 00341 char *strcpy(char *s1, const char *s2) 00342 { 00343 char *ret = s1; 00344 00345 while ((*s1++ = *s2++) != 0) 00346 ; 00347 00348 return ret; 00349 } 00350 #endif// TARGET_RT_MAC_MACHO 00351 #endif// WIN_VERSION 00352
Copyright © 2008, Cycling '74