#include "glulxe.h"
#define serop_KeyIndirect (0x01)
#define serop_ZeroKeyTerminates (0x02)
#define serop_ReturnIndex (0x04)
static void fetchkey(unsigned char *keybuf, glui32 key, glui32 keysize,
glui32 options);
glui32 linear_search(glui32 key, glui32 keysize,
glui32 start, glui32 structsize, glui32 numstructs,
glui32 keyoffset, glui32 options)
{
unsigned char keybuf[4];
glui32 count;
int ix;
int retindex = ((options & serop_ReturnIndex) != 0);
int zeroterm = ((options & serop_ZeroKeyTerminates) != 0);
fetchkey(keybuf, key, keysize, options);
for (count=0; count<numstructs; count++, start+=structsize) {
int match = TRUE;
if (keysize <= 4) {
for (ix=0; match && ix<keysize; ix++) {
if (Mem1(start + keyoffset + ix) != keybuf[ix])
match = FALSE;
}
}
else {
for (ix=0; match && ix<keysize; ix++) {
if (Mem1(start + keyoffset + ix) != Mem1(key + ix))
match = FALSE;
}
}
if (match) {
if (retindex)
return count;
else
return start;
}
if (zeroterm) {
match = TRUE;
for (ix=0; match && ix<keysize; ix++) {
if (Mem1(start + keyoffset + ix) != 0)
match = FALSE;
}
if (match) {
break;
}
}
}
if (retindex)
return -1;
else
return 0;
}
glui32 binary_search(glui32 key, glui32 keysize,
glui32 start, glui32 structsize, glui32 numstructs,
glui32 keyoffset, glui32 options)
{
unsigned char keybuf[4];
unsigned char byte, byte2;
glui32 top, bot, val, addr;
int ix;
int retindex = ((options & serop_ReturnIndex) != 0);
fetchkey(keybuf, key, keysize, options);
bot = 0;
top = numstructs;
while (bot < top) {
int cmp = 0;
val = (top+bot) / 2;
addr = start + val * structsize;
if (keysize <= 4) {
for (ix=0; (!cmp) && ix<keysize; ix++) {
byte = Mem1(addr + keyoffset + ix);
byte2 = keybuf[ix];
if (byte < byte2)
cmp = -1;
else if (byte > byte2)
cmp = 1;
}
}
else {
for (ix=0; (!cmp) && ix<keysize; ix++) {
byte = Mem1(addr + keyoffset + ix);
byte2 = Mem1(key + ix);
if (byte < byte2)
cmp = -1;
else if (byte > byte2)
cmp = 1;
}
}
if (!cmp) {
if (retindex)
return val;
else
return addr;
}
if (cmp < 0) {
bot = val+1;
}
else {
top = val;
}
}
if (retindex)
return -1;
else
return 0;
}
glui32 linked_search(glui32 key, glui32 keysize,
glui32 start, glui32 keyoffset, glui32 nextoffset, glui32 options)
{
unsigned char keybuf[4];
int ix;
glui32 val;
int zeroterm = ((options & serop_ZeroKeyTerminates) != 0);
fetchkey(keybuf, key, keysize, options);
while (start != 0) {
int match = TRUE;
if (keysize <= 4) {
for (ix=0; match && ix<keysize; ix++) {
if (Mem1(start + keyoffset + ix) != keybuf[ix])
match = FALSE;
}
}
else {
for (ix=0; match && ix<keysize; ix++) {
if (Mem1(start + keyoffset + ix) != Mem1(key + ix))
match = FALSE;
}
}
if (match) {
return start;
}
if (zeroterm) {
match = TRUE;
for (ix=0; match && ix<keysize; ix++) {
if (Mem1(start + keyoffset + ix) != 0)
match = FALSE;
}
if (match) {
break;
}
}
val = start + nextoffset;
start = Mem4(val);
}
return 0;
}
static void fetchkey(unsigned char *keybuf, glui32 key, glui32 keysize,
glui32 options)
{
int ix;
if (options & serop_KeyIndirect) {
if (keysize <= 4) {
for (ix=0; ix<keysize; ix++)
keybuf[ix] = Mem1(key+ix);
}
}
else {
switch (keysize) {
case 4:
Write4(keybuf, key);
break;
case 2:
Write2(keybuf, key);
break;
case 1:
Write1(keybuf, key);
break;
default:
fatal_error("Direct search key must hold one, two, or four bytes.");
}
}
}