aboutsummaryrefslogtreecommitdiff
path: root/k_cext.c
blob: a96030266e43537c5bdb1fa572c3da4821428df6 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* --------------------------- k_cext  ----------------------------------- */
/*                                                                              */
/* Program c directly within a pd object. */
/*                                                                              */
/* This program is free software; you can redistribute it and/or                */
/* modify it under the terms of the GNU General Public License                  */
/* as published by the Free Software Foundation; either version 2               */
/* of the License, or (at your option) any later version.                       */
/*                                                                              */
/* This program is distributed in the hope that it will be useful,              */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of               */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
/* GNU General Public License for more details.                                 */
/*                                                                              */
/* You should have received a copy of the GNU General Public License            */
/* along with this program; if not, write to the Free Software                  */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */
/*                                                                              */
/* ---------------------------------------------------------------------------- */


#include "m_pd.h"
#include "k_cext.h"

#include <ctype.h>

static char *version = 
"k_cext v0.3.1, written by Kjetil S. Matheussen, k.s.matheussen@notam02.no\n"
"Windows-port by Olaf Matthes. Contributors: Thomas Grill and Tim Blechmann.";

int instancenumber=0; // Can not be static because of the win-port.


static t_class *k_cext_class;
static t_class *k_cfunc_class;

int k_cext_intcompare(const void *p1, const void *p2)
{
  int i = *((int *)p1);
  int j = *((int *)p2);
  
  if (i > j)
    return (1);
  if (i < j)
    return (-1);
  return (0);
}

int k_cext_floatcompare(const void *p1, const void *p2)
{
  float i = *((int *)p1);
  float j = *((int *)p2);
  
  if (i > j)
    return (1);
  if (i < j)
    return (-1);
  return (0);
}


static void k_cext_print(t_k_cext *x){
  FILE *printfile;
  char name[500];
  int lokke;
  char temp[500];

  sprintf(name,"%s.c",x->filename);

  printfile=fopen(name,"r");
  post("------------------------------------------");
  for(lokke=1;;lokke++){
    char temp2[500];
    int c;
    if((c=fgetc(printfile))==EOF) break;
    ungetc(c,printfile);
    fgets(temp,400,printfile);
    sprintf(temp2,"%3d %s",lokke,temp);
    temp2[strlen(temp2)-1]=0;
    post(temp2);
  }
  fclose(printfile);
  post("------------------------------------------");

}

static void k_cext_bang(t_k_cext *x)
{
  /* Fixme, for some reason, k_cfunc's float method points to k_cext_float
     instead of k_cfunc_float.
     As a workaround, there is this x->iscext==true test below. */
  if(x->k_cext_process!=NULL && x->iscext==true)
    (*x->k_cext_process)(x);
}


static void k_cext_float(t_k_cext *x, t_floatarg f)
{
  x->values[0]=f;

  k_cext_bang(x);
}

static void k_cfunc_float(t_k_cext *x, t_floatarg f)
{
  post("k_cfunc_float");
  x->values[0]=f;
}


#include "k_cext_funchandler.c"


struct k_cext_init{
  int indentation;
  int set_indentation[500]; // Very unlikely that anyone wants to indent more than 500 levels.
  int thisisanelifline;
  FILE *file;
  char name[500];
  char funcname[500];

  int num_intfuncs;
  int num_floatfuncs;

  char intfuncnames[50000]; // Max functionname length=50, max number of functions=1000.
  char floatfuncnames[50000];

  int doinitpos1;
  int doinitpos2;

  /* The rest is used by k_cfunc objects. */
  bool cfuncnamefound;
  char cfuncname[200];
  int cfuncrettype; //0=int, 1=float
  int numargs;
  char cfuncargtypes[50000];
  char cfuncargnames[50000];
};




/******************************/
/* Set up inlets and outlets. */
/******************************/
static void k_cext_makeinletsandoutlets(t_k_cext *x,t_int argc,t_atom* argv){
  int lokke;

  if(argc<2 || argv[1].a_type!=A_FLOAT){
    x->num_outs=0;
  }else{
    x->num_outs=atom_getfloatarg(1,argc,argv);    
    x->outlets=calloc(sizeof(t_outlet*),x->num_outs);
  }

  if(argc<1  || argv[1].a_type!=A_FLOAT){
    x->num_ins=1;
  }else{
    x->num_ins=atom_getfloatarg(0,argc,argv);    
  }

  x->inlets=calloc(sizeof(t_inlet*),x->num_ins);
  x->values=calloc(sizeof(t_float),x->num_ins);

  for(lokke=1;lokke<x->num_ins;lokke++){
    x->inlets[lokke-1] = floatinlet_new(&x->x_ob, &x->values[lokke]);
  }

  for(lokke=0;lokke<x->num_outs;lokke++){
    x->outlets[lokke] = outlet_new(&x->x_ob, gensym("float"));
  }

}


/******************************/
/* Set default values for the inlets. */
/******************************/
static int k_cext_setdefaultvalues(t_k_cext *x,t_int argc, t_atom* argv){
  int i;
  for(i=2;i<argc;i++){
    char string[500];
    switch(argv[i].a_type){
    case A_FLOAT:
      x->values[i-2]=atom_getfloatarg(i,argc,argv);
      break;
    case A_SYMBOL:
      return i;
    }
  }
  return i;
}



