aboutsummaryrefslogtreecommitdiff
path: root/externals/loaders/import.c
blob: b57b594e791dddd0522d7479ebea2e7d4a43fdfa (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
/*
 * 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("[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);
    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);
}