aboutsummaryrefslogtreecommitdiff
path: root/src/liststorage.c
blob: 7139cee85c3374125f1e96e7dab71ef4e4587f04 (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
/*
 * liststorage: stores a number of lists
 *
 * (c) 1999-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem)
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

/*
    this is heavily based on code from [textfile],
    which is part of pd and written by Miller S. Puckette
    pd (and thus [textfile]) come with their own license
*/

#include "zexy.h"

#ifdef __WIN32__
# include <io.h>
#else
# include <unistd.h>
#endif

#include <stdio.h>
#include <fcntl.h>
#include <string.h>



/* ****************************************************************************** */
/* liststorage : store several lists in a slots (array of lists of lists) */


/* a list of lists  */
typedef struct _msglist {
  int argc;
  t_atom *argv;
  struct _msglist *next;
} t_msglist;

typedef struct _liststorage {
  t_object x_obj;              /* everything */

  t_outlet*x_dataout; /* where the data appears */
  t_outlet*x_infoout; /* where meta-information appears */

  t_inlet*x_slotin;   /* setting the current slot */

  int x_numslots, x_defaultnumslots;
  int x_currentslot;

  t_msglist**x_slots;
} t_liststorage;

static t_class *liststorage_class;


/* ************************************************************************ */
/* helper functions                                                           */

static t_msglist*_liststorage_getslot(t_liststorage*x, int slot)
{
  //  post("getting slot %d of %d|%d", slot, 0, x->x_numslots);
  if(slot<0 || slot>=x->x_numslots) {
    pd_error(x, "[liststorage]: attempting to access invalid slot %d", slot);
    return NULL;
  }
  return x->x_slots[slot];
}

static void _liststorage_deletemsglist(t_msglist*list)
{
  t_msglist*x=list;
  while(x) {
    t_msglist*y=x;
    x=x->next;

    freebytes(y->argv, y->argc*sizeof(t_atom));
    y->argc=0;
    y->argv=NULL;
    y->next=NULL;
    freebytes(y, sizeof(t_msglist));
  }
}

static void _liststorage_deleteslot(t_liststorage*x, int slot)
{
  t_msglist*list=_liststorage_getslot(x, slot);
  if(list) {
    _liststorage_deletemsglist(list);
    x->x_slots[slot]=NULL;
  }
}

static t_msglist*_liststorage_newslot(int argc, t_atom*argv)
{
  t_msglist*slot=getbytes(sizeof(t_msglist));
  int i=0;

  slot->argv=getbytes(sizeof(t_atom)*argc);
  for(i=0; i<argc; i++) {
    slot->argv[i]=argv[i];
  }

  slot->argc=argc;
  slot->next=NULL;

  return slot;
}


static t_msglist*_liststorage_add2slot(t_msglist*slot, int argc,
                                       t_atom*argv)
{
  t_msglist*dummy=slot;
  t_msglist*newlist=_liststorage_newslot(argc, argv);
  if(NULL==slot) {
    //    post("no data yet: new data is %x", newlist);
    return newlist;
  }

  while(dummy->next) {
    dummy=dummy->next;
  }
  dummy->next=newlist;
  //  post("added data to slot @ %x", slot);
  return slot;
}


static int _liststorage_resize(t_liststorage*x, int size)
{
  t_msglist**newarray=NULL;
  int i=0;

  if(size<0) {
    pd_error(x,
             "[liststorage]: refusing to resize for negative amount of slots");
    return 0;
  }

  if(size==x->x_numslots) {
    verbose(1, "[liststorate] no need to resize array");
    return size;
  }

  /* create a new array */
  newarray=getbytes(sizeof(t_msglist*)*size);
  for(i=0; i<size; i++) {
    newarray[i]=NULL;
  }

  /* copy over all the data from the old array (if there was one */
  i=(size<x->x_numslots)?size:x->x_numslots;
  while(i-->0) {
    newarray[i]=x->x_slots[i];
    x->x_slots[i]=NULL;
  }

  /* delete the old array */
  for(i=0; i<x->x_numslots; i++) {
    _liststorage_deleteslot(x, i);
  }
  freebytes(x->x_slots, sizeof(t_msglist*));

  /* make the new array the current */
  x->x_slots=newarray;
  x->x_numslots=size;

  return size;
}

static int _liststorage_checkslot(t_liststorage*x, const char*string,
                                  const int resize)
{
  int slot=x->x_currentslot;
  t_atom atom;
  SETFLOAT(&atom, (t_float)slot);

  if(slot<0) {
    if(NULL!=string) {
      pd_error(x, "[liststorage]: %s %d", string, slot);
    }
    outlet_anything(x->x_infoout, gensym("invalidslot"), 1, &atom);
    return -1;
  }
  if(slot>=x->x_numslots) {
    if(resize) {
      _liststorage_resize(x, slot+1);
    } else {
      if(NULL!=string) {
        pd_error(x, "[liststorage]: %s %d", string, slot);
      }
      outlet_anything(x->x_infoout, gensym("invalidslot"), 1, &atom);
      return -1;
    }
  }
  return slot;
}
/* ************************************************************************ */
/* object methods                                                           */

/* recall all lists from the current slot */
static void liststorage_bang(t_liststorage *x)
{
  t_atom atom;
  t_msglist*list=NULL;

  int slot=_liststorage_checkslot(x,
                                  "attempting to read data from invalid slot", 0);
  if(slot<0) {
    return;
  }
  list=_liststorage_getslot(x, slot);

  while(list) {
    outlet_list(x->x_dataout, gensym("list"), list->argc, list->argv);
    list=list->next;
  }
  SETFLOAT(&atom, (t_float)slot);

  /* no need for done: use [t b b b] to get beginning and end of output */
  //  outlet_anything(x->x_infoout, gensym("done"), 1, &atom);
}

/* add a new list to the current slot */
static void liststorage_add(t_liststorage *x, t_symbol *s, int ac,
                            t_atom *av)
{
  t_msglist*list=NULL;
  int slot=_liststorage_checkslot(x,
                                  "attempting to add data to invalid slot", 1);
  if(slot<0) {
    return;
  }
  list=_liststorage_getslot(x, slot);
  x->x_slots[slot]=_liststorage_add2slot(x->x_slots[slot], ac, av);
}

/* clear the current slot */
static void liststorage_clear(t_liststorage *x)
{
  int slot=_liststorage_checkslot(x, "attempting to clear invalid slot", 0);
  if(slot<0) {
    return;
  }

  _liststorage_deleteslot(x, slot);
}

/* clear all slots */
static void liststorage_clearall(t_liststorage *x)
{
  int i=0;
  for(i=0; i<x->x_numslots; i++) {
    _liststorage_deleteslot(x, i);
  }
}

/* insert an empty slot at (before) given position */
static void liststorage_insert(t_liststorage *x, t_floatarg f)
{
  int current=x->x_currentslot;
  int slot=-1;
  int i=0;

  x->x_currentslot=f;
  slot=_liststorage_checkslot(x, "attempting to insert invalid slot", 1);
  x->x_currentslot=current;

  if(slot<0) {
    return;
  }

  _liststorage_resize(x, x->x_numslots+1);

  for(i=x->x_numslots-1; i>slot; i--) {
    x->x_slots[i]=x->x_slots[i-1];
  }
  x->x_slots[slot]=NULL;
}

/* get the number of slots */
static void liststorage_info(t_liststorage *x)
{
  t_atom ap;
  SETFLOAT(&ap, (t_float)x->x_numslots);
  outlet_anything(x->x_infoout, gensym("numslots"), 1, &ap);
}


/* get the number of slots */
static void liststorage_slot(t_liststorage *x, t_floatarg f)
{
  int slot=f;
  x->x_currentslot=slot;

}


/* remove empty slots */
static void liststorage_compress(t_liststorage *x)
{
  t_msglist**newarray=NULL;
  int i=0, j=0;
  int size=0;
  for(i=0; i<x->x_numslots; i++) {
    if(NULL!=x->x_slots[i]) {

      size++;
    }
  }

  if(size>=x->x_numslots) {
    //    post("incomressible: %d of %d", size, x->x_numslots);
    return;
  }

  if(size<x->x_defaultnumslots) {
    size=x->x_defaultnumslots;
  }

  /* create a new array */
  newarray=getbytes(sizeof(t_msglist*)*size);
  for(i=0; i<size; i++) {
    newarray[i]=NULL;
  }

  /* copy over all the data from the old array (if there was one */
  for(i=0, j=0; i<x->x_numslots; i++) {
    if(NULL!=x->x_slots[i]) {
      newarray[j]=x->x_slots[i];
      j++;
    }
    x->x_slots[i]=NULL;
  }

  /* delete the old array */
  for(i=0; i<x->x_numslots; i++) {
    _liststorage_deleteslot(x, i);
  }
  freebytes(x->x_slots, sizeof(t_msglist*));

  /* make the new array the current */
  x->x_slots=newarray;
  x->x_numslots=size;
}


/* ************************************************************************ */
/* constructor/destructor                                                   */

static void liststorage_free(t_liststorage *x)
{
  liststorage_clearall(x);
  _liststorage_resize(x, 0);
}

/* constructor: argument is initial number of slots (can grow) */
static void *liststorage_new(t_floatarg f)
{
  t_liststorage *x = (t_liststorage *)pd_new(liststorage_class);
  int slots=f;

  x->x_slotin=inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"),
                        gensym("slot"));
  x->x_dataout=outlet_new(&x->x_obj, gensym("list"));
  x->x_infoout=outlet_new(&x->x_obj, 0);



  if(slots<=0) {
    slots=20;
  }
  x->x_defaultnumslots=slots;
  x->x_numslots=0;
  x->x_currentslot=0;
  x->x_slots=NULL;

  _liststorage_resize(x, x->x_defaultnumslots);



  return (x);
}

