aboutsummaryrefslogtreecommitdiff
path: root/externals/loaders/import.c
blob: 21ecd65674d74609cbd146da4dcef9b5ac059828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * 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_outlet            *x_data_outlet;
	t_outlet            *x_status_outlet;
} t_import;


static int import_load_lib(char *libname)
{
	post("import_load_lib");
/* sys_load_lib_dir is only used on Pd-extended < 0.40 */
#if (PD_MINOR_VERSION >= 40)
	t_canvas *canvas = canvas_getcurrent();

    if (!sys_load_lib(canvas, libname))
	{
		post("%s: can't load library in %s", libname, sys_libdir->s_name);
		return 0;
	}
#else
    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;
#endif
}


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("[import] loaded library: %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);
	char buffer[MAXPDSTRING];

    x->loaded_libs = 0;
    x->num_libs = 0;
	x->x_data_outlet = outlet_new(&x->x_obj, &s_symbol);
	x->x_status_outlet = outlet_new(&x->x_obj, 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;
  }
  /* TODO: look into freeing the namelist.  It probably does not need to
   * happen, since this class is just copying the pointer of an existing
   * namelist that is handled elsewhere. */
}


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);
}