#include "k_cext_generatecode.c"



static void *k_cextandfunc_new(t_symbol *s, t_int argc, t_atom* argv,bool iscext)
{
  char temp[500];
  int i;

  struct k_cext_init k;

  t_k_cext *x = (t_k_cext *)pd_new(k_cext_class);
  x->iscext=iscext;

  memset(&k,0,sizeof(struct k_cext_init));

  k_cext_makeinletsandoutlets(x,argc,argv);

  if(argv[2].a_type==A_FLOAT){
    i=k_cext_setdefaultvalues(x,argc,argv);
  }else{
    if(argv[1].a_type==A_FLOAT){
      i=2;
    }else{
      if(argv[0].a_type==A_FLOAT){
	i=1;
      }else{
	i=0;
      }
    }
  }

  k_sys_mktempfilename(x->filename);
  sprintf(k.name,"%s",x->filename);
  k.name[strlen(k.name)+2]=0;
  k.name[strlen(k.name)+1]='c';
  k.name[strlen(k.name)]='.';

  k.file=fopen(k.name,"w");
  //  post("name: %s\n",name)

  k_sys_writeincludes(k.file);



  k_cext_generatecode(x,argc,argv,i,&k);



  /*************************************/
  /* Compile and link                  */
  /*************************************/

  k_sys_makecompilestring(temp,k.name,k.funcname);
  post("Compiling %s",k.name);
  system(temp);

  sprintf(k.name,"%s.o",k.name);

  if(!k_sys_getprocessfunction(x,k.funcname,k.name)){
    FILE *printfile;
    post("Error in loader!");
    x->k_cext_process=NULL;
    k_cext_print(x);
    return NULL;
  }


  if(x->iscext==false){
    if(k.cfuncrettype==0){
      k_cext_addintfunc(k.cfuncname,x);
    }else{
      k_cext_addfloatfunc(k.cfuncname,x);
    }
  }

  return (void *)x;
}

static void *k_cext_new(t_symbol *s, t_int argc, t_atom* argv){
  return k_cextandfunc_new(s,argc,argv,true);
}

static void *k_cfunc_new(t_symbol *s, t_int argc, t_atom* argv){
  return k_cextandfunc_new(s,argc,argv,false);
}



static void k_cext_free(t_k_cext *x)
{
  char temp[500];

  if(x->iscext==false){
    k_cext_setdummy(x->k_cext_process);
  }

  if(x->handle!=NULL){
    k_sys_freehandle(x);
  }

  sprintf(temp,"%s.c",x->filename);
  k_sys_deletefile(temp);

  free(x->inlets);
  free(x->outlets);
  free(x->values);
}




void k_cext_setup(void)
{

  k_sys_init();

  /* k_cext */
  k_cext_class = class_new(gensym("k_cext"), (t_newmethod)k_cext_new,
			   (t_method)k_cext_free, sizeof(t_k_cext), 0, A_GIMME, 0);
  class_addfloat(k_cext_class, k_cext_float);
  class_addbang(k_cext_class, (t_method)k_cext_bang);
  class_addmethod(k_cext_class, (t_method)k_cext_print, gensym("print"), 0);
  class_sethelpsymbol(k_cext_class, gensym("help-k_cext.pd"));

  /* k_cfunc */
  k_cfunc_class = class_new(gensym("k_cfunc"), (t_newmethod)k_cfunc_new,
			   (t_method)k_cext_free, sizeof(t_k_cext), 0, A_GIMME, 0);

  // This does not work! Why? (Have to make workaround-code in k_cext_bang)
  class_addfloat(k_cfunc_class, k_cfunc_float);
  class_addmethod(k_cfunc_class, (t_method)k_cext_print, gensym("print"), 0);
  class_sethelpsymbol(k_cfunc_class, gensym("help-k_cfunc.pd")); 

 
  post(version);
}

void k_cfunc_setup(void){
  k_cext_setup();
}

/* TB: for accessing $0 values */
t_float k_cext_getvalue(char c[])
{
    while ( isspace(c[0]) )
    {
	c++;
    }
    return (*(value_get(gensym(c))));
}

int k_cext_setvalue(char c[],float f)
{
    while ( isspace(c[0]) )
    {
	c++;
    }
    return value_setfloat(gensym(c),f);
}

void k_cext_sendfloat (char c[],float f)
{
    static t_symbol *k_cext_internal_symbol=NULL;
    while ( isspace(c[0]) )
    {
	c++;
    }
    if(k_cext_internal_symbol==NULL) k_cext_internal_symbol=gensym(c);
    if(k_cext_internal_symbol->s_thing) 
	pd_float(k_cext_internal_symbol->s_thing, f); 
}

void k_cext_sendsymbol (char c[],char s[])
{
    static t_symbol *k_cext_internal_symbol=NULL;
    while ( isspace(c[0]) )
    {
	c++;
    }
    if(k_cext_internal_symbol==NULL) 
	k_cext_internal_symbol=gensym(c);
    if(k_cext_internal_symbol->s_thing) 
	pd_symbol(k_cext_internal_symbol->s_thing, gensym(s)); 
}