From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/lang/token.cc | 268 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100644 src/lang/token.cc (limited to 'src/lang/token.cc') diff --git a/src/lang/token.cc b/src/lang/token.cc new file mode 100644 index 0000000..5bee9ca --- /dev/null +++ b/src/lang/token.cc @@ -0,0 +1,268 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +using namespace std; + +/////////////////////////////////////////////////////////////////// + +const unsigned tok_t::CONSTANT=1; +const unsigned tok_t::RESTRICT=2; +const unsigned tok_t::NORMAL=4; +const unsigned tok_t::SYSONLY=8; +const unsigned tok_t::INIT=16; +const unsigned tok_t::RUNNABLE=32; +const unsigned tok_t::FUNCTION=64; +const unsigned tok_t::BINARY=128; +const unsigned tok_t::REFERENCE=256; +const unsigned tok_t::DBREF=512; +const unsigned tok_t::CRON=1024; +const unsigned tok_t::PRIVATE=NORMAL|SYSONLY; +const unsigned tok_t::PUBLIC=RESTRICT|PRIVATE; +const unsigned tok_t::XMODES=PUBLIC|INIT; + +/////////////////////////////////////////////////////////////////// + +uniheap tok_t::tree; +void* tok_t::operator new(size_t n) { return tree.push(); } +void tok_t::operator delete(void* p) { tree.pop(p); } + +uniheap reference_t::tree; +void* reference_t::operator new(size_t n) { return tree.push(); } +void reference_t::operator delete(void* p) { tree.pop(p); } + +uniheap dbref_t::tree; +void* dbref_t::operator new(size_t n) { return tree.push(); } +void dbref_t::operator delete(void* p) { tree.pop(p); } + +uniheap runnable_t::tree; +void* runnable_t::operator new(size_t n) { return tree.push(); } +void runnable_t::operator delete(void* p) { tree.pop(p); } + +uniheap func_t::tree; +void* func_t::operator new(size_t n) { return tree.push(); } +void func_t::operator delete(void* p) { tree.pop(p); } + +uniheap cron_t::tree; +void* cron_t::operator new(size_t n) { return tree.push(); } +void cron_t::operator delete(void* p) { tree.pop(p); } + +uniheap binary_t::tree; +void* binary_t::operator new(size_t n) { return tree.push(); } +void binary_t::operator delete(void* p) { tree.pop(p); } + +/////////////////////////////////////////////////////////////////// + +mstring uscore2dot(const char* s) { + mstring t=s; int p; while((p=t.find('_'))>=0) t[p]='.'; return t; +} + +/////////////////////////////////////////////////////////////////// + +tok_t::~tok_t() {} + +/////////////////////////////////////////////////////////////////// + +void tokmap::clear() +{ + for(iterator p=list.begin();p!=list.end();p++) delete *p; + list.clear(); +} + +tok_t* tokmap::get_this(const sref& t) +{ + pair p=equal_range(list.begin(),list.end(),(val_t)0,comp_t(t)); + return p.first!=p.second?*p.first:0; +} + +const tok_t* tokmap::get_this(const sref& t) const +{ + pair p= + equal_range(list.begin(),list.end(),(val_t)0,comp_t(t)); + return p.first!=p.second?*p.first:0; +} + +const tok_t* tokmap::get(const sref& t) const +{ + const tok_t* p=get_this(t); return p?p:up?up->get(t):0; +} + +tok_t* tokmap::get(const sref& t) +{ + tok_t* p=get_this(t); return p?p:up?up->get(t):0; +} + +tok_t* tokmap::set(tok_t* tok,int replace,int force) throw(merror_t) +{ + pair p= + equal_range(list.begin(),list.end(),(val_t)0,comp_t(tok->name)); + if(p.first!=p.second) { + if(!(*p.first)->is_replaceable(*tok)) { + delete tok; + THROW("token: Could not set "<<(*p.first)->name<<" - protected"); + return 0; + } + else if((*p.first)->is_const()&&!replace) { + delete tok; + if(force) THROW("token: Could not set "<<(*p.first)->name<<" - constant"); + return 0; + } + else { delete *p.first; return *p.first=tok; } + } + else if(up) return up->set(tok,replace,force); + else { + mstring name=tok->name; + delete tok; + if(force) THROW("token: Could not set "<name); + if(old) { + if(!old->is_replaceable(*tok)) { + delete tok; + THROW("token: Could not add "<name<<" - protected"); + return 0; + } + else if(old->is_const()&&!replace) { + delete tok; + if(force) THROW("token: Could not add "<name<<" - constant"); + return 0; + } + } + pair p= + equal_range(list.begin(),list.end(),(val_t)0,comp_t(tok->name)); + if(p.first!=p.second) { delete *p.first; return *p.first=tok; } + else return *list.insert(p.second,tok); +} + +/////////////////////////////////////////////////////////////////// + +static void Check(const sref& s) throw(merror_t) +{ + if(!is_tokname(s)) THROW("token: bad name ("<name.left(s.size())==s) p=map.erase(p); else p++; + } + } +} + +/////////////////////////////////////////////////////////////// + +mstream& operator<<(mstream& out,const runnable_t& ftok) +{ + out<<"\\"<=0;i--) { if(tok.mod&(1<