aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/archetype/id_table.cpp
blob: a611503b71a9415fb386eaa9f038ef023933108b (plain)
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
/* 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.
 *
 */

#include "glk/archetype/id_table.h"
#include "glk/archetype/array.h"

namespace Glk {
namespace Archetype {

ClassifyType DefaultClassification;

// Static variables
IdRecPtr hash[BUCKETS];
XArrayType h_index;

int add_ident(const String &id_str) {
	int hasher;
	IdRecPtr p, new_rec;

	hasher = (int)(toupper(id_str[1])) - 65;	// A..Z => 65..90 => 0..25
	if (hasher < 0 || hasher > 25)
		hasher = 26;
  
	p = hash[hasher];
	while (p->next && *p->next->id_name < id_str)
		p = p->next;

	if (p->next == nullptr || *p->next->id_name > id_str) {
		new_rec = new IdRecType();
		append_to_xarray(h_index, new_rec);
		
		new_rec->id_kind    = DefaultClassification;
		new_rec->id_index   = h_index.size() - 1;
		new_rec->id_integer = new_rec->id_index;
		new_rec->id_name    = NewConstStr(id_str);
		new_rec->next       = p->next;

		p->next = new_rec;
		return h_index.size() - 1;
	} else {
		// found existing identifier
		return p->next->id_index;
	}
}

bool index_ident(int index, IdRecPtr &id_ptr) {
	void *p;
	bool result = index_xarray(h_index, index, p);
	id_ptr = (IdRecPtr)p;
	return result;
}

} // End of namespace Archetype
} // End of namespace Glk