aboutsummaryrefslogtreecommitdiff
path: root/gfsm/src/pd_alphabet.c
blob: 0fb86f0e51b13485845dac92f1bd4f5a5445c108 (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*=============================================================================*\
 * File: pd_alphabet.c
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: finite state automata for Pd: alphabet
 *
 * Copyright (c) 2004 Bryan Jurish.
 *
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
 *
 * 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.
 *
 * See file LICENSE for further informations on licensing terms.
 *
 * 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.
 *=============================================================================*/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <pd_alphabet.h>
#include <pd_io.h>
#include <string.h>

/*=====================================================================
 * DEBUG
 *=====================================================================*/
//#define ALPHABET_DEBUG

/*=====================================================================
 * Pd Constants
 *=====================================================================*/
static t_class *pd_gfsm_alphabet_pd_class;
static t_class *pd_gfsm_alphabet_obj_class;

/*=====================================================================
 * pd_gfsm_alphabet_pd: Methods
 *=====================================================================*/

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_pd: new
 */
static void *pd_gfsm_alphabet_pd_new(t_symbol *name)
{
    t_pd_gfsm_alphabet_pd *x = (t_pd_gfsm_alphabet_pd *)pd_new(pd_gfsm_alphabet_pd_class);

#ifdef ALPHABET_DEBUG
    post("pd_gfsm_alphabet_pd_new() called ; name=%s ; x=%p",
	 (name ? name->s_name : "(null)"),
	 x);
#endif

    //-- defaults
    x->x_refcnt   = 0;
    x->x_name     = name;
    x->x_alphabet = (gfsmAlphabet*)gfsm_pd_atom_alphabet_new();
    
    //-- bindings
    if (name != &s_) pd_bind((t_pd*)x, name);
    
    return (void *)x;
}


/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_pd: free
 */
static void pd_gfsm_alphabet_pd_free(t_pd_gfsm_alphabet_pd *x)
{
#ifdef ALPHABET_DEBUG
  post("pd_gfsm_alphabet_pd_free() called ; x=%p", x);
#endif

  if (x->x_alphabet) gfsm_pd_atom_alphabet_free((gfsmPdAtomAlphabet*)(x->x_alphabet));

  /* unbind the symbol of the name of hashtable in pd's global namespace */
  if (x->x_name != &s_) pd_unbind((t_pd*)x, x->x_name);
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_pd: find
 */
t_pd_gfsm_alphabet_pd *pd_gfsm_alphabet_pd_find(t_symbol *name)
{
  if (name != &s_)
    return (t_pd_gfsm_alphabet_pd*)pd_findbyclass(name,pd_gfsm_alphabet_pd_class);
  return NULL;
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_pd: get
 */
t_pd_gfsm_alphabet_pd *pd_gfsm_alphabet_pd_get(t_symbol *name)
{
  t_pd_gfsm_alphabet_pd *x = pd_gfsm_alphabet_pd_find(name);
  if (!x) {
    x = (t_pd_gfsm_alphabet_pd*)pd_gfsm_alphabet_pd_new(name);
    x->x_refcnt = 0;
  }
  return x;
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_pd: release
 */
void pd_gfsm_alphabet_pd_release(t_pd_gfsm_alphabet_pd *x)
{
  if (x) {
    if (x->x_refcnt) --x->x_refcnt;
    if (!x->x_refcnt) pd_gfsm_alphabet_pd_free(x);
  }
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_pd: setup
 */
void pd_gfsm_alphabet_pd_setup(void)
{
  //-- class
  pd_gfsm_alphabet_pd_class = class_new(gensym("gfsm_alphabet_pd"),
					(t_newmethod)pd_gfsm_alphabet_pd_new,
					(t_method)pd_gfsm_alphabet_pd_free,
					sizeof(t_pd_gfsm_alphabet_pd),
					CLASS_PD,
					A_DEFSYM,
					A_NULL);
}


/*=====================================================================
 * pd_gfsm_alphabet: Methods
 *=====================================================================*/

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: new
 */
static void *pd_gfsm_alphabet_obj_new(t_symbol *name)
{
  t_pd_gfsm_alphabet_obj *x = (t_pd_gfsm_alphabet_obj *)pd_new(pd_gfsm_alphabet_obj_class);

#ifdef ALPHABET_DEBUG
  post("pd_gfsm_alphabet_obj_new() called: name=%s ; x=%p",
       (name ? name->s_name : "(null)"),
       x);
#endif

    //-- defaults
    x->x_alphabet_pd = NULL;

    //-- bindings
    x->x_alphabet_pd = pd_gfsm_alphabet_pd_get(name);
    x->x_alphabet_pd->x_refcnt++;

    //-- outlets
    x->x_labout = outlet_new(&x->x_obj, &s_float);     //-- label / "char" outlet
    x->x_keyout = outlet_new(&x->x_obj, &s_anything);  //-- atom outlet

    return (void *)x;
}


/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: free
 */
static void pd_gfsm_alphabet_obj_free(t_pd_gfsm_alphabet_obj *x)
{
#ifdef ALPHABET_DEBUG
  post("pd_gfsm_alphabet_obj_free() called: x=%p", x);
#endif

  pd_gfsm_alphabet_pd_release(x->x_alphabet_pd);

  //-- do we need to do this?
  outlet_free(x->x_labout);
  outlet_free(x->x_keyout);
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: set
 */
static void pd_gfsm_alphabet_obj_alphabet(t_pd_gfsm_alphabet_obj *x, t_symbol *name)
{
#ifdef ALPHABET_DEBUG
  post("pd_gfsm_alphabet_obj_alphabet() called ; oldname=%p=%s ; newname=%p=%s",
       x->x_alphabet_pd->x_name, x->x_alphabet_pd->x_name->s_name, name, name->s_name);
#endif
  if (name == x->x_alphabet_pd->x_name) return;
  pd_gfsm_alphabet_pd_release(x->x_alphabet_pd);

  x->x_alphabet_pd = pd_gfsm_alphabet_pd_get(name);
  ++x->x_alphabet_pd->x_refcnt;
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: load
 */
static void pd_gfsm_alphabet_obj_load(t_pd_gfsm_alphabet_obj *x, t_symbol *s)
{
  gfsmError *errp = NULL;
  gfsm_alphabet_load_filename(x->x_alphabet_pd->x_alphabet, s->s_name, &errp);
  if (errp != NULL) {
    error("gfsm_alphabet: load %s: %s", s->s_name, errp->message);
    g_error_free(errp);
  }
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: print
 */
static void pd_gfsm_alphabet_obj_print(t_pd_gfsm_alphabet_obj *x)
{
  gfsmError    *errp = NULL;
  gfsmIOHandle *ioh  = pd_gfsm_console_handle_new();
  gfsm_alphabet_save_handle(x->x_alphabet_pd->x_alphabet, ioh, &errp);
  if (errp != NULL) {
    error("gfsm_alphabet: print: %s", errp->message);
    g_error_free(errp);
  }
  pd_gfsm_console_handle_free(ioh);
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: save
 */
static void pd_gfsm_alphabet_obj_save(t_pd_gfsm_alphabet_obj *x, t_symbol *s)
{
  gfsmError *errp = NULL;
  gfsm_alphabet_save_filename(x->x_alphabet_pd->x_alphabet, s->s_name, &errp);
  if (errp != NULL) {
    error("gfsm_alphabet: save %s: %s", s->s_name, errp->message);
    g_error_free(errp);
  }
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: clear
 */
static void pd_gfsm_alphabet_obj_clear(t_pd_gfsm_alphabet_obj *x)
{
  gfsm_alphabet_clear(x->x_alphabet_pd->x_alphabet);
}


/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: outlet_pair
 */
static void pd_gfsm_alphabet_obj_outlet_pair(t_pd_gfsm_alphabet_obj *x, t_float lab, t_atom *a)
{
  if (a) {
    switch (a->a_type) {
    case A_SYMBOL:
      outlet_symbol(x->x_keyout, a->a_w.w_symbol);
      break;
    case A_FLOAT:
      outlet_float(x->x_keyout, a->a_w.w_float);
      break;
    default:
      outlet_symbol(x->x_keyout, atom_getsymbol(a));
      break;
    }
  } else {
    outlet_bang(x->x_keyout);
  }

  if (lab != gfsmNoLabel) outlet_float(x->x_obj.ob_outlet, lab);
  else                    outlet_bang(x->x_obj.ob_outlet);
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: insert
 */
static void pd_gfsm_alphabet_obj_insert(t_pd_gfsm_alphabet_obj *x, GFSM_UNUSED t_symbol *s, int argc, t_atom *argv)
{
  if (argc < 1) {
    error("pd_gfsm_alphabet_obj_insert(): no atom to insert?");
    return;
  }
  t_float labf = argc > 1 ? atom_getfloat(argv+1) : (t_float)gfsmNoLabel;

  gfsm_alphabet_get_full(x->x_alphabet_pd->x_alphabet, argv, (gfsmLabelVal)labf);
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: atom2label
 */
static void pd_gfsm_alphabet_obj_atom2label(t_pd_gfsm_alphabet_obj *x, GFSM_UNUSED t_symbol *s, int argc, t_atom *argv)
{
  if (argc < 1) {
    error("pd_gfsm_alphabet_obj_atom2label(): no arguments?");
    return;
  }
  t_float labf = (t_float)(gfsm_alphabet_find_label(x->x_alphabet_pd->x_alphabet, argv));

  pd_gfsm_alphabet_obj_outlet_pair(x, labf, argv);
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: atom2label_force
 */
static void pd_gfsm_alphabet_obj_atom2label_force(t_pd_gfsm_alphabet_obj *x, GFSM_UNUSED t_symbol *s, int argc, t_atom *argv)
{
  if (argc < 1) {
    error("pd_gfsm_alphabet_obj_atom2label_force(): no arguments?");
    return;
  }
  t_float labf = (t_float)(gfsm_alphabet_get_label(x->x_alphabet_pd->x_alphabet, argv));

  pd_gfsm_alphabet_obj_outlet_pair(x, labf, argv);
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: label2atom
 */
static void pd_gfsm_alphabet_obj_label2atom(t_pd_gfsm_alphabet_obj *x, t_float labf)
{
  t_atom *key = (t_atom*)gfsm_alphabet_find_key(x->x_alphabet_pd->x_alphabet, (gfsmLabelVal)labf);

  if (key==NULL) pd_gfsm_alphabet_obj_outlet_pair(x, labf, NULL);
  else           pd_gfsm_alphabet_obj_outlet_pair(x, labf, key);
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: label2atom_force
 */
static void pd_gfsm_alphabet_obj_label2atom_force(t_pd_gfsm_alphabet_obj *x, t_float labf)
{
  t_atom *key = (t_atom*)gfsm_alphabet_find_key(x->x_alphabet_pd->x_alphabet,
						(gfsmLabelVal)labf);
  if (key==NULL) {
    t_atom labatom;
    SETFLOAT(&labatom, labf);
    gfsm_alphabet_insert(x->x_alphabet_pd->x_alphabet, &labatom, (gfsmLabelVal)labf);
    pd_gfsm_alphabet_obj_outlet_pair(x, labf, &labatom);
  }
  else {
    pd_gfsm_alphabet_obj_outlet_pair(x, labf, key);
  }
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: rmatom()
 */
static void pd_gfsm_alphabet_obj_rmatom(t_pd_gfsm_alphabet_obj *x, GFSM_UNUSED t_symbol *sel, int argc, t_atom *argv)
{
  if (argc < 1) return;
  gfsm_alphabet_remove_key(x->x_alphabet_pd->x_alphabet, argv);
}

/*--------------------------------------------------------------------
 * pd_gfsm_alphabet_obj: rmlabel()
 */
static void pd_gfsm_alphabet_obj_rmlabel(t_pd_gfsm_alphabet_obj *x, t_float labf)
{
  gfsm_alphabet_remove_label(x->x_alphabet_pd->x_alphabet, (gfsmLabelVal)labf);
}


/*--------------------------------------------------------------------
 * pd_gfsm_alphabet: setup
 */
void pd_gfsm_alphabet_setup(void)
{
  //-- pd_gfsm_alphabet_pd
  pd_gfsm_alphabet_pd_setup();

  //-- class
  pd_gfsm_alphabet_obj_class = class_new(gensym("gfsm_alphabet"),
			     (t_newmethod)pd_gfsm_alphabet_obj_new,
			     (t_method)pd_gfsm_alphabet_obj_free,
			     sizeof(t_pd_gfsm_alphabet_obj),
			     CLASS_DEFAULT,
			     A_DEFSYM,
			     A_NULL);

  //-- methods: I/O
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_print,
		  gensym("print"),
		  A_NULL);
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_load,
		  gensym("load"),
		  A_SYMBOL,
		  A_NULL);
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_save,
		  gensym("save"),
		  A_SYMBOL,
		  A_NULL);


  //-- methods: whole-object manipulation
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_clear,
		  gensym("clear"),
		  A_NULL);
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_alphabet,
		  gensym("alphabet"),
		  A_DEFSYM,
		  A_NULL);


  //-- methods: insert
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_insert,
		  gensym("insert"),
		  A_GIMME,
		  A_NULL);

  //-- methods: safe access: atom->label
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_atom2label,
		  gensym("atom2char"),
		  A_GIMME,
		  A_NULL);
  /*
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_atom2label,
		  gensym("a2c"),
		  A_GIMME,
		  A_NULL);
  */

  //-- methods: destructive access: atom->label
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_atom2label_force,
		  gensym("atom2char!"),
		  A_GIMME,
		  A_NULL);
  /*
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_atom2label_force,
		  gensym("a2c!"),
		  A_GIMME,
		  A_NULL);
  */


  //-- methods: safe access: label->atom
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_label2atom,
		  gensym("char2atom"),
		  A_FLOAT,
		  A_NULL);
  /*
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_label2atom,
		  gensym("c2a"),
		  A_FLOAT,
		  A_NULL);
  */

  //-- methods: destructive access: label->atom
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_label2atom_force,
		  gensym("char2atom!"),
		  A_FLOAT,
		  A_NULL);
  /*
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_label2atom_force,
		  gensym("c2a!"),
		  A_FLOAT,
		  A_NULL);
  */

  //-- methods: removal
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_rmatom,
		  gensym("rmatom"),
		  A_GIMME,
		  A_NULL);
  class_addmethod(pd_gfsm_alphabet_obj_class,
		  (t_method)pd_gfsm_alphabet_obj_rmlabel,
		  gensym("rmchar"),
		  A_FLOAT,
		  A_NULL);
  

  //-- help symbol
  class_sethelpsymbol(pd_gfsm_alphabet_obj_class, gensym("gfsm_alphabet-help.pd"));
}