void liststorage_setup(void)
{
  liststorage_class = class_new(gensym("liststorage"),
                                (t_newmethod)liststorage_new,
                                (t_method)liststorage_free, sizeof(t_liststorage), 0, A_DEFFLOAT, 0);

  /* recall all lists from the current slot */
  class_addbang(liststorage_class, (t_method)liststorage_bang);

  /* add a new list to the current slot */
  class_addmethod(liststorage_class, (t_method)liststorage_add,
                  gensym("add"), A_GIMME, 0);
  /* clear the current slot */
  class_addmethod(liststorage_class, (t_method)liststorage_clear,
                  gensym("clear"), 0);
  /* clear all slots */
  class_addmethod(liststorage_class, (t_method)liststorage_clearall,
                  gensym("clearall"), 0);


  /* add a new list to the current slot */
  class_addmethod(liststorage_class, (t_method)liststorage_slot,
                  gensym("slot"), A_FLOAT, 0);


  /* insert an empty slot at (before) given position */
  class_addmethod(liststorage_class, (t_method)liststorage_insert,
                  gensym("insert"), A_DEFFLOAT, 0);

  /* remove empty slots */
  class_addmethod(liststorage_class, (t_method)liststorage_compress,
                  gensym("compress"), 0);


  /* get the number of slots */
  class_addmethod(liststorage_class, (t_method)liststorage_info,
                  gensym("info"), 0);

  zexy_register("liststorage");
}