From 69487c956975fb9dc0587fb22fc20d6ca62ec08d Mon Sep 17 00:00:00 2001 From: Thomas Grill Date: Thu, 8 Jan 2004 03:39:00 +0000 Subject: "" svn path=/trunk/; revision=1252 --- externals/grill/pool/source/data.cpp | 294 +++++++++ externals/grill/pool/source/main.cpp | 1152 ++++++++++++++++++++++++++++++++++ externals/grill/pool/source/pool.cpp | 883 ++++++++++++++++++++++++++ externals/grill/pool/source/pool.h | 164 +++++ 4 files changed, 2493 insertions(+) create mode 100644 externals/grill/pool/source/data.cpp create mode 100644 externals/grill/pool/source/main.cpp create mode 100644 externals/grill/pool/source/pool.cpp create mode 100644 externals/grill/pool/source/pool.h (limited to 'externals/grill/pool/source') diff --git a/externals/grill/pool/source/data.cpp b/externals/grill/pool/source/data.cpp new file mode 100644 index 00000000..9eda196b --- /dev/null +++ b/externals/grill/pool/source/data.cpp @@ -0,0 +1,294 @@ +/* + +pool - hierarchical storage object for PD and Max/MSP + +Copyright (c) 2002-2003 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 +#include +#include +#include + +using namespace std; + +pooldata::pooldata(const S *s,I vcnt,I dcnt): + sym(s),nxt(NULL),refs(0), + root(nullatom,NULL,vcnt,dcnt) +{ + FLEXT_LOG1("new pool %s",sym?flext_base::GetString(sym):""); +} + +pooldata::~pooldata() +{ + FLEXT_LOG1("free pool %s",sym?flext_base::GetString(sym):""); +} + + +const A pooldata::nullatom = { A_NULL }; + + +V pooldata::Reset() +{ + root.Reset(); +} + +BL pooldata::MkDir(const AtomList &d,I vcnt,I dcnt) +{ + root.AddDir(d,vcnt,dcnt); + 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::Peek(const AtomList &d,const A &key) +{ + pooldir *pd = root.GetDir(d); + return pd?pd->PeekVal(key):NULL; +} + +poolval *pooldata::Ref(const AtomList &d,const A &key) +{ + pooldir *pd = root.GetDir(d); + return pd?pd->RefVal(key):NULL; +} + +poolval *pooldata::Refi(const AtomList &d,I ix) +{ + pooldir *pd = root.GetDir(d); + return pd?pd->RefVali(ix):NULL; +} + +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::PrintAll(const AtomList &d) +{ + char tmp[1024]; + d.Print(tmp,sizeof tmp); + pooldir *pd = root.GetDir(d); + strcat(tmp," , "); + return pd?pd->PrintAll(tmp,sizeof tmp):0; +} + +I pooldata::CntSub(const AtomList &d) +{ + pooldir *pd = root.GetDir(d); + return pd?pd->CntSub():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,NULL,pd->VSize(),pd->DSize()); + 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) { + // What sizes should we choose here? + pooldir *ret = new pooldir(nullatom,NULL,pd->VSize(),pd->DSize()); + 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 FLEXT_SYS == FLEXT_SYS_PD && FLEXT_OS == FLEXT_OS_WIN + I i,cnt = strlen(src); + if(cnt >= sz-1) return NULL; + for(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); + AtomList tmp; + if(absdir) tmp = d; + return fl.good() && pd->SvDir(fl,depth,tmp); + } + else return false; + } + else + return false; +} + +BL pooldata::LdDirXML(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); + BL ret = fl.good() != 0; + if(ret) { + fl.getline(tmp,sizeof tmp); + ret = !strncmp(tmp,"LdDirXML(fl,depth,mkdir); + return ret; + } + } + + return false; +} + +BL pooldata::SvDirXML(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); + AtomList tmp; + if(absdir) tmp = d; + if(fl.good()) { + fl << "" << endl; + fl << "" << endl; + fl << "" << endl; + BL ret = pd->SvDirXML(fl,depth,tmp); + fl << "" << endl; + return ret; + } + } + } + + return false; +} + + diff --git a/externals/grill/pool/source/main.cpp b/externals/grill/pool/source/main.cpp new file mode 100644 index 00000000..5f75f07d --- /dev/null +++ b/externals/grill/pool/source/main.cpp @@ -0,0 +1,1152 @@ +/* + +pool - hierarchical storage object for PD and Max/MSP + +Copyright (c) 2002-2003 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 + +#define POOL_VERSION "0.2.0pre" + +#define VCNT 64 +#define DCNT 16 + +class pool: + public flext_base +{ + FLEXT_HEADER_S(pool,flext_base,setup) + +public: + pool(I argc,const A *argv); + ~pool(); + + static V setup(t_classid); + + virtual BL Init(); + + pooldata *Pool() { return pl; } + +protected: + + // switch to other pool + V ms_pool(const AtomList &l); + V mg_pool(AtomList &l); + + // clear all data in pool + V m_reset(); + + // handle directories + V m_getdir(); + + V m_mkdir(I argc,const A *argv,BL abs = true,BL chg = false); // make (and change) to dir + V m_mkchdir(I argc,const A *argv) { m_mkdir(argc,argv,true,true); } // make and change to dir + V m_chdir(I argc,const A *argv,BL abs = true); // change to dir + V m_rmdir(I argc,const A *argv,BL abs = true); // remove dir + V m_updir(I argc,const A *argv); // one or more levels up + + V m_mksub(I argc,const A *argv) { m_mkdir(argc,argv,false); } + V m_mkchsub(I argc,const A *argv) { m_mkdir(argc,argv,false,true); } + V m_chsub(I argc,const A *argv) { m_chdir(argc,argv,false); } + V m_rmsub(I argc,const A *argv) { m_rmdir(argc,argv,false); } + + // handle data + V m_set(I argc,const A *argv) { set(MakeSymbol("set"),argc,argv,true); } + V m_add(I argc,const A *argv) { set(MakeSymbol("add"),argc,argv,false); } + V m_clr(I argc,const A *argv); + V m_clrall(); // only values + V m_clrrec(); // also subdirectories + V m_clrsub(); // only subdirectories + V m_get(I argc,const A *argv); + V m_geti(I ix); // get value at index + V m_getall(); // only values + V m_getrec(I argc,const A *argv); // also subdirectories + V m_getsub(I argc,const A *argv); // only subdirectories + V m_ogetall(); // only values (ordered) + V m_ogetrec(I argc,const A *argv); // also subdirectories (ordered) + V m_ogetsub(I argc,const A *argv); // only subdirectories (ordered) + V m_cntall(); // only values + V m_cntrec(I argc,const A *argv); // also subdirectories + V m_cntsub(I argc,const A *argv); // only subdirectories + + // print directories + V m_printall(); // print values in current dir + V m_printrec(I argc,const A *argv,BL fromroot = false); // print values recursively + V m_printroot() { m_printrec(0,NULL,true); } // print values recursively from root + + // cut/copy/paste + V m_paste(I argc,const A *argv) { paste(MakeSymbol("paste"),argc,argv,true); } // paste contents of clipboard + V m_pasteadd(I argc,const A *argv) { paste(MakeSymbol("pasteadd"),argc,argv,false); } // paste but don't replace + V m_clrclip(); // clear clipboard + V m_cut(I argc,const A *argv) { copy(MakeSymbol("cut"),argc,argv,true); } // cut value into clipboard + V m_copy(I argc,const A *argv) { copy(MakeSymbol("copy"),argc,argv,false); } // copy value into clipboard + V m_cutall() { copyall(MakeSymbol("cutall"),true,0); } // cut all values in current directory into clipboard + V m_copyall() { copyall(MakeSymbol("copyall"),false,0); } // copy all values in current directory into clipboard + V m_cutrec(I argc,const A *argv) { copyrec(MakeSymbol("cutrec"),argc,argv,true); } // cut directory (and subdirs) into clipboard + V m_copyrec(I argc,const A *argv) { copyrec(MakeSymbol("copyrec"),argc,argv,false); } // cut directory (and subdirs) into clipboard + + // load/save from/to file + V m_load(I argc,const A *argv) { load(argc,argv,false); } + V m_save(I argc,const A *argv) { save(argc,argv,false); } + V m_loadx(I argc,const A *argv) { load(argc,argv,true); } // XML + V m_savex(I argc,const A *argv) { save(argc,argv,true); } // XML + + // load directories + V m_lddir(I argc,const A *argv) { lddir(argc,argv,false); } // load values into current dir + V m_ldrec(I argc,const A *argv) { ldrec(argc,argv,false); } // load values recursively + V m_ldxdir(I argc,const A *argv) { lddir(argc,argv,true); } // load values into current dir (XML) + V m_ldxrec(I argc,const A *argv) { ldrec(argc,argv,true); } // load values recursively (XML) + + // save directories + V m_svdir(I argc,const A *argv) { svdir(argc,argv,false); } // save values in current dir + V m_svrec(I argc,const A *argv) { svrec(argc,argv,false); } // save values recursively + V m_svxdir(I argc,const A *argv) { svdir(argc,argv,true); } // save values in current dir (XML) + V m_svxrec(I argc,const A *argv) { svrec(argc,argv,true); } // save values recursively (XML) + +private: + static BL KeyChk(const A &a); + static BL ValChk(I argc,const A *argv); + static BL ValChk(const AtomList &l) { return ValChk(l.Count(),l.Atoms()); } + V ToOutAtom(I ix,const A &a); + + enum get_t { get_norm,get_cnt,get_print }; + + V set(const S *tag,I argc,const A *argv,BL over); + V getdir(const S *tag); + I getrec(const S *tag,I level,BL order,get_t how = get_norm,const AtomList &rdir = AtomList()); + I getsub(const S *tag,I level,BL order,get_t how = get_norm,const AtomList &rdir = AtomList()); + + V paste(const S *tag,I argc,const A *argv,BL repl); + V copy(const S *tag,I argc,const A *argv,BL cut); + V copyall(const S *tag,BL cut,I lvls); + V copyrec(const S *tag,I argc,const A *argv,BL cut); + + V load(I argc,const A *argv,BL xml); + V save(I argc,const A *argv,BL xml); + V lddir(I argc,const A *argv,BL xml); // load values into current dir + V ldrec(I argc,const A *argv,BL xml); // load values recursively + V svdir(I argc,const A *argv,BL xml); // save values in current dir + V svrec(I argc,const A *argv,BL xml); // save values recursively + + V echodir() { if(echo) getdir(MakeSymbol("echo")); } + + BL priv,absdir,echo; + I vcnt,dcnt; + pooldata *pl; + AtomList curdir; + pooldir *clip; + const S *holdname; + + static pooldata *head,*tail; + + V SetPool(const S *s); + V FreePool(); + + static pooldata *GetPool(const S *s); + static V RmvPool(pooldata *p); + + std::string MakeFilename(const C *fn) const; + + FLEXT_CALLVAR_V(mg_pool,ms_pool) + FLEXT_ATTRVAR_B(absdir) + FLEXT_ATTRVAR_B(echo) + FLEXT_ATTRGET_B(priv) +// FLEXT_ATTRGET_B(curdir) + FLEXT_ATTRVAR_I(vcnt) + FLEXT_ATTRVAR_I(dcnt) + + FLEXT_CALLBACK(m_reset) + + FLEXT_CALLBACK(m_getdir) + FLEXT_CALLBACK_V(m_mkdir) + FLEXT_CALLBACK_V(m_chdir) + FLEXT_CALLBACK_V(m_mkchdir) + FLEXT_CALLBACK_V(m_updir) + FLEXT_CALLBACK_V(m_rmdir) + FLEXT_CALLBACK_V(m_mksub) + FLEXT_CALLBACK_V(m_chsub) + FLEXT_CALLBACK_V(m_mkchsub) + FLEXT_CALLBACK_V(m_rmsub) + + FLEXT_CALLBACK_V(m_set) + FLEXT_CALLBACK_V(m_add) + FLEXT_CALLBACK_V(m_clr) + FLEXT_CALLBACK(m_clrall) + FLEXT_CALLBACK(m_clrrec) + FLEXT_CALLBACK(m_clrsub) + FLEXT_CALLBACK_V(m_get) + FLEXT_CALLBACK_I(m_geti) + FLEXT_CALLBACK(m_getall) + FLEXT_CALLBACK_V(m_getrec) + FLEXT_CALLBACK_V(m_getsub) + FLEXT_CALLBACK(m_ogetall) + FLEXT_CALLBACK_V(m_ogetrec) + FLEXT_CALLBACK_V(m_ogetsub) + FLEXT_CALLBACK(m_cntall) + FLEXT_CALLBACK_V(m_cntrec) + FLEXT_CALLBACK_V(m_cntsub) + FLEXT_CALLBACK(m_printall) + FLEXT_CALLBACK_V(m_printrec) + FLEXT_CALLBACK(m_printroot) + + FLEXT_CALLBACK_V(m_paste) + FLEXT_CALLBACK_V(m_pasteadd) + FLEXT_CALLBACK(m_clrclip) + FLEXT_CALLBACK_V(m_copy) + FLEXT_CALLBACK_V(m_cut) + FLEXT_CALLBACK(m_copyall) + FLEXT_CALLBACK(m_cutall) + FLEXT_CALLBACK_V(m_copyrec) + FLEXT_CALLBACK_V(m_cutrec) + + FLEXT_CALLBACK_V(m_load) + FLEXT_CALLBACK_V(m_save) + FLEXT_CALLBACK_V(m_lddir) + FLEXT_CALLBACK_V(m_ldrec) + FLEXT_CALLBACK_V(m_svdir) + FLEXT_CALLBACK_V(m_svrec) + FLEXT_CALLBACK_V(m_loadx) + FLEXT_CALLBACK_V(m_savex) + FLEXT_CALLBACK_V(m_ldxdir) + FLEXT_CALLBACK_V(m_ldxrec) + FLEXT_CALLBACK_V(m_svxdir) + FLEXT_CALLBACK_V(m_svxrec) +}; + +FLEXT_NEW_V("pool",pool); + + +pooldata *pool::head,*pool::tail; + + +V pool::setup(t_classid c) +{ + post(""); + post("pool %s - hierarchical storage object, (C)2002-2003 Thomas Grill",POOL_VERSION); + post(""); + + head = tail = NULL; + + FLEXT_CADDATTR_VAR(c,"pool",mg_pool,ms_pool); + FLEXT_CADDATTR_VAR1(c,"absdir",absdir); + FLEXT_CADDATTR_VAR1(c,"echodir",echo); + FLEXT_CADDATTR_GET(c,"private",priv); + FLEXT_CADDATTR_VAR1(c,"valcnt",vcnt); + FLEXT_CADDATTR_VAR1(c,"dircnt",dcnt); + + FLEXT_CADDMETHOD_(c,0,"reset",m_reset); + FLEXT_CADDMETHOD_(c,0,"set",m_set); + FLEXT_CADDMETHOD_(c,0,"add",m_add); + FLEXT_CADDMETHOD_(c,0,"getdir",m_getdir); + FLEXT_CADDMETHOD_(c,0,"mkdir",m_mkdir); + FLEXT_CADDMETHOD_(c,0,"chdir",m_chdir); + FLEXT_CADDMETHOD_(c,0,"mkchdir",m_mkchdir); + FLEXT_CADDMETHOD_(c,0,"rmdir",m_rmdir); + FLEXT_CADDMETHOD_(c,0,"updir",m_updir); + FLEXT_CADDMETHOD_(c,0,"mksub",m_mksub); + FLEXT_CADDMETHOD_(c,0,"chsub",m_chsub); + FLEXT_CADDMETHOD_(c,0,"mkchsub",m_mkchsub); + FLEXT_CADDMETHOD_(c,0,"rmsub",m_rmsub); + + FLEXT_CADDMETHOD_(c,0,"set",m_set); + FLEXT_CADDMETHOD_(c,0,"clr",m_clr); + FLEXT_CADDMETHOD_(c,0,"clrall",m_clrall); + FLEXT_CADDMETHOD_(c,0,"clrrec",m_clrrec); + FLEXT_CADDMETHOD_(c,0,"clrsub",m_clrsub); + FLEXT_CADDMETHOD_(c,0,"get",m_get); + FLEXT_CADDMETHOD_(c,0,"geti",m_geti); + FLEXT_CADDMETHOD_(c,0,"getall",m_getall); + FLEXT_CADDMETHOD_(c,0,"getrec",m_getrec); + FLEXT_CADDMETHOD_(c,0,"getsub",m_getsub); + FLEXT_CADDMETHOD_(c,0,"ogetall",m_ogetall); + FLEXT_CADDMETHOD_(c,0,"ogetrec",m_ogetrec); + FLEXT_CADDMETHOD_(c,0,"ogetsub",m_ogetsub); + FLEXT_CADDMETHOD_(c,0,"cntall",m_cntall); + FLEXT_CADDMETHOD_(c,0,"cntrec",m_cntrec); + FLEXT_CADDMETHOD_(c,0,"cntsub",m_cntsub); + + FLEXT_CADDMETHOD_(c,0,"printall",m_printall); + FLEXT_CADDMETHOD_(c,0,"printrec",m_printrec); + FLEXT_CADDMETHOD_(c,0,"printroot",m_printroot); + + FLEXT_CADDMETHOD_(c,0,"paste",m_paste); + FLEXT_CADDMETHOD_(c,0,"pasteadd",m_pasteadd); + FLEXT_CADDMETHOD_(c,0,"clrclip",m_clrclip); + FLEXT_CADDMETHOD_(c,0,"cut",m_cut); + FLEXT_CADDMETHOD_(c,0,"copy",m_copy); + FLEXT_CADDMETHOD_(c,0,"cutall",m_cutall); + FLEXT_CADDMETHOD_(c,0,"copyall",m_copyall); + FLEXT_CADDMETHOD_(c,0,"cutrec",m_cutrec); + FLEXT_CADDMETHOD_(c,0,"copyrec",m_copyrec); + + FLEXT_CADDMETHOD_(c,0,"load",m_load); + FLEXT_CADDMETHOD_(c,0,"save",m_save); + FLEXT_CADDMETHOD_(c,0,"lddir",m_lddir); + FLEXT_CADDMETHOD_(c,0,"ldrec",m_ldrec); + FLEXT_CADDMETHOD_(c,0,"svdir",m_svdir); + FLEXT_CADDMETHOD_(c,0,"svrec",m_svrec); + FLEXT_CADDMETHOD_(c,0,"loadx",m_loadx); + FLEXT_CADDMETHOD_(c,0,"savex",m_savex); + FLEXT_CADDMETHOD_(c,0,"ldxdir",m_ldxdir); + FLEXT_CADDMETHOD_(c,0,"ldxrec",m_ldxrec); + FLEXT_CADDMETHOD_(c,0,"svxdir",m_svxdir); + FLEXT_CADDMETHOD_(c,0,"svxrec",m_svxrec); +} + +pool::pool(I argc,const A *argv): + absdir(true),echo(false),pl(NULL), + clip(NULL), + vcnt(VCNT),dcnt(DCNT) +{ + holdname = argc >= 1 && IsSymbol(argv[0])?GetSymbol(argv[0]):NULL; + + AddInAnything("Commands in"); + AddOutList(); + AddOutAnything(); + AddOutList(); + AddOutAnything(); +} + +pool::~pool() +{ + FreePool(); +} + +BL pool::Init() +{ + if(flext_base::Init()) { + SetPool(holdname); + return true; + } + else return false; +} + +V pool::SetPool(const S *s) +{ + if(pl) FreePool(); + + if(s) { + priv = false; + pl = GetPool(s); + } + else { + priv = true; + pl = new pooldata(NULL,vcnt,dcnt); + } +} + +V pool::FreePool() +{ + curdir(); // reset current directory + + if(pl) { + if(!priv) + RmvPool(pl); + else + delete pl; + pl = NULL; + } + + if(clip) { delete clip; clip = NULL; } +} + +V pool::ms_pool(const AtomList &l) +{ + const S *s = NULL; + if(l.Count()) { + if(l.Count() > 1) post("%s - pool: superfluous arguments ignored",thisName()); + s = GetASymbol(l[0]); + if(!s) post("%s - pool: invalid pool name, pool set to private",thisName()); + } + + SetPool(s); +} + +V pool::mg_pool(AtomList &l) +{ + if(priv) l(); + else { l(1); SetSymbol(l[0],pl->sym); } +} + +V pool::m_reset() +{ + pl->Reset(); +} + + +V pool::getdir(const S *tag) +{ + ToOutAnything(3,tag,0,NULL); + ToOutList(2,curdir); +} + +V pool::m_getdir() { getdir(MakeSymbol("getdir")); } + +V pool::m_mkdir(I argc,const A *argv,BL abs,BL chg) +{ + const char *nm = chg?"mkchdir":"mkdir"; + if(!ValChk(argc,argv)) + post("%s - %s: invalid directory name",thisName(),nm); + else { + AtomList ndir; + if(abs) ndir(argc,argv); + else (ndir = curdir).Append(argc,argv); + if(!pl->MkDir(ndir,vcnt,dcnt)) { + post("%s - %s: directory couldn't be created",thisName(),nm); + } + else if(chg) + // change to newly created directory + curdir = ndir; + } + + echodir(); +} + +V pool::m_chdir(I argc,const A *argv,BL abs) +{ + if(!ValChk(argc,argv)) + post("%s - chdir: invalid directory name",thisName()); + else { + AtomList prv(curdir); + if(abs) curdir(argc,argv); + else curdir.Append(argc,argv); + if(!pl->ChkDir(curdir)) { + post("%s - chdir: directory couldn't be changed",thisName()); + curdir = prv; + } + } + + echodir(); +} + +V pool::m_updir(I argc,const A *argv) +{ + I lvls = 1; + if(argc > 0) { + if(CanbeInt(argv[0])) { + if(argc > 1) + post("%s - updir: superfluous arguments ignored",thisName()); + lvls = GetAInt(argv[0]); + if(lvls < 0) + post("%s - updir: invalid level specification - set to 1",thisName()); + } + else + post("%s - updir: invalid level specification - set to 1",thisName()); + } + + AtomList prv(curdir); + + if(lvls > curdir.Count()) { + post("%s - updir: level exceeds directory depth - corrected",thisName()); + curdir(); + } + else + curdir.Part(0,curdir.Count()-lvls); + + if(!pl->ChkDir(curdir)) { + post("%s - updir: directory couldn't be changed",thisName()); + curdir = prv; + } + + echodir(); +} + +V pool::m_rmdir(I argc,const A *argv,BL abs) +{ + if(abs) curdir(argc,argv); + else curdir.Append(argc,argv); + + if(!pl->RmDir(curdir)) + post("%s - rmdir: directory couldn't be removed",thisName()); + curdir(); + + echodir(); +} + +V pool::set(const S *tag,I argc,const A *argv,BL over) +{ + if(!argc || !KeyChk(argv[0])) + post("%s - %s: invalid key",thisName(),GetString(tag)); + else if(!ValChk(argc-1,argv+1)) { + post("%s - %s: invalid data values",thisName(),GetString(tag)); + } + else + if(!pl->Set(curdir,argv[0],new AtomList(argc-1,argv+1),over)) + post("%s - %s: value couldn't be set",thisName(),GetString(tag)); + + echodir(); +} + +V pool::m_clr(I argc,const A *argv) +{ + if(!argc || !KeyChk(argv[0])) + post("%s - clr: invalid key",thisName()); + else { + if(argc > 1) + post("%s - clr: superfluous arguments ignored",thisName()); + + if(!pl->Clr(curdir,argv[0])) + post("%s - clr: value couldn't be cleared",thisName()); + } + + echodir(); +} + +V pool::m_clrall() +{ + if(!pl->ClrAll(curdir,false)) + post("%s - clrall: values couldn't be cleared",thisName()); + + echodir(); +} + +V pool::m_clrrec() +{ + if(!pl->ClrAll(curdir,true)) + post("%s - clrrec: values couldn't be cleared",thisName()); + + echodir(); +} + +V pool::m_clrsub() +{ + if(!pl->ClrAll(curdir,true,true)) + post("%s - clrsub: directories couldn't be cleared",thisName()); + + echodir(); +} + +V pool::m_get(I argc,const A *argv) +{ + if(!argc || !KeyChk(argv[0])) + post("%s - get: invalid key",thisName()); + else { + if(argc > 1) + post("%s - get: superfluous arguments ignored",thisName()); + + poolval *r = pl->Ref(curdir,argv[0]); + + ToOutAnything(3,MakeSymbol("get"),0,NULL); + if(absdir) + ToOutList(2,curdir); + else + ToOutList(2,0,NULL); + if(r) { + ToOutAtom(1,r->key); + ToOutList(0,*r->data); + } + else { + ToOutBang(1); + ToOutBang(0); + } + } + + echodir(); +} + +V pool::m_geti(I ix) +{ + if(ix < 0) + post("%s - geti: invalid index",thisName()); + else { + poolval *r = pl->Refi(curdir,ix); + + ToOutAnything(3,MakeSymbol("geti"),0,NULL); + if(absdir) + ToOutList(2,curdir); + else + ToOutList(2,0,NULL); + if(r) { + ToOutAtom(1,r->key); + ToOutList(0,*r->data); + } + else { + ToOutBang(1); + ToOutBang(0); + } + } + + echodir(); +} + +I pool::getrec(const S *tag,I level,BL order,get_t how,const AtomList &rdir) +{ + AtomList gldir(curdir); + gldir.Append(rdir); + + I ret = 0; + + switch(how) { + case get_cnt: + ret = pl->CntAll(gldir); + break; + case get_print: + ret = pl->PrintAll(gldir); + break; + case get_norm: { + A *k; + AtomList *r; + I cnt = pl->GetAll(gldir,k,r); + if(!k) + post("%s - %s: error retrieving values",thisName(),GetString(tag)); + else { + for(I i = 0; i < cnt; ++i) { + ToOutAnything(3,tag,0,NULL); + ToOutList(2,absdir?gldir:rdir); + ToOutAtom(1,k[i]); + ToOutList(0,r[i]); + } + delete[] k; + delete[] r; + } + ret = cnt; + } + } + + if(level != 0) { + const A **r; + I cnt = pl->GetSub(gldir,r); + if(!r) + post("%s - %s: error retrieving directories",thisName(),GetString(tag)); + else { + I lv = level > 0?level-1:-1; + for(I i = 0; i < cnt; ++i) { + ret += getrec(tag,lv,order,how,AtomList(rdir).Append(*r[i])); + } + delete[] r; + } + } + + return ret; +} + +V pool::m_getall() +{ + getrec(MakeSymbol("getall"),0,false); + ToOutBang(3); + + echodir(); +} + +V pool::m_ogetall() +{ + getrec(MakeSymbol("ogetall"),0,true); + ToOutBang(3); + + echodir(); +} + +V pool::m_getrec(I argc,const A *argv) +{ + I lvls = -1; + if(argc > 0) { + if(CanbeInt(argv[0])) { + if(argc > 1) + post("%s - getrec: superfluous arguments ignored",thisName()); + lvls = GetAInt(argv[0]); + } + else + post("%s - getrec: invalid level specification - set to infinite",thisName()); + } + getrec(MakeSymbol("getrec"),lvls,false); + ToOutBang(3); + + echodir(); +} + + +V pool::m_ogetrec(I argc,const A *argv) +{ + I lvls = -1; + if(argc > 0) { + if(CanbeInt(argv[0])) { + if(argc > 1) + post("%s - ogetrec: superfluous arguments ignored",thisName()); + lvls = GetAInt(argv[0]); + } + else + post("%s - ogetrec: invalid level specification - set to infinite",thisName()); + } + getrec(MakeSymbol("ogetrec"),lvls,true); + ToOutBang(3); + + echodir(); +} + + +I pool::getsub(const S *tag,I level,BL order,get_t how,const AtomList &rdir) +{ + AtomList gldir(curdir); + gldir.Append(rdir); + + I ret = 0; + + const A **r = NULL; + // CntSub is not used here because it doesn't allow checking for valid directory + I cnt = pl->GetSub(gldir,r); + if(!r) + post("%s - %s: error retrieving directories",thisName(),GetString(tag)); + else { + I lv = level > 0?level-1:-1; + for(I i = 0; i < cnt; ++i) { + AtomList ndir(absdir?gldir:rdir); + ndir.Append(*r[i]); + + if(how == get_norm) { + ToOutAnything(3,tag,0,NULL); + ToOutList(2,curdir); + ToOutList(1,ndir); + ToOutBang(0); + } + + if(level != 0) + ret += getsub(tag,lv,order,how,AtomList(rdir).Append(*r[i])); + } + delete[] r; + } + + return ret; +} + +V pool::m_getsub(I argc,const A *argv) +{ + I lvls = 0; + if(argc > 0) { + if(CanbeInt(argv[0])) { + if(argc > 1) + post("%s - getsub: superfluous arguments ignored",thisName()); + lvls = GetAInt(argv[0]); + } + else + post("%s - getsub: invalid level specification - set to 0",thisName()); + } + + getsub(MakeSymbol("getsub"),lvls,false); + ToOutBang(3); + + echodir(); +} + + +V pool::m_ogetsub(I argc,const A *argv) +{ + I lvls = 0; + if(argc > 0) { + if(CanbeInt(argv[0])) { + if(argc > 1) + post("%s - ogetsub: superfluous arguments ignored",thisName()); + lvls = GetAInt(argv[0]); + } + else + post("%s - ogetsub: invalid level specification - set to 0",thisName()); + } + + getsub(MakeSymbol("ogetsub"),lvls,true); + ToOutBang(3); + + echodir(); +} + + +V pool::m_cntall() +{ + const S *tag = MakeSymbol("cntall"); + I cnt = getrec(tag,0,false,get_cnt); + ToOutSymbol(3,tag); + ToOutBang(2); + ToOutBang(1); + ToOutInt(0,cnt); + + echodir(); +} + +V pool::m_cntrec(I argc,const A *argv) +{ + const S *tag = MakeSymbol("cntrec"); + + I lvls = -1; + if(argc > 0) { + if(CanbeInt(argv[0])) { + if(argc > 1) + post("%s - %s: superfluous arguments ignored",thisName(),GetString(tag)); + lvls = GetAInt(argv[0]); + } + else + post("%s - %s: invalid level specification - set to infinite",thisName(),GetString(tag)); + } + + I cnt = getrec(tag,lvls,false,get_cnt); + ToOutSymbol(3,tag); + ToOutBang(2); + ToOutBang(1); + ToOutInt(0,cnt); + + echodir(); +} + + +V pool::m_cntsub(I argc,const A *argv) +{ + const S *tag = MakeSymbol("cntsub"); + + I lvls = 0; + if(argc > 0) { + if(CanbeInt(argv[0])) { + if(argc > 1) + post("%s - %s: superfluous arguments ignored",thisName(),GetString(tag)); + lvls = GetAInt(argv[0]); + } + else + post("%s - %s: invalid level specification - set to 0",thisName(),GetString(tag)); + } + + I cnt = getsub(tag,lvls,false,get_cnt); + ToOutSymbol(3,tag); + ToOutBang(2); + ToOutBang(1); + ToOutInt(0,cnt); + + echodir(); +} + +V pool::m_printall() +{ + const S *tag = MakeSymbol("printall"); + I cnt = getrec(tag,0,false,get_print); + post(""); +} + +V pool::m_printrec(I argc,const A *argv,BL fromroot) +{ + const S *tag = MakeSymbol(fromroot?"printroot":"printrec"); + + I lvls = -1; + if(argc > 0) { + if(CanbeInt(argv[0])) { + if(argc > 1) + post("%s - %s: superfluous arguments ignored",thisName(),GetString(tag)); + lvls = GetAInt(argv[0]); + } + else + post("%s - %s: invalid level specification - set to infinite",thisName(),GetString(tag)); + } + + AtomList svdir(curdir); + if(fromroot) curdir.Clear(); + + I cnt = getrec(tag,lvls,false,get_print); + post(""); + + curdir = svdir; +} + + +V pool::paste(const S *tag,I argc,const A *argv,BL repl) +{ + if(clip) { + BL mkdir = true; + I depth = -1; + + if(argc >= 1) { + if(CanbeInt(argv[0])) depth = GetAInt(argv[1]); + else + post("%s - %s: invalid depth argument - set to -1",thisName(),GetString(tag)); + + if(argc >= 2) { + if(CanbeBool(argv[1])) mkdir = GetABool(argv[1]); + else + post("%s - %s: invalid mkdir argument - set to true",thisName(),GetString(tag)); + + if(argc > 2) post("%s - %s: superfluous arguments ignored",thisName(),GetString(tag)); + } + } + + pl->Paste(curdir,clip,depth,repl,mkdir); + } + else + post("%s - %s: clipboard is empty",thisName(),GetString(tag)); + + echodir(); +} + + +V pool::m_clrclip() +{ + if(clip) { delete clip; clip = NULL; } +} + + +V pool::copy(const S *tag,I argc,const A *argv,BL cut) +{ + if(!argc || !KeyChk(argv[0])) + post("%s - %s: invalid key",thisName(),GetString(tag)); + else { + if(argc > 1) + post("%s - %s: superfluous arguments ignored",thisName(),GetString(tag)); + + m_clrclip(); + clip = pl->Copy(curdir,argv[0],cut); + + if(!clip) + post("%s - %s: Copying into clipboard failed",thisName(),GetString(tag)); + } + + echodir(); +} + + +V pool::copyall(const S *tag,BL cut,I depth) +{ + m_clrclip(); + clip = pl->CopyAll(curdir,depth,cut); + + if(!clip) + post("%s - %s: Copying into clipboard failed",thisName(),GetString(tag)); + + echodir(); +} + + +V pool::copyrec(const S *tag,I argc,const A *argv,BL cut) +{ + I lvls = -1; + if(argc > 0) { + if(CanbeInt(argv[0])) { + if(argc > 1) + post("%s - %s: superfluous arguments ignored",thisName(),GetString(tag)); + lvls = GetAInt(argv[0]); + } + else + post("%s - %s: invalid level specification - set to infinite",thisName(),GetString(tag)); + } + + copyall(tag,cut,lvls); +} + +V pool::load(I argc,const A *argv,BL xml) +{ + const C *sym = xml?"loadx":"load"; + const C *flnm = NULL; + if(argc > 0) { + if(argc > 1) post("%s - %s: superfluous arguments ignored",thisName(),sym); + if(IsString(argv[0])) flnm = GetString(argv[0]); + } + + if(!flnm) + post("%s - %s: no filename given",thisName(),sym); + else { + string fl(MakeFilename(flnm)); + if(!(xml?pl->LoadXML(fl.c_str()):pl->Load(fl.c_str()))) + post("%s - %s: error loading data",thisName(),sym); + } + + echodir(); +} + +V pool::save(I argc,const A *argv,BL xml) +{ + const C *sym = xml?"savex":"save"; + const C *flnm = NULL; + if(argc > 0) { + if(argc > 1) post("%s - %s: superfluous arguments ignored",thisName(),sym); + if(IsString(argv[0])) flnm = GetString(argv[0]); + } + + if(!flnm) + post("%s - %s: no filename given",thisName(),sym); + else { + string fl(MakeFilename(flnm)); + if(!(xml?pl->SaveXML(fl.c_str()):pl->Save(fl.c_str()))) + post("%s - %s: error saving data",thisName(),sym); + } + + echodir(); +} + +V pool::lddir(I argc,const A *argv,BL xml) +{ + const C *sym = xml?"ldxdir":"lddir"; + const C *flnm = NULL; + if(argc > 0) { + if(argc > 1) post("%s - %s: superfluous arguments ignored",thisName(),sym); + if(IsString(argv[0])) flnm = GetString(argv[0]); + } + + if(!flnm) + post("%s - %s: invalid filename",thisName(),sym); + else { + string fl(MakeFilename(flnm)); + if(!(xml?pl->LdDirXML(curdir,fl.c_str(),0):pl->LdDir(curdir,fl.c_str(),0))) + post("%s - %s: directory couldn't be loaded",thisName(),sym); + } + + echodir(); +} + +V pool::ldrec(I argc,const A *argv,BL xml) +{ + const C *sym = xml?"ldxrec":"ldrec"; + const C *flnm = NULL; + I depth = -1; + BL mkdir = true; + if(argc >= 1) { + if(IsString(argv[0])) flnm = GetString(argv[0]); + + if(argc >= 2) { + if(CanbeInt(argv[1])) depth = GetAInt(argv[1]); + else + post("%s - %s: invalid depth argument - set to -1",thisName(),sym); + + if(argc >= 3) { + if(CanbeBool(argv[2])) mkdir = GetABool(argv[2]); + else + post("%s - %s: invalid mkdir argument - set to true",thisName(),sym); + + if(argc > 3) post("%s - %s: superfluous arguments ignored",thisName(),sym); + } + } + } + + if(!flnm) + post("%s - %s: invalid filename",thisName(),sym); + else { + string fl(MakeFilename(flnm)); + if(!(xml?pl->LdDirXML(curdir,fl.c_str(),depth,mkdir):pl->LdDir(curdir,fl.c_str(),depth,mkdir))) + post("%s - %s: directory couldn't be saved",thisName(),sym); + } + + echodir(); +} + +V pool::svdir(I argc,const A *argv,BL xml) +{ + const C *sym = xml?"svxdir":"svdir"; + const C *flnm = NULL; + if(argc > 0) { + if(argc > 1) post("%s - %s: superfluous arguments ignored",thisName(),sym); + if(IsString(argv[0])) flnm = GetString(argv[0]); + } + + if(!flnm) + post("%s - %s: invalid filename",thisName(),sym); + else { + string fl(MakeFilename(flnm)); + if(!(xml?pl->SvDirXML(curdir,fl.c_str(),0,absdir):pl->SvDir(curdir,fl.c_str(),0,absdir))) + post("%s - %s: directory couldn't be saved",thisName(),sym); + } + + echodir(); +} + +V pool::svrec(I argc,const A *argv,BL xml) +{ + const C *sym = xml?"svxrec":"svrec"; + const C *flnm = NULL; + if(argc > 0) { + if(argc > 1) post("%s - %s: superfluous arguments ignored",thisName(),sym); + if(IsString(argv[0])) flnm = GetString(argv[0]); + } + + if(!flnm) + post("%s - %s: invalid filename",thisName(),sym); + else { + string fl(MakeFilename(flnm)); + if(!(xml?pl->SvDirXML(curdir,fl.c_str(),-1,absdir):pl->SvDir(curdir,fl.c_str(),-1,absdir))) + post("%s - %s: directory couldn't be saved",thisName(),sym); + } + + echodir(); +} + + + +BL pool::KeyChk(const t_atom &a) +{ + return IsSymbol(a) || IsFloat(a) || IsInt(a); +} + +BL pool::ValChk(I argc,const t_atom *argv) +{ + for(I i = 0; i < argc; ++i) { + const t_atom &a = argv[i]; + if(!IsSymbol(a) && !IsFloat(a) && !IsInt(a)) return false; + } + return true; +} + +V pool::ToOutAtom(I ix,const t_atom &a) +{ + if(IsSymbol(a)) + ToOutSymbol(ix,GetSymbol(a)); + else if(IsFloat(a)) + ToOutFloat(ix,GetFloat(a)); + else if(IsInt(a)) + ToOutInt(ix,GetInt(a)); + else + post("%s - output atom: type not supported!",thisName()); +} + + + +pooldata *pool::GetPool(const S *s) +{ + pooldata *pi = head; + for(; pi && pi->sym != s; pi = pi->nxt) (V)0; + + if(pi) { + pi->Push(); + return pi; + } + else { + pooldata *p = new pooldata(s); + p->Push(); + + // now add to chain + if(head) head->nxt = p; + else head = p; + tail = p; + return p; + } +} + +V pool::RmvPool(pooldata *p) +{ + pooldata *prv = NULL,*pi = head; + for(; pi && pi != p; prv = pi,pi = pi->nxt) (V)0; + + if(pi && !pi->Pop()) { + if(prv) prv->nxt = pi->nxt; + else head = pi->nxt; + if(!pi->nxt) tail = pi; + + delete pi; + } +} + +std::string pool::MakeFilename(const C *fn) const +{ +#if FLEXT_SYS == FLEXT_SYS_PD + // / and \ must not be mixed! + // (char *) type casts for BorlandC++ + C *sl = strchr((C *)fn,'/'); + if(!sl) sl = strchr((C *)fn,'\\'); + if(!sl || (sl != fn +#if FLEXT_OS == FLEXT_OS_WIN + && sl[-1] != ':' // look for drive specification with ":/" or ":\\" +#endif + )) { + // prepend absolute canvas path if filename has no absolute path + const C *p = GetString(canvas_getdir(thisCanvas())); + return string(p)+'/'+fn; + } + else + return fn; +#else +#pragma message("Relative file paths not implemented") + return fn; +#endif +} diff --git a/externals/grill/pool/source/pool.cpp b/externals/grill/pool/source/pool.cpp new file mode 100644 index 00000000..4d6d100e --- /dev/null +++ b/externals/grill/pool/source/pool.cpp @@ -0,0 +1,883 @@ +/* + +pool - hierarchical storage object for PD and Max/MSP + +Copyright (c) 2002-2003 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 +#include +#include +#include + +using namespace std; + +#define VBITS 6 +#define DBITS 5 + +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(flext::GetType(a) == flext::GetType(b)) { + switch(flext::GetType(a)) { + case A_FLOAT: + return compare(flext::GetFloat(a),flext::GetFloat(b)); +#if FLEXT_SYS == FLEXT_SYS_MAX + case A_LONG: + return compare(flext::GetInt(a),flext::GetInt(b)); +#endif + case A_SYMBOL: + return compare(flext::GetSymbol(a),flext::GetSymbol(b)); +#if FLEXT_SYS == FLEXT_SYS_PD + case A_POINTER: + return flext::GetPointer(a) == flext::GetPointer(b)?0:(flext::GetPointer(a) < flext::GetPointer(b)?-1:1); +#endif + default: + FLEXT_LOG("pool - atom comparison: type not handled"); + return -1; + } + } + else + return flext::GetType(a) < flext::GetType(b)?-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,pooldir *p,I vcnt,I dcnt): + parent(p),nxt(NULL),vals(NULL),dirs(NULL), + vbits(vcnt?Int2Bits(vcnt):VBITS),dbits(dcnt?Int2Bits(dcnt):DBITS), + vsize(1<nxt) { + c = compare(argv[0],ix->dir); + if(c <= 0) break; + } + + if(c || !ix) { + pooldir *nd = new pooldir(argv[0],this,vcnt,dcnt); + nd->nxt = ix; + + if(prv) prv->nxt = nd; + else dirs[dix].d = nd; + dirs[dix].cnt++; + 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,dix = DIdx(argv[0]); + pooldir *prv = NULL,*ix = dirs[dix].d; + 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[dix].d = nd; + dirs[dix].cnt--; + ix->nxt = NULL; + return ix; + } + else + return ix; + } +} + +BL pooldir::DelDir(I argc,const A *argv) +{ + pooldir *pd = GetDir(argc,argv,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,vix = VIdx(key); + poolval *prv = NULL,*ix = vals[vix].v; + 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[vix].v = nv; + vals[vix].cnt++; + } + } + else if(over) { + // data exists... only set if overwriting enabled + + if(data) + ix->Set(data); + else { + // delete key + + poolval *nv = ix->nxt; + if(prv) prv->nxt = nv; + else vals[vix].v = nv; + vals[vix].cnt--; + ix->nxt = NULL; + delete ix; + } + } +} + +poolval *pooldir::RefVal(const A &key) +{ + I c = 1,vix = VIdx(key); + poolval *ix = vals[vix].v; + for(; ix; ix = ix->nxt) { + c = compare(key,ix->key); + if(c <= 0) break; + } + + return c || !ix?NULL:ix; +} + + +poolval *pooldir::RefVali(I rix) +{ + for(I vix = 0; vix < vsize; ++vix) + if(rix > vals[vix].cnt) rix -= vals[vix].cnt; + else { + poolval *ix = vals[vix].v; + for(; ix && rix; ix = ix->nxt) --rix; + if(ix && !rix) return ix; + } + return NULL; +} + +flext::AtomList *pooldir::PeekVal(const A &key) +{ + poolval *ix = RefVal(key); + return ix?ix->data:NULL; +} + +flext::AtomList *pooldir::GetVal(const A &key,BL cut) +{ + I c = 1,vix = VIdx(key); + poolval *prv = NULL,*ix = vals[vix].v; + 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[vix].v = nv; + vals[vix].cnt--; + ix->nxt = NULL; + ret = ix->data; ix->data = NULL; + delete ix; + } + else + ret = new AtomList(*ix->data); + return ret; + } +} + +I pooldir::CntAll() const +{ + I cnt = 0; + for(I vix = 0; vix < vsize; ++vix) cnt += vals[vix].cnt; + return cnt; +} + +I pooldir::PrintAll(char *buf,int len) const +{ + int offs = strlen(buf); + + I cnt = 0; + for(I vix = 0; vix < vsize; ++vix) { + poolval *ix = vals[vix].v; + for(I i = 0; ix; ++i,ix = ix->nxt) { + PrintAtom(ix->key,buf+offs,len-offs); + strcat(buf+offs," , "); + int l = strlen(buf+offs)+offs; + ix->data->Print(buf+l,len-l); + post(buf); + } + cnt += vals[vix].cnt; + } + + buf[offs] = 0; + + return cnt; +} + +I pooldir::GetKeys(AtomList &keys) +{ + I cnt = CntAll(); + keys(cnt); + + for(I vix = 0; vix < vsize; ++vix) { + poolval *ix = vals[vix].v; + for(I i = 0; ix; ++i,ix = ix->nxt) + SetAtom(keys[i],ix->key); + } + return cnt; +} + +I pooldir::GetAll(A *&keys,AtomList *&lst,BL cut) +{ + I cnt = CntAll(); + keys = new A[cnt]; + lst = new AtomList[cnt]; + + for(I i = 0,vix = 0; vix < vsize; ++vix) { + poolval *ix = vals[vix].v; + for(; ix; ++i) { + SetAtom(keys[i],ix->key); + lst[i] = *ix->data; + + if(cut) { + poolval *t = ix; + vals[vix].v = ix = ix->nxt; + vals[vix].cnt--; + t->nxt = NULL; delete t; + } + else + ix = ix->nxt; + } + } + return cnt; +} + + +I pooldir::CntSub() const +{ + I cnt = 0; + for(I dix = 0; dix < dsize; ++dix) cnt += dirs[dix].cnt; + return cnt; +} + + +I pooldir::GetSub(const A **&lst) +{ + const I cnt = CntSub(); + lst = new const A *[cnt]; + for(I i = 0,dix = 0; dix < dsize; ++dix) { + pooldir *ix = dirs[dix].d; + for(; ix; ix = ix->nxt) lst[i++] = &ix->dir; + } + return cnt; +} + + +BL pooldir::Paste(const pooldir *p,I depth,BL repl,BL mkdir) +{ + BL ok = true; + + for(I vi = 0; vi < p->vsize; ++vi) { + for(poolval *ix = p->vals[vi].v; ix; ix = ix->nxt) { + SetVal(ix->key,new AtomList(*ix->data),repl); + } + } + + if(ok && depth) { + for(I di = 0; di < p->dsize; ++di) { + for(pooldir *dix = p->dirs[di].d; 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) { + for(I vi = 0; vi < vsize; ++vi) { + for(poolval *ix = vals[vi].v; ix; ix = ix->nxt) + p->SetVal(ix->key,ix->data); + vals[vi].cnt = 0; + vals[vi].v = NULL; + } + } + else { + for(I vi = 0; vi < vsize; ++vi) { + for(poolval *ix = vals[vi].v; ix; ix = ix->nxt) { + p->SetVal(ix->key,new AtomList(*ix->data)); + } + } + } + + if(ok && depth) { + for(I di = 0; di < dsize; ++di) { + for(pooldir *dix = dirs[di].d; ok && dix; dix = dix->nxt) { + pooldir *ndir = p->AddDir(1,&dix->dir); + if(ndir) + ok = dix->Copy(ndir,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)) + if(!s && (*c == '-' || *c == '+')) {} // minus or plus is ok + else + s = (*c != '.' || s == 1)?2:1; + } + + if(a) { + switch(s) { + case 0: // integer +#if FLEXT_SYS == FLEXT_SYS_MAX + flext::SetInt(*a,atoi(m)); + break; +#endif + case 1: // float + flext::SetFloat(*a,(F)atof(m)); + break; + default: { // anything else is a symbol + C t = *c; *c = 0; + flext::SetString(*a,m); + *c = t; + break; + } + } + } + + return c; +} + +static BL ParseAtoms(C *tmp,flext::AtomList &l) +{ + 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 BL ParseAtoms(string &s,flext::AtomList &l) +{ + return ParseAtoms((C *)s.c_str(),l); +} + +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; + else + return ParseAtoms(tmp,l); +} + +static V WriteAtom(ostream &os,const A &a) +{ + switch(a.a_type) { + case A_FLOAT: + os << a.a_w.w_float; + break; +#if FLEXT_SYS == FLEXT_SYS_MAX + case A_LONG: + os << a.a_w.w_long; + break; +#endif + case A_SYMBOL: + os << flext::GetString(flext::GetSymbol(a)); + break; + } +} + +static V WriteAtoms(ostream &os,const flext::AtomList &l) +{ + for(I i = 0; i < l.Count(); ++i) { +// if(IsSymbol(l[i]) os << "\""; + WriteAtom(os,l[i]); +// if(IsSymbol(l[i]) os << "\""; + if(i < l.Count()-1) os << ' '; + } +} + +BL pooldir::LdDir(istream &is,I depth,BL mkdir) +{ + for(I i = 1; !is.eof(); ++i) { + AtomList d,k,*v = new AtomList; + BL r = + ReadAtoms(is,d,',') && + ReadAtoms(is,k,',') && k.Count() == 1 && + ReadAtoms(is,*v,'\n'); + + 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 FLEXT_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(I vi = 0; vi < vsize; ++vi) { + for(poolval *ix = vals[vi].v; 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(I di = 0; di < dsize; ++di) { + for(pooldir *ix = dirs[di].d; ix; ix = ix->nxt) { + ix->SvDir(os,nd,AtomList(dir).Append(ix->dir)); + } + } + } + return true; +} + +class xmltag { +public: + string tag,attr; + bool Ok() const { return tag.length() > 0; } + bool operator ==(const C *t) const { return !tag.compare(t); } + void Clear() { tag.clear(); attr.clear(); } + enum { t_start,t_end,t_empty } type; +}; + +static bool gettag(istream &is,xmltag &tag) +{ + static const char *commstt = ""; + + for(;;) { + // eat whitespace + while(isspace(is.peek())) is.get(); + + // no tag begin -> break + if(is.peek() != '<') break; + is.get(); // swallow < + + char tmp[1024],*t = tmp; + + // parse for comment start + const char *c = commstt; + while(*++c) { + if(*c != is.peek()) break; + *(t++) = is.get(); + } + + if(!*c) { // is comment + char cmp[2] = {0,0}; // set to some unusual initial value + + for(int ic = 0; ; ic = (++ic)%2) { + char c = is.get(); + if(c == '>') { + // if third character is > then check also the former two + int i; + for(i = 0; i < 2 && cmp[(ic+i)%2] == commend[i]; ++i); + if(i == 2) break; // match: comment end found! + } + else + cmp[ic] = c; + } + } + else { + // parse until > with consideration of "s + bool intx = false; + for(;;) { + *t = is.get(); + if(*t == '"') intx = !intx; + else if(*t == '>' && !intx) { + *t = 0; + break; + } + t++; + } + + // look for tag slashes + + char *tb = tmp,*te = t-1,*tf; + + for(; isspace(*tb); ++tb); + if(*tb == '/') { + // slash at the beginning -> end tag + tag.type = xmltag::t_end; + for(++tb; isspace(*tb); ++tb); + } + else { + for(; isspace(*te); --te); + if(*te == '/') { + // slash at the end -> empty tag + for(--te; isspace(*te); --te); + tag.type = xmltag::t_empty; + } + else + // no slash -> begin tag + tag.type = xmltag::t_start; + } + + // copy tag text without slashes + for(tf = tb; tf <= te && *tf && !isspace(*tf); ++tf); + tag.tag.assign(tb,tf-tb); + while(isspace(*tf)) ++tf; + tag.attr.assign(tf,te-tf+1); + + return true; + } + } + + tag.Clear(); + return false; +} + +static void getvalue(istream &is,string &s) +{ + char tmp[1024],*t = tmp; + bool intx = false; + for(;;) { + char c = is.peek(); + if(c == '"') intx = !intx; + else if(c == '<' && !intx) break; + *(t++) = is.get(); + } + *t = 0; + s = tmp; +} + +BL pooldir::LdDirXML(istream &is,I depth,BL mkdir) +{ + AtomList d,k,v; + bool inpool = false,inval = false,inkey = false,indata = false; + const t_symbol *empty = MakeSymbol(""); + + while(!is.eof()) { + xmltag tag; + gettag(is,tag); + if(!tag.Ok()) { + // look for value + string s; + getvalue(is,s); + + if(s.length() && inpool && + ( + (!inval && inkey && d.Count()) || /* dir */ + (inval && (inkey || indata)) /* value */ + ) + ) { + BL ret = true; + if(indata) { + if(v.Count()) + post("pool - XML load: value data already given, ignoring new data"); + else + ret = ParseAtoms(s,v); + } + else // inkey + if(inval) { + if(k.Count()) + post("pool - XML load, value key already given, ignoring new key"); + else + ret = ParseAtoms(s,k); + } + else { + t_atom &dkey = d[d.Count()-1]; + const char *ds = GetString(dkey); + FLEXT_ASSERT(ds); + if(*ds) + post("pool - XML load: dir key already given, ignoring new key"); + else + SetString(dkey,s.c_str()); + ret = true; + } + if(!ret) post("pool - error interpreting XML value (%s)",s.c_str()); + } + else + post("pool - error reading XML data"); + } + else if(tag == "pool") { + if(tag.type == xmltag::t_end) break; + else inpool = true; + } + else if(inpool) { + if(tag == "dir") { + if(tag.type == xmltag::t_start) { + // warn if last directory key was not given + if(d.Count() && GetSymbol(d[d.Count()-1]) == empty) + post("pool - XML load: dir key must be given prior to subdirs, ignoring items"); + + // initialize dir key as empty + t_atom at; SetSymbol(at,empty); + d.Append(at); + } + else if(tag.type == xmltag::t_end) { + if(d.Count()) + d.Part(0,d.Count()-1); + else + post("pool - XML load: superfluous in XML data"); + } + } + else if(tag == "value") { + if(tag.type == xmltag::t_start) { + inval = true; + k.Clear(); v.Clear(); + } + else if(tag.type == xmltag::t_end) { + if(depth < 0 || d.Count() <= depth) { + // NOW set value + + int fnd; + for(fnd = d.Count()-1; fnd >= 0; --fnd) + if(GetSymbol(d[fnd]) == empty) break; + + // look if last dir key has been given + if(fnd >= 0) { + if(fnd == d.Count()-1) + post("pool - XML load: dir key must be given prior to values"); + + // else: one directoy level has been left unintialized, ignore items + } + else { + // only use first word of key + if(k.Count() == 1) { + pooldir *nd = mkdir?AddDir(d):GetDir(d); + if(nd) + nd->SetVal(k[0],new AtomList(v)); + else + post("pool - XML load: value key must be exactly one word, value not stored"); + } + } + } + inval = false; + } + } + else if(tag == "key") { + if(tag.type == xmltag::t_start) { + inkey = true; + } + else if(tag.type == xmltag::t_end) { + inkey = false; + } + } + else if(tag == "data") { + if(!inval) + post("pool - XML tag not within "); + + if(tag.type == xmltag::t_start) { + indata = true; + } + else if(tag.type == xmltag::t_end) { + indata = false; + } + } +#ifdef FLEXT_DEBUG + else { + post("pool - unknown XML tag '%s'",tag.tag.c_str()); + } +#endif + } + else if(tag == "!DOCTYPE") { + // ignore + } +#ifdef FLEXT_DEBUG + else { + post("pool - unknown XML tag '%s'",tag.tag.c_str()); + } +#endif + } + return true; +} + +static void indent(ostream &s,I cnt) +{ + for(I i = 0; i < cnt; ++i) s << '\t'; +} + +BL pooldir::SvDirXML(ostream &os,I depth,const AtomList &dir,I ind) +{ + if(dir.Count()) { + indent(os,ind); + os << "" << endl; + indent(os,ind+1); + os << ""; + WriteAtom(os,dir[dir.Count()-1]); + os << "" << endl; + } + + for(I vi = 0; vi < vsize; ++vi) { + for(poolval *ix = vals[vi].v; ix; ix = ix->nxt) { + indent(os,ind+1); + os << ""; + WriteAtom(os,ix->key); + os << ""; + WriteAtoms(os,*ix->data); + os << "" << endl; + } + } + + if(depth) { + I nd = depth > 0?depth-1:-1; + for(I di = 0; di < dsize; ++di) { + for(pooldir *ix = dirs[di].d; ix; ix = ix->nxt) { + ix->SvDirXML(os,nd,AtomList(dir).Append(ix->dir),ind+1); + } + } + } + + if(dir.Count()) { + indent(os,ind); + os << "" << endl; + } + return true; +} + + + + diff --git a/externals/grill/pool/source/pool.h b/externals/grill/pool/source/pool.h new file mode 100644 index 00000000..99e271b3 --- /dev/null +++ b/externals/grill/pool/source/pool.h @@ -0,0 +1,164 @@ +/* + +pool - hierarchical storage object for PD and Max/MSP + +Copyright (c) 2002-2003 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. + +*/ + +#ifndef __POOL_H +#define __POOL_H + +#define FLEXT_ATTRIBUTES 1 + +#include + +#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 402) +#error You need at least flext version 0.4.2 +#endif + +#include + +using namespace std; + +typedef void V; +typedef int I; +typedef unsigned long UL; +typedef float F; +typedef char C; +typedef bool BL; +typedef t_atom A; +typedef t_symbol S; + +class poolval: + public flext +{ +public: + poolval(const A &key,AtomList *data); + ~poolval(); + + poolval &Set(AtomList *data); + poolval *Dup() const; + + A key; + AtomList *data; + poolval *nxt; +}; + +class pooldir: + public flext +{ +public: + pooldir(const A &dir,pooldir *parent,I vcnt = 0,I dcnt = 0); + ~pooldir(); + + V Clear(BL rec,BL dironly = false); + V Reset(BL realloc = true); + + BL Empty() const { return !dirs && !vals; } + BL HasDirs() const { return dirs != NULL; } + BL HasVals() const { return vals != NULL; } + + pooldir *GetDir(I argc,const A *argv,BL cut = false); + pooldir *GetDir(const AtomList &d,BL cut = false) { return GetDir(d.Count(),d.Atoms(),cut); } + BL DelDir(I argc,const A *argv); + BL DelDir(const AtomList &d) { return DelDir(d.Count(),d.Atoms()); } + pooldir *AddDir(I argc,const A *argv,I vcnt = 0,I dcnt = 0); + pooldir *AddDir(const AtomList &d,I vcnt = 0,I dcnt = 0) { return AddDir(d.Count(),d.Atoms(),vcnt,dcnt); } + + V SetVal(const A &key,AtomList *data,BL over = true); + V ClrVal(const A &key) { SetVal(key,NULL); } + AtomList *PeekVal(const A &key); + AtomList *GetVal(const A &key,BL cut = false); + I CntAll() const; + I GetAll(A *&keys,AtomList *&lst,BL cut = false); + I PrintAll(char *buf,int len) const; + I GetKeys(AtomList &keys); + I CntSub() const; + I GetSub(const A **&dirs); + + poolval *RefVal(const A &key); + poolval *RefVali(I ix); + + BL Paste(const pooldir *p,I depth,BL repl,BL mkdir); + BL Copy(pooldir *p,I depth,BL cur); + + BL LdDir(istream &is,I depth,BL mkdir); + BL LdDirXML(istream &is,I depth,BL mkdir); + BL SvDir(ostream &os,I depth,const AtomList &dir = AtomList()); + BL SvDirXML(ostream &os,I depth,const AtomList &dir = AtomList(),I ind = 0); + + int VSize() const { return vsize; } + int DSize() const { return dsize; } + +protected: + int VIdx(const A &v) const { return FoldBits(AtomHash(v),vbits); } + int DIdx(const A &d) const { return FoldBits(AtomHash(d),dbits); } + + A dir; + pooldir *nxt; + + pooldir *parent; + const I vbits,dbits,vsize,dsize; + + struct valentry { int cnt; poolval *v; }; + struct direntry { int cnt; pooldir *d; }; + + valentry *vals; + direntry *dirs; +}; + +class pooldata: + public flext +{ +public: + pooldata(const S *s = NULL,I vcnt = 0,I dcnt = 0); + ~pooldata(); + + V Push() { ++refs; } + BL Pop() { return --refs > 0; } + + V Reset(); + BL MkDir(const AtomList &d,I vcnt = 0,I dcnt = 0); + BL ChkDir(const AtomList &d); + BL RmDir(const AtomList &d); + + BL Set(const AtomList &d,const A &key,AtomList *data,BL over = true); + BL Clr(const AtomList &d,const A &key); + BL ClrAll(const AtomList &d,BL rec,BL dironly = false); + AtomList *Peek(const AtomList &d,const A &key); + AtomList *Get(const AtomList &d,const A &key); + poolval *Ref(const AtomList &d,const A &key); + poolval *Refi(const AtomList &d,I ix); + I CntAll(const AtomList &d); + I PrintAll(const AtomList &d); + I GetAll(const AtomList &d,A *&keys,AtomList *&lst); + I CntSub(const AtomList &d); + I GetSub(const AtomList &d,const t_atom **&dirs); + + BL Paste(const AtomList &d,const pooldir *clip,I depth = -1,BL repl = true,BL mkdir = true); + pooldir *Copy(const AtomList &d,const A &key,BL cut); + pooldir *CopyAll(const AtomList &d,I depth,BL cut); + + BL LdDir(const AtomList &d,const C *flnm,I depth,BL mkdir = true); + BL SvDir(const AtomList &d,const C *flnm,I depth,BL absdir); + BL Load(const C *flnm) { return LdDir(AtomList(),flnm,-1); } + BL Save(const C *flnm) { return SvDir(AtomList(),flnm,-1,true); } + BL LdDirXML(const AtomList &d,const C *flnm,I depth,BL mkdir = true); + BL SvDirXML(const AtomList &d,const C *flnm,I depth,BL absdir); + BL LoadXML(const C *flnm) { return LdDirXML(AtomList(),flnm,-1); } + BL SaveXML(const C *flnm) { return SvDirXML(AtomList(),flnm,-1,true); } + + I refs; + const S *sym; + pooldata *nxt; + + pooldir root; + +private: + static const A nullatom; +}; + +#endif -- cgit v1.2.1