forked from dail8859/LuaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIFaceTable.cpp
More file actions
175 lines (150 loc) · 5.19 KB
/
Copy pathIFaceTable.cpp
File metadata and controls
175 lines (150 loc) · 5.19 KB
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
// This file is part of LuaScript.
//
// Original work Copyright 1998-2004 by Neil Hodgson <neilh@scintilla.org>
// Derived work Copyright (C)2016 Justin Dailey <dail8859@yahoo.com>
//
// LuaScript 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "IFaceTable.h"
#include <string.h>
#include <ctype.h>
#include <algorithm>
#include <iterator>
template<typename Iter>
static typename const std::iterator_traits<Iter>::value_type *binary_find(Iter begin, Iter end, const char *name) {
auto it = std::lower_bound(begin, end, name, [](const auto &lhs, const char *rhs) {
return strcmp(lhs.name, rhs) < 0;
});
if (it == end || strcmp(name, it->name) != 0) {
return nullptr;
}
else {
return &(*it);
}
}
const IFaceConstant *IFaceTable::FindConstant(const char *name) const {
return binary_find(constants.cbegin(), constants.cend(), name);
}
const IFaceFunction *IFaceTable::FindFunction(const char *name) const {
return binary_find(functions.cbegin(), functions.cend(), name);
}
const IFaceFunction *IFaceTable::FindFunctionByConstantName(const char *name) const {
if (strncmp(name, prefix, strlen(prefix)) == 0) {
// This looks like a constant for an iface function. This requires
// a sequential search. Take special care since the function names
// are mixed case, whereas the constants are all-caps.
for (const auto &func : functions) {
const char *nm = name + strlen(prefix);
const char *fn = func.name;
while (*nm && *fn && (*nm == toupper(*fn))) {
++nm;
++fn;
}
if (!*nm && !*fn) {
return &func;
}
}
}
return nullptr;
}
const IFaceFunction *IFaceTable::FindFunctionByValue(int value) const {
for (const auto &func : functions) {
if (func.value == value)
return &func;
}
return nullptr;
}
const IFaceProperty *IFaceTable::FindProperty(const char *name) const {
return binary_find(properties.cbegin(), properties.cend(), name);
}
int IFaceTable::GetConstantName(int value, char *nameOut, unsigned nameBufferLen, const char *hint) const {
if (nameOut && nameBufferLen > 0) {
*nameOut = '\0';
}
// Look in both the constants table and the functions table. Start with functions.
for (const auto &func : functions) {
if (func.value == value) {
int len = static_cast<int>(strlen(func.name) + strlen(prefix));
if (nameOut && (static_cast<int>(nameBufferLen) > len)) {
strcpy(nameOut, prefix);
strcat(nameOut, func.name);
// fix case
for (char *nm = nameOut + strlen(prefix); *nm; ++nm) {
if (*nm >= 'a' && *nm <= 'z') {
*nm = static_cast<char>(*nm - 'a' + 'A');
}
}
return len;
} else {
return -1 - len;
}
}
}
for (const auto &con : constants) {
if (con.value == value && (hint == NULL || strncmp(hint, con.name, strlen(hint)) == 0)) {
int len = static_cast<int>(strlen(con.name));
if (nameOut && (static_cast<int>(nameBufferLen) > len)) {
strcpy(nameOut, con.name);
return len;
} else {
return -1 - len;
}
}
}
return 0;
}
const IFaceFunction *IFaceTable::GetFunctionByMessage(int message) const {
for (const auto &func : functions) {
if (func.value == message) {
return &func;
}
}
return nullptr;
}
IFaceFunction IFaceTable::GetPropertyFuncByMessage(int message) const {
for (const auto &prop : properties) {
if (prop.getter == message) {
return prop.GetterFunction();
}
else if (prop.setter == message) {
return prop.SetterFunction();
}
}
return { "invalid", -1, iface_void, {iface_void, iface_void} };
}
std::vector<std::string> IFaceTable::GetAllConstantNames() const {
std::vector<std::string> kws;
std::string prefix_str(prefix);
kws.reserve(constants.size() + functions.size());
std::transform(constants.begin(), constants.end(), std::back_inserter(kws), [](const IFaceConstant &ifc) { return ifc.name; });
std::transform(functions.begin(), functions.end(), std::back_inserter(kws), [&](const IFaceFunction &iff) {
std::string s = prefix_str + std::string(iff.name);
std::transform(s.begin(), s.end(), s.begin(), ::toupper);
return s;
});
std::sort(kws.begin(), kws.end());
return kws;
}
std::vector<std::string> IFaceTable::GetAllFunctionNames() const {
std::vector<std::string> kws;
kws.reserve(functions.size());
std::transform(functions.begin(), functions.end(), std::back_inserter(kws), [](const IFaceFunction &iff) { return iff.name; });
return kws;
}
std::vector<std::string> IFaceTable::GetAllPropertyNames() const {
std::vector<std::string> kws;
kws.reserve(properties.size());
std::transform(properties.begin(), properties.end(), std::back_inserter(kws), [](const IFaceProperty &ifp) { return ifp.name; });
return kws;
}