diff options
Diffstat (limited to 'externals/loaders')
-rw-r--r-- | externals/loaders/help/import-help.pd | 12 | ||||
-rw-r--r-- | externals/loaders/import.c | 111 |
2 files changed, 123 insertions, 0 deletions
diff --git a/externals/loaders/help/import-help.pd b/externals/loaders/help/import-help.pd new file mode 100644 index 00000000..c640ef82 --- /dev/null +++ b/externals/loaders/help/import-help.pd @@ -0,0 +1,12 @@ +#N canvas 476 83 465 356 10; +#X msg 112 134 bang; +#X text 33 42 [import] loads libraries from the patch. Currently \, +it loads the libraries into the global namespace \, but the grand plan +is for it only load the libraries into the patch's namespace.; +#X obj 112 159 import ext13 memento rradical; +#X text 37 204 Sending a bang to [import] makes it output information +about the libraries it loaded to the Console.; +#X text 27 300 (C) Copyright 2004 Hans-Christoph Steiner <hans@at.or.at> +; +#X text 244 314 released under the GNU GPL; +#X connect 0 0 2 0; diff --git a/externals/loaders/import.c b/externals/loaders/import.c new file mode 100644 index 00000000..adf46ba7 --- /dev/null +++ b/externals/loaders/import.c @@ -0,0 +1,111 @@ +/* + * This object loads libraries and libdirs from within a patch. It is the + * first small step towards a patch-specific namespace. Currently, it just + * adds things to the global path. It is a reimplementation of a similar/same + * idea from Guenter Geiger's [using] object. <hans@at.or.at> + * + * This object currently depends on the packages/patches/libdir-0.38-4.patch + * for sys_load_lib_dir(). + */ + +#include "m_pd.h" +#include "s_stuff.h" + +static t_class *import_class; + +typedef struct _import +{ + t_object x_obj; + t_atom* loaded_libs; + t_atom* current; + t_int num_libs; +} t_import; + + +static int import_load_lib(char *libname) +{ + if (!sys_load_lib(sys_libdir->s_name, libname)) + if (!sys_load_lib_dir(sys_libdir->s_name, libname)) + { + post("%s: can't load library in %s", libname, sys_libdir->s_name); + return 0; + } + return 1; +} + + +static void import_load_arguments(t_import *x, int argc, t_atom *argv) +{ + t_symbol *libname; + + while (argc--) { + switch (argv->a_type) { + case A_FLOAT: + post("[import] ERROR: floats not supported yet: %f",atom_getfloat(argv)); + break; + case A_SYMBOL: + libname = atom_getsymbol(argv); + + if (import_load_lib(libname->s_name)) + { + x->loaded_libs = copybytes(libname, sizeof(t_atom)); + x->current = x->loaded_libs; + x->num_libs++; + } + post("blahsymbol: %s",libname->s_name); + break; + default: + post("[import] ERROR: Unsupported atom type"); + } + argv++; + } +} + + +static void import_bang(t_import *x) +{ + t_int i = x->num_libs; + t_atom* libs_list = x->loaded_libs; + + post("[import]: %d libs loaded.",x->num_libs); + while(i--) + { + startpost(" %s",(atom_getsymbol(libs_list))->s_name); + libs_list++; + } + endpost(); +} + + +static void *import_new(t_symbol *s, int argc, t_atom *argv) +{ + t_import *x = (t_import *)pd_new(import_class); + x->loaded_libs = 0; + x->num_libs = 0; + import_load_arguments(x,argc,argv); + return (x); +} + + +static void import_free(t_import *x) +{ + + if (x->loaded_libs) { + freebytes(x->loaded_libs, x->num_libs * sizeof(t_atom)); + x->loaded_libs = 0; + x->num_libs = 0; + } + +} + + +void import_setup(void) +{ + import_class = class_new(gensym("import"), (t_newmethod)import_new, + (t_method)import_free, + sizeof(t_import), + CLASS_DEFAULT, + A_GIMME, + 0); + class_addbang (import_class, import_bang); +} |