summaryrefslogtreecommitdiff
path: root/src/hed/tables.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/hed/tables.cc')
-rw-r--r--src/hed/tables.cc442
1 files changed, 442 insertions, 0 deletions
diff --git a/src/hed/tables.cc b/src/hed/tables.cc
new file mode 100644
index 0000000..c489e6f
--- /dev/null
+++ b/src/hed/tables.cc
@@ -0,0 +1,442 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include "tables.h"
21#include "input.h"
22#include <string.h>
23#include <malloc.h>
24#include <stdio.h>
25
26const mstring star_tag="*";
27
28const int PAD=2;
29const mstring HELP=
30"Commands from record page:\n"
31"\n"
32"DEL - Delete record\n"
33"RET - Edit record\n"
34"n - New record\n"
35"s - Sort by table\n"
36"o - Order by table\n"
37"c - Check for duplicates\n"
38"f - Filename for map\n"
39"r - Remove table\n"
40"a - Add table\n";
41
42Tables::Tables() :
43 from(0),lines(Lines()-1),atline(0),writable(0),
44 tab(0),space(10),cols(Columns()),atcol(0),
45 lastfrom(-1),lasttab(-1),lastline(0),lastcol(0)
46{
47 dispcols=cols/space;
48}
49
50Tables::~Tables()
51{
52}
53
54void Tables::Setup(int sp)
55{
56 space=sp;
57 dispcols=cols/space;
58 Home();
59}
60
61void Tables::Show()
62{
63 if(db.remap()) {
64 lastfrom=-1;
65 caption.convert("%d, %d, %d, %d",
66 db.head()->filesize.get(),
67 db.head()->fatal.get(),
68 db.head()->major.get(),
69 db.head()->minor.get());
70 }
71 if(from!=lastfrom||tab!=lasttab) {
72 ClearScreen();
73 Bold(1);
74 for(int j=tab;j<tab+dispcols&&j<db.Tables();j++) {
75 if(j==db.Index()) {
76 OutputField(0,space*(j-tab),space-PAD-1,db.Table(j));
77 OutputField(0,space*(j-tab)+space-PAD-1,1,star_tag);
78 }
79 else {
80 OutputField(0,space*(j-tab),space-PAD,db.Table(j));
81 }
82 }
83 Bold(0);
84 for(int i=from;i<from+lines&&i<=db.size();i++) {
85 for(int j=tab;j<tab+dispcols&&j<=db.Tables();j++) {
86 sref tmp; if(i<db.size()&&j<db.Tables()) tmp=db(i,j);
87 if(i==atline&&j==atcol) Reverse(1); else Reverse(0);
88 OutputField(i+1-from,space*(j-tab),space-PAD,tmp);
89 if(i==atline&&j==atcol) Reverse(0);
90 }
91 }
92 Reverse(1);
93 OutputField(lines+1,1,cols-1,caption);
94 Reverse(0);
95 }
96 else {
97 int i=lastline,j=lastcol;
98 if(i>=0&&i<=db.size()&&j>=0&&j<=db.Tables()) {
99 sref tmp; if(i<db.size()&&j<db.Tables()) tmp=db(i,j);
100 OutputField(i+1-from,space*(j-tab),space-PAD,tmp);
101 }
102 i=atline; j=atcol;
103 if(i>=0&&i<=db.size()&&j>=0&&j<=db.Tables()) {
104 sref tmp; if(i<db.size()&&j<db.Tables()) tmp=db(i,j);
105 Reverse(1);
106 OutputField(i+1-from,space*(j-tab),space-PAD,tmp);
107 Reverse(0);
108 }
109 }
110 MoveCursor(lines+1,0);
111 Refresh();
112 lastline=atline;
113 lastcol=atcol;
114 lastfrom=from;
115 lasttab=tab;
116}
117
118void Tables::Home()
119{
120 atline=atcol=from=tab=0;
121}
122
123void Tables::Move(int i,int j)
124{
125 atline+=i;
126 if(atline>=db.size()) atline=db.size();
127 if(atline<0) atline=0;
128 if(atline>=from+lines) from=atline+1-lines;
129 if(atline<from) from=atline;
130 atcol+=j;
131 if(atcol>=db.Tables()) atcol=db.Tables();
132 if(atcol<0) atcol=0;
133 if(atcol>=tab+dispcols) tab=atcol+1-dispcols;
134 if(atcol<tab) tab=atcol;
135}
136
137void Tables::Insert()
138{
139 if(db.remap()) lastfrom=-1;
140 if(db.Tables()==0) return;
141 mstring tx,key,ttab=db.Table(db.Index());
142 tx.convert("%s - insert %s",path.c_str(),ttab.c_str());
143 Editor edit(tx,1);
144 edit.Paste("");
145 edit.Navigate();
146 edit.Copy(key);
147 int row=-1;
148 try {
149 spile list(db.Tables());
150 list[db.Index()]=key;
151 row=db.Insert(list,1);
152 if(row>=0) {
153 caption.convert("%s - added record",path.c_str());
154 Move(row-atline,0);
155 }
156 else {
157 caption.convert("%s - record nonunique - not added",path.c_str());
158 Move(0,0);
159 }
160 }
161 catch(const merror_t& e) {
162 caption=e.desc;
163 Move(0,0);
164 }
165 lastfrom=-1;
166}
167
168void Tables::Delete()
169{
170 if(db.remap()) lastfrom=-1;
171 if(atline<db.size()) {
172 db.Rem(atline);
173 Move(0,0);
174 lastfrom=-1;
175 }
176}
177
178void Tables::Navigate()
179{
180 int c;
181 while(1) {
182 Show();
183 switch(c=Input()) {
184 case KK_UP:
185 case 16:
186 Move(-1,0);
187 break;
188 case KK_DOWN:
189 case 14:
190 Move(1,0);
191 break;
192 case KK_LEFT:
193 case 2:
194 Move(0,-1); break;
195 case KK_RIGHT:
196 case 6:
197 case 9:
198 Move(0,1);
199 break;
200 case KK_NPAGE:
201 Move(lines/2,0);
202 break;
203 case KK_PPAGE:
204 Move(-lines/2,0);
205 break;
206 case KK_HOME:
207 case 1:
208 Move(0,-db.Tables());
209 break;
210 case KK_END:
211 case 5:
212 Move(0,db.Tables());
213 break;
214 case 127:
215 case 4:
216 if(writable) Delete();
217 break;
218 case 13:
219 Edit();
220 break;
221 case 's':
222 Sort();
223 break;
224 case 'o':
225 if(writable) Order();
226 break;
227 case 'c':
228 Check();
229 break;
230 case 'f':
231 FileName();
232 break;
233 case 'r':
234 if(writable) RemTable();
235 break;
236 case 'a':
237 if(writable) AddTable();
238 break;
239 case 'n':
240 if(writable) Insert();
241 break;
242 case 27:
243 if(Command()) break;
244 else return;
245 }
246
247 }
248}
249
250int Tables::Command()
251{
252 mstring tmp=caption;
253 caption="q (quit), s (save), h (help)";
254 lastfrom=-1;
255 Show();
256 switch(Input()) {
257 case 'q':
258 return 0;
259 case 's':
260 Save();
261 break;
262 case 'h':
263 Help();
264 break;
265 default:
266 caption=tmp;
267 break;
268 }
269 lastfrom=-1;
270 return 1;
271}
272
273void Tables::Help()
274{
275 Editor edit("help",0);
276 edit.Paste(HELP);
277 edit.Navigate();
278 lastfrom=-1;
279}
280
281void Tables::Edit()
282{
283 if(db.remap()) lastfrom=-1;
284 if(atline<db.size()&&atcol<db.Tables()) {
285 mstring tx;
286 tx.convert("%s - (%d,%d)",path.c_str(),atline+1,atcol+1);
287 Editor edit(tx,writable&&atcol!=db.Index());
288 edit.Paste(db(atline,atcol));
289 edit.Navigate();
290 edit.Copy(tx);
291 try {
292 if(writable) db.Set(atline,atcol,tx);
293 caption=path;
294 caption+=sref(" edited.");
295 }
296 catch(const merror_t& e) {
297 caption=e.desc;
298 }
299 catch(...) {
300 }
301 Move(0,0);
302 lastfrom=-1;
303 }
304}
305
306void Tables::Sort()
307{
308 if(db.remap()) lastfrom=-1;
309 if(atline<=db.size()&&atcol<db.Tables()) {
310 mstring tx=db.Compname();
311 Editor edit("compname",1);
312 edit.Paste(tx);
313 edit.Navigate();
314 edit.Copy(tx);
315 db.Sort(db.Table(atcol),tx);
316 mstring ttab=db.Table(db.Index());
317 caption.convert("%s sorted: %s %s",path.c_str(),ttab.c_str(),tx.c_str());
318 Move(0,0);
319 lastfrom=-1;
320 }
321}
322
323void Tables::Order()
324{
325 if(db.remap()) lastfrom=-1;
326 if(atline<=db.size()&&atcol<db.Tables()) {
327 mstring tx=db.Compname();
328 Editor edit("compname",1);
329 edit.Paste(tx);
330 edit.Navigate();
331 edit.Copy(tx);
332 db.Order(db.Table(atcol),tx);
333 mstring ttab=db.Table(db.Index());
334 caption.convert("%s ordered: %s %s",path.c_str(),ttab.c_str(),tx.c_str());
335 Move(0,0);
336 lastfrom=-1;
337 }
338}
339
340void Tables::Check()
341{
342 if(db.remap()) lastfrom=-1;
343 int idx=db.Index();
344 mswrite mm;
345 for(int i=0;i<db.size()-1;i++) {
346 if(db(i,idx)==db(i+1,idx)) mm<<db(i,idx)<<" duplicated\n";
347 }
348 if(mm.str().empty()) mm<<"Nothing duplicated\n";
349 Editor edit("check",0);
350 edit.Paste(mm.str());
351 edit.Navigate();
352 lastfrom=-1;
353}
354
355void Tables::FileName()
356{
357 if(db.remap()) lastfrom=-1;
358 mstring tx; tx.convert("%s - filename",path.c_str());
359 Editor edit(tx,1);
360 tx.convert("%s",path.c_str());
361 edit.Paste(tx);
362 edit.Navigate();
363 edit.Copy(tx);
364 path=tx;
365 lastfrom=-1;
366}
367
368void Tables::AddTable()
369{
370 if(db.remap()) lastfrom=-1;
371 mstring tx;
372 tx.convert("%s - add table (def: 2nd line)",path.c_str());
373 Editor edit(tx,1);
374 edit.Paste("");
375 edit.Navigate();
376 mstring tb;
377 edit.Copy(tb);
378 sref first,rest=tb;
379 Splitln(first,rest);
380 mstring name=first;
381 Splitln(first,rest);
382 int ok=-db.Tables();
383 db.InsertTable(atcol,name);
384 ok+=db.Tables();
385 if(ok) {
386 Move(0,0);
387 caption.convert("%s - added table",path.c_str());
388 }
389 else caption.convert("%s - not unique - table not added",path.c_str());
390 lastfrom=-1;
391}
392
393void Tables::RemTable()
394{
395 if(db.remap()) lastfrom=-1;
396 if(atcol<db.Tables()&&atcol!=db.Index()) {
397 db.RemTable(atcol);
398 Move(0,0);
399 caption.convert("%s - removed table",path.c_str());
400 }
401 else caption.convert("%s - key table - not removed",path.c_str());
402 lastfrom=-1;
403}
404
405void Tables::Open(const mstring& s,int ex)
406{
407 path=s;
408 writable=ex;
409 db.Open(s,ex);
410 if(ex) db.Saveas(path+sref(".bak"));
411 caption=s;
412 caption+=sref(" loaded");
413 if(ex) caption+=sref(" (exclusive)");
414 lastfrom=-1;
415}
416
417void Tables::Save()
418{
419 try {
420 db.Saveas(path);
421 caption=path;
422 caption+=sref(" saved");
423 }
424 catch(const merror_t& e) {
425 caption=e.desc;
426 }
427 lastfrom=-1;
428}
429
430void Tables::Close()
431{
432 if(writable) {
433 db.Close();
434 caption=path;
435 caption+=sref(" closed");
436 }
437 else {
438 caption=path;
439 caption+=sref(" readonly - not closed");
440 }
441 lastfrom=-1;
442}