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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
|
/*
pool - hierarchical storage object for PD and Max/MSP
Copyright (c) 2002 Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.
*/
#include "pool.h"
#include <string.h>
#include <fstream.h>
#include <ctype.h>
#include <stdlib.h>
inline I compare(I a,I b) { return a == b?0:(a < b?-1:1); }
inline I compare(F a,F b) { return a == b?0:(a < b?-1:1); }
static I compare(const S *a,const S *b)
{
if(a == b)
return 0;
else
return strcmp(flext::GetString(a),flext::GetString(b));
}
static I compare(const A &a,const A &b)
{
if(a.a_type == b.a_type) {
switch(a.a_type) {
case A_FLOAT:
return compare(a.a_w.w_float,b.a_w.w_float);
#ifdef MAXMSP
case A_LONG:
return compare((I)a.a_w.w_long,(I)b.a_w.w_long);
#endif
case A_SYMBOL:
return compare(a.a_w.w_symbol,b.a_w.w_symbol);
#ifdef PD
case A_POINTER:
return a.a_w.w_gpointer == b.a_w.w_gpointer?0:(a.a_w.w_gpointer < b.a_w.w_gpointer?-1:1);
#endif
default:
LOG("pool - atom comparison: type not handled");
return -1;
}
}
else
return a.a_type < b.a_type?-1:1;
}
poolval::poolval(const A &k,AtomList *d):
data(d),nxt(NULL)
{
SetAtom(key,k);
}
poolval::~poolval()
{
if(data) delete data;
if(nxt) delete nxt;
}
poolval &poolval::Set(AtomList *d)
{
if(data) delete data;
data = d;
return *this;
}
poolval *poolval::Dup() const
{
return new poolval(key,data?new AtomList(*data):NULL);
}
pooldir::pooldir(const A &d):
dirs(NULL),vals(NULL),nxt(NULL)
{
CopyAtom(&dir,&d);
}
pooldir::~pooldir()
{
Clear(true);
if(nxt) delete nxt;
}
V pooldir::Clear(BL rec,BL dironly)
{
if(rec && dirs) { delete dirs; dirs = NULL; }
if(!dironly && vals) { delete vals; vals = NULL; }
}
pooldir *pooldir::AddDir(I argc,const A *argv)
{
if(!argc) return this;
I c = 1;
pooldir *prv = NULL,*ix = dirs;
for(; ix; prv = ix,ix = ix->nxt) {
c = compare(argv[0],ix->dir);
if(c <= 0) break;
}
if(c || !ix) {
pooldir *nd = new pooldir(argv[0]);
nd->nxt = ix;
if(prv) prv->nxt = nd;
else dirs = nd;
ix = nd;
}
return ix->AddDir(argc-1,argv+1);
}
pooldir *pooldir::GetDir(I argc,const A *argv,BL rmv)
{
if(!argc) return this;
I c = 1;
pooldir *prv = NULL,*ix = dirs;
for(; ix; prv = ix,ix = ix->nxt) {
c = compare(argv[0],ix->dir);
if(c <= 0) break;
}
if(c || !ix)
return NULL;
else {
if(argc > 1)
return ix->GetDir(argc-1,argv+1,rmv);
else if(rmv) {
pooldir *nd = ix->nxt;
if(prv) prv->nxt = nd;
else dirs = nd;
ix->nxt = NULL;
return ix;
}
else
return ix;
}
}
BL pooldir::DelDir(const AtomList &d)
{
pooldir *pd = GetDir(d,true);
if(pd && pd != this) {
delete pd;
return true;
}
else
return false;
}
V pooldir::SetVal(const A &key,AtomList *data,BL over)
{
I c = 1;
poolval *prv = NULL,*ix = vals;
for(; ix; prv = ix,ix = ix->nxt) {
c = compare(key,ix->key);
if(c <= 0) break;
}
if(c || !ix) {
// no existing data found
if(data) {
poolval *nv = new poolval(key,data);
nv->nxt = ix;
if(prv) prv->nxt = nv;
else vals = nv;
}
}
else if(over) {
// data exists... only set if overwriting enabled
if(data)
ix->Set(data);
else {
poolval *nv = ix->nxt;
if(prv) prv->nxt = nv;
else vals = nv;
ix->nxt = NULL;
delete ix;
}
}
}
flext::AtomList *pooldir::GetVal(const A &key,BL cut)
{
I c = 1;
poolval *prv = NULL,*ix = vals;
for(; ix; prv = ix,ix = ix->nxt) {
c = compare(key,ix->key);
if(c <= 0) break;
}
if(c || !ix)
return NULL;
else {
AtomList *ret;
if(cut) {
poolval *nv = ix->nxt;
if(prv) prv->nxt = nv;
else vals = nv;
ix->nxt = NULL;
ret = ix->data; ix->data = NULL;
delete ix;
}
else
ret = new AtomList(*ix->data);
return ret;
}
}
I pooldir::CntAll()
{
I cnt = 0;
poolval *ix = vals;
for(; ix; ix = ix->nxt,++cnt) {}
return cnt;
}
I pooldir::GetAll(A *&keys,AtomList *&lst,BL cut)
{
I cnt = CntAll();
keys = new A[cnt];
lst = new AtomList[cnt];
poolval *ix = vals;
for(I i = 0; ix; ++i) {
SetAtom(keys[i],ix->key);
lst[i] = *ix->data;
if(cut) {
poolval *t = ix;
vals = ix = ix->nxt;
t->nxt = NULL; delete t;
}
else
ix = ix->nxt;
}
return cnt;
}
I pooldir::GetSub(const A **&lst)
{
I cnt = 0;
pooldir *ix = dirs;
for(; ix; ix = ix->nxt,++cnt) {}
lst = new const A *[cnt];
ix = dirs;
for(I i = 0; ix; ix = ix->nxt,++i) {
lst[i] = &ix->dir;
}
return cnt;
}
BL pooldir::Paste(const pooldir *p,I depth,BL repl,BL mkdir)
{
BL ok = true;
for(poolval *ix = p->vals; ix; ix = ix->nxt) {
SetVal(ix->key,new AtomList(*ix->data),repl);
}
if(ok && depth) {
for(pooldir *dix = p->dirs; ok && dix; dix = dix->nxt) {
pooldir *ndir = mkdir?AddDir(1,&dix->dir):GetDir(1,&dix->dir);
if(ndir) {
ok = ndir->Paste(dix,depth > 0?depth-1:depth,repl,mkdir);
}
}
}
return ok;
}
BL pooldir::Copy(pooldir *p,I depth,BL cut)
{
BL ok = true;
if(cut) {
if(p->vals)
ok = false;
else
p->vals = vals, vals = NULL;
}
else {
// inefficient!! p->SetVal has to search through list unnecessarily!!
for(poolval *ix = vals; ix; ix = ix->nxt) {
p->SetVal(ix->key,new AtomList(*ix->data));
}
}
if(ok && depth) {
// also quite inefficient for cut
for(pooldir *dix = dirs; ok && dix; dix = dix->nxt) {
pooldir *ndir = p->AddDir(1,&dix->dir);
if(ndir)
ok = ndir->Copy(dix,depth > 0?depth-1:depth,cut);
else
ok = false;
}
}
return ok;
}
static C *ReadAtom(C *c,A *a)
{
// skip whitespace
while(*c && isspace(*c)) ++c;
if(!*c) return NULL;
const C *m = c; // remember position
// check for word type (s = 0,1,2 ... int,float,symbol)
I s = 0;
for(; *c && !isspace(*c); ++c) {
if(!isdigit(*c))
s = (*c != '.' || s == 1)?2:1;
}
if(a) {
switch(s) {
case 0: // integer
#ifdef MAXMSP
a->a_type = A_LONG;
a->a_w.w_long = atol(m);
break;
#endif
case 1: // float
a->a_type = A_FLOAT;
a->a_w.w_float = (F)atof(m);
break;
default: { // anything else is a symbol
C t = *c; *c = 0;
a->a_type = A_SYMBOL;
a->a_w.w_symbol = (S *)flext::MakeSymbol(m);
*c = t;
break;
}
}
}
return c;
}
static BL ReadAtoms(istream &is,flext::AtomList &l,C del)
{
C tmp[1024];
is.getline(tmp,sizeof tmp,del);
if(is.eof() || !is.good()) return false;
I i,cnt;
C *t = tmp;
for(cnt = 0; ; ++cnt) {
t = ReadAtom(t,NULL);
if(!t) break;
}
l(cnt);
if(cnt) {
for(i = 0,t = tmp; i < cnt; ++i)
t = ReadAtom(t,&l[i]);
}
return true;
}
static V WriteAtom(ostream &os,const A &a)
{
switch(a.a_type) {
case A_FLOAT:
os << a.a_w.w_float;
break;
#ifdef MAXMSP
case A_LONG:
os << a.a_w.w_long;
break;
#endif
case A_SYMBOL:
os << flext::GetString(a.a_w.w_symbol);
break;
}
}
static V WriteAtoms(ostream &os,const flext::AtomList &l)
{
for(I i = 0; i < l.Count(); ++i) {
WriteAtom(os,l[i]);
os << ' ';
}
}
BL pooldir::LdDir(istream &is,I depth,BL mkdir)
{
BL r;
for(I i = 1; !is.eof(); ++i) {
AtomList d,k,*v = new AtomList;
r = ReadAtoms(is,d,',');
r = r && ReadAtoms(is,k,',') && k.Count() == 1;
r = r && ReadAtoms(is,*v,'\n') && v->Count();
if(r) {
if(depth < 0 || d.Count() <= depth) {
pooldir *nd = mkdir?AddDir(d):GetDir(d);
if(nd) {
nd->SetVal(k[0],v); v = NULL;
}
#ifdef _DEBUG
else
post("pool - directory was not found",i);
#endif
}
}
else if(!is.eof())
post("pool - format mismatch encountered, skipped line %i",i);
if(v) delete v;
}
return true;
}
BL pooldir::SvDir(ostream &os,I depth,const AtomList &dir)
{
{
for(poolval *ix = vals; ix; ix = ix->nxt) {
WriteAtoms(os,dir);
os << ", ";
WriteAtom(os,ix->key);
os << " , ";
WriteAtoms(os,*ix->data);
os << endl;
}
}
if(depth) {
I nd = depth > 0?depth-1:-1;
for(pooldir *ix = dirs; ix; ix = ix->nxt) {
ix->SvDir(os,nd,AtomList(dir).Append(ix->dir));
}
}
return true;
}
pooldata::pooldata(const S *s):
sym(s),nxt(NULL),refs(0),
root(nullatom)
{
LOG1("new pool %s",sym?flext_base::GetString(sym):"<private>");
}
pooldata::~pooldata()
{
LOG1("free pool %s",sym?flext_base::GetString(sym):"<private>");
}
t_atom pooldata::nullatom = { A_NULL };
V pooldata::Reset()
{
root.Clear(true);
}
BL pooldata::MkDir(const AtomList &d)
{
root.AddDir(d);
return true;
}
BL pooldata::ChkDir(const AtomList &d)
{
return root.GetDir(d) != NULL;
}
BL pooldata::RmDir(const AtomList &d)
{
return root.DelDir(d);
}
BL pooldata::Set(const AtomList &d,const A &key,AtomList *data,BL over)
{
pooldir *pd = root.GetDir(d);
if(!pd) return false;
pd->SetVal(key,data,over);
return true;
}
BL pooldata::Clr(const AtomList &d,const A &key)
{
pooldir *pd = root.GetDir(d);
if(!pd) return false;
pd->ClrVal(key);
return true;
}
BL pooldata::ClrAll(const AtomList &d,BL rec,BL dironly)
{
pooldir *pd = root.GetDir(d);
if(!pd) return false;
pd->Clear(rec,dironly);
return true;
}
flext::AtomList *pooldata::Get(const AtomList &d,const A &key)
{
pooldir *pd = root.GetDir(d);
return pd?pd->GetVal(key):NULL;
}
I pooldata::CntAll(const AtomList &d)
{
pooldir *pd = root.GetDir(d);
return pd?pd->CntAll():0;
}
I pooldata::GetAll(const AtomList &d,A *&keys,AtomList *&lst)
{
pooldir *pd = root.GetDir(d);
if(pd)
return pd->GetAll(keys,lst);
else {
keys = NULL; lst = NULL;
return 0;
}
}
I pooldata::GetSub(const AtomList &d,const t_atom **&dirs)
{
pooldir *pd = root.GetDir(d);
if(pd)
return pd->GetSub(dirs);
else {
dirs = NULL;
return 0;
}
}
BL pooldata::Paste(const AtomList &d,const pooldir *clip,I depth,BL repl,BL mkdir)
{
pooldir *pd = root.GetDir(d);
if(pd)
return pd->Paste(clip,depth,repl,mkdir);
else
return false;
}
pooldir *pooldata::Copy(const AtomList &d,const A &key,BL cut)
{
pooldir *pd = root.GetDir(d);
if(pd) {
AtomList *val = pd->GetVal(key,cut);
if(val) {
pooldir *ret = new pooldir(nullatom);
ret->SetVal(key,val);
return ret;
}
else
return NULL;
}
else
return NULL;
}
pooldir *pooldata::CopyAll(const AtomList &d,I depth,BL cut)
{
pooldir *pd = root.GetDir(d);
if(pd) {
pooldir *ret = new pooldir(nullatom);
if(pd->Copy(ret,depth,cut))
return ret;
else {
delete ret;
return NULL;
}
}
else
return NULL;
}
static const C *CnvFlnm(C *dst,const C *src,I sz)
{
#if defined(PD) && defined(NT)
I cnt = strlen(src);
if(cnt >= sz-1) return NULL;
for(I i = 0; i < cnt; ++i)
dst[i] = src[i] != '/'?src[i]:'\\';
dst[i] = 0;
return dst;
#else
return src;
#endif
}
BL pooldata::LdDir(const AtomList &d,const C *flnm,I depth,BL mkdir)
{
pooldir *pd = root.GetDir(d);
if(pd) {
C tmp[1024];
const C *t = CnvFlnm(tmp,flnm,sizeof tmp);
if(t) {
ifstream fl(t);
return fl.good() && pd->LdDir(fl,depth,mkdir);
}
else return false;
}
else
return false;
}
BL pooldata::SvDir(const AtomList &d,const C *flnm,I depth,BL absdir)
{
pooldir *pd = root.GetDir(d);
if(pd) {
C tmp[1024];
const C *t = CnvFlnm(tmp,flnm,sizeof tmp);
if(t) {
ofstream fl(t);
return fl.good() && pd->SvDir(fl,depth,absdir?d:AtomList());
}
else return false;
}
else
return false;
}
|