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
|
/*************************************************************************
*
* 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 <lang/env.h>
static dbmap* GetDB(Env& env) throw(merror_t) {
if(!env.db) THROW("htc: no open database");
return env.db;
}
static int eval(Env& env,expr_t* x,int mod=OP_ALL) {
mswrite out;
if(x->status&OP_PARSE) env.parse(out,x->expr); else x->eval(out,env,mod);
return atob(out.str());
}
////////////////////////////////////////////////////////////////////////////
TOK_IMPL(create) {
dbref_t* ref=DBRef(*env.toks,env[0],ms_empty,tok_t::PUBLIC,1);
ref->db.Open("/dev/zero",1);
sref first,rest=env[1]; int n=0;
while(Split(first,rest)) ref->db.InsertTable(n++,first);
ref->db.Order(env[2],env[3]);
}
TOK_IMPL(sort) {
dbref_t* ref=(dbref_t*)env.toks->get_this(env[0]);
if(env.is_clear(ref)&&ref->is_dbref()) {
ref->db.remap();
ref->db.Sort(env[1],env[2]);
}
else THROW("htc: "<<env[0]<<" is not a sortable on the current level");
}
TOK_IMPL(db) {
dbref_t* ref=(dbref_t*)env.toks->get(env[0]);
if(env.is_clear(ref)&&ref->is_dbref()) {
ref->db.remap();
Env(env,ref->db).parse(out,env.arg(1));
}
else THROW("htc: "<<env[0]<<" is not a db reference");
}
////////////////////////////////////////////////////////////////////////////
TOK_IMPL(query) {
dbmap* db=GetDB(env);
expr_t* x[2]={env.arg(0),env.arg(1)};
Env nenv(env,*db);
nenv.dbat=0;
while(nenv.dbat>=0&&env.req->is_running()&&nenv.dbat<db->size()) {
int size=db->size();
if(eval(nenv,x[0],OP_LOCAL)) nenv.parse(out,x[1]);
if(db->size()==size) nenv.dbat++;
}
}
TOK_IMPL(find_rec) {
dbmap* db=GetDB(env);
Env nenv(env,*db);
nenv.dbat=db->Find(env[0]);
if(nenv.dbat>=0&&nenv.dbat<db->size()) nenv.parse(out,env.arg(1));
else env.parg(out,2);
}
////////////////////////////////////////////////////////////////////
TOK_IMPL(first_rec) { env.dbat=0; }
TOK_IMPL(prev_rec) { if(env.dbat>0) env.dbat--; }
TOK_IMPL(next_rec) { if(env.dbat<GetDB(env)->size()) env.dbat++; }
TOK_IMPL(exist_rec) { out<<bool(env.dbat>=0&&env.dbat<GetDB(env)->size()); }
TOK_IMPL(rem_rec) {
dbmap* db=GetDB(env);
if(env.dbat>=0&&env.dbat<env.db->size()) db->Rem(env.dbat);
}
////////////////////////////////////////////////////////////////////
TOK_IMPL(lastkey) {
dbmap* db=GetDB(env);
if(!db->empty()) out<<db->at(db->size()-1,db->Index());
}
TOK_IMPL(prevkey) {
dbmap* db=GetDB(env);
if(env.dbat>0&&env.dbat<=db->size()) out<<db->at(env.dbat-1,db->Index());
}
TOK_IMPL(nextkey) {
dbmap* db=GetDB(env);
if(env.dbat>=0&&env.dbat+1<db->size()) out<<db->at(env.dbat+1,db->Index());
}
TOK_IMPL(qprevkey) {
dbmap* db=GetDB(env);
expr_t* x=env.arg(0);
Env nenv(env,*db);
nenv.dbat=env.dbat-1;
while(nenv.dbat>=0&&env.req->is_running()&&nenv.dbat<db->size()) {
if(eval(nenv,x,OP_LOCAL)) { out<<db->at(nenv.dbat,db->Index()); break; }
else nenv.dbat--;
}
}
TOK_IMPL(qnextkey) {
dbmap* db=GetDB(env);
expr_t* x=env.arg(0);
Env nenv(env,*db);
nenv.dbat=env.dbat+1;
while(nenv.dbat>=0&&env.req->is_running()&&nenv.dbat<db->size()) {
if(eval(nenv,x,OP_LOCAL)) { out<<db->at(nenv.dbat,db->Index()); break; }
else nenv.dbat++;
}
}
////////////////////////////////////////////////////////////////////
void envAddDB(tokmap& T)
{
TOK_ADD(create,"xxxx",tok_t::SYSONLY,
"%Create a new database\n"
"%Syntax: \\create{varname}{tables}{index}{comp}\n"
"%\n"
"%Index is the unique, ordered table.\n"
"%Comp is the compare function.\n"
"%A maxmimum of 32 tables are allowed in HDB/1.2\n"
);
TOK_ADD(sort,"xxx",tok_t::PRIVATE,
"%Sort a database\n"
"%Syntax: \\sort{varname}{index}{comp}\n"
"%\n"
"%Note that only globals on the current level are sortable.\n"
);
////////////////////////////////////////////////////////
TOK_ADD(db,"xr",tok_t::PRIVATE,
"%Set current database\n"
"%Syntax: \\db{varname}{body}\n"
"%\n"
"%Within body, all database functions apply.\n"
);
TOK_ADD(query,"xr",tok_t::PRIVATE,
"%Query current database\n"
"%Syntax: \\query{test}{body}\n"
"%\n"
"%Evaluates body for all record where test is true.\n"
"%\n"
"%optimizes globals; it is ok to use full function\n"
"%expressions in the query. Turned out to be better after all.\n"
);
TOK_ADD(find_rec,"xr?",tok_t::PRIVATE,
"%Find record by key, in current database\n"
"%Syntax: \\find.rec{key}{body}<else>\n"
"%\n"
"%Evaluates body if key is found in the current ordering.\n"
"%Otherwise, evaluates <else>, if exist.\n"
);
////////////////////////////////////////////////////////
TOK_ADD(first_rec,"",tok_t::PRIVATE,
"%Go to first record\n"
"%Syntax: \\first.rec\n"
);
TOK_ADD(exist_rec,"",tok_t::PRIVATE,
"%True if current record exist\n"
"%Syntax: \\exist.rec\n"
);
TOK_ADD(next_rec,"",tok_t::PRIVATE,
"%Go to next record\n"
"%Syntax: \\next.rec\n"
);
TOK_ADD(prev_rec,"",tok_t::PRIVATE,
"%Go to previous record\n"
"%Syntax: \\prev.rec\n"
);
TOK_ADD(rem_rec,"",tok_t::SYSONLY,
"%Remove current record\n"
"%Syntax: \\rem.rec\n"
);
////////////////////////////////////////////////////////
TOK_ADD(lastkey,"",tok_t::PRIVATE,
"%Last key of current order\n"
"%Syntax: \\lastkey\n"
"%\n"
"%Silently ignored if not found.\n"
);
TOK_ADD(prevkey,"",tok_t::PRIVATE,
"%Previous key of current order\n"
"%Syntax: \\prevkey\n"
"%\n"
"%Silently ignored if not found.\n"
);
TOK_ADD(nextkey,"",tok_t::PRIVATE,
"%Next key of current order\n"
"%Syntax: \\nextkey\n"
"%\n"
"%Silently ignored if not found.\n"
);
////////////////////////////////////////////////////////
TOK_ADD(qprevkey,"r",tok_t::PRIVATE,
"%Previous key of current order, satisfying query\n"
"%Syntax: \\qprevkey\n"
"%\n"
"%Silently ignored if not found.\n"
);
TOK_ADD(qnextkey,"r",tok_t::PRIVATE,
"%Next key of current order, satisfying query\n"
"%Syntax: \\qnextkey\n"
"%\n"
"%Silently ignored if not found.\n"
);
}
|