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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* This code is based on original Soltys source code
* Copyright (c) 1994-1995 Janus B. Wisniewski and L.K. Avalon
*/
#include "cge/btfile.h"
#include "common/system.h"
#include "common/str.h"
#include <string.h>
namespace CGE {
#ifndef BT_SIZE
#define BT_SIZE K(1)
#endif
#ifndef BT_KEYLEN
#define BT_KEYLEN 13
#endif
BTFILE::BTFILE(const char *name, IOMODE mode, CRYPT *crpt)
: IOHAND(name, mode, crpt) {
for (int i = 0; i < BT_LEVELS; i++) {
Buff[i].Page = new BT_PAGE;
Buff[i].PgNo = BT_NONE;
Buff[i].Indx = -1;
Buff[i].Updt = false;
if (Buff[i].Page == NULL)
error("No core");
}
}
BTFILE::~BTFILE(void) {
for (int i = 0; i < BT_LEVELS; i++) {
PutPage(i);
delete Buff[i].Page;
}
}
void BTFILE::PutPage(int lev, bool hard) {
if (hard || Buff[lev].Updt) {
Seek(Buff[lev].PgNo * sizeof(BT_PAGE));
Write((uint8 *) Buff[lev].Page, sizeof(BT_PAGE));
Buff[lev].Updt = false;
}
}
BT_PAGE *BTFILE::GetPage(int lev, uint16 pgn) {
if (Buff[lev].PgNo != pgn) {
int32 pos = pgn * sizeof(BT_PAGE);
PutPage(lev);
Buff[lev].PgNo = pgn;
if (Size() > pos) {
Seek((uint32) pgn * sizeof(BT_PAGE));
Read((uint8 *) Buff[lev].Page, sizeof(BT_PAGE));
Buff[lev].Updt = false;
} else {
Buff[lev].Page->Hea.Count = 0;
Buff[lev].Page->Hea.Down = BT_NONE;
memset(Buff[lev].Page->Data, '\0', sizeof(Buff[lev].Page->Data));
Buff[lev].Updt = true;
}
Buff[lev].Indx = -1;
}
return Buff[lev].Page;
}
BT_KEYPACK *BTFILE::Find(const char *key) {
int lev = 0;
uint16 nxt = BT_ROOT;
while (! Error) {
BT_PAGE *pg = GetPage(lev, nxt);
// search
if (pg->Hea.Down != BT_NONE) {
int i;
for (i = 0; i < pg->Hea.Count; i++) {
// Does this work, or does it have to compare the entire buffer?
if (scumm_strnicmp((const char *) key, (const char*)pg->Inn[i].Key, BT_KEYLEN) < 0)
break;
}
nxt = (i) ? pg->Inn[i - 1].Down : pg->Hea.Down;
Buff[lev].Indx = i - 1;
++ lev;
} else {
int i;
for (i = 0; i < pg->Hea.Count - 1; i++) {
if (scumm_stricmp((const char *)key, (const char *)pg->Lea[i].Key) <= 0)
break;
}
Buff[lev].Indx = i;
return &pg->Lea[i];
}
}
return NULL;
}
int keycomp(const void *k1, const void *k2) {
return scumm_strnicmp((const char *) k1, (const char*) k2, BT_KEYLEN);
}
void BTFILE::Make(BT_KEYPACK *keypack, uint16 count) {
#if BT_LEVELS != 2
#error This tiny BTREE implementation works with exactly 2 levels!
#endif
_fqsort(keypack, count, sizeof(*keypack), keycomp);
uint16 n = 0;
BT_PAGE *Root = GetPage(0, n++),
*Leaf = GetPage(1, n);
Root->Hea.Down = n;
PutPage(0, true);
while (count--) {
if (Leaf->Hea.Count >= ArrayCount(Leaf->Lea)) {
PutPage(1, true); // save filled page
Leaf = GetPage(1, ++n); // take empty page
memcpy(Root->Inn[Root->Hea.Count].Key, keypack->Key, BT_KEYLEN);
Root->Inn[Root->Hea.Count++].Down = n;
Buff[0].Updt = true;
}
Leaf->Lea[Leaf->Hea.Count++] = *(keypack++);
Buff[1].Updt = true;
}
}
} // End of namespace CGE
|