Debian Codegraph @

deps/winpty/src/agent/InputMap.h

raw
1 // Copyright (c) 2011-2015 Ryan Prichard
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20
21 #ifndef INPUT_MAP_H
22 #define INPUT_MAP_H
23
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <string>
29
30 #include "SimplePool.h"
31 #include "../shared/WinptyAssert.h"
32
33 class InputMap {
34 public:
35 struct Key {
36 uint16_t virtualKey;
37 uint32_t unicodeChar;
38 uint16_t keyState;
39
40 std::string toString() const;
41 };
42
43 private:
44 struct Node;
45
46 struct Branch {
47 Branch() {
48 memset(&children, 0, sizeof(children));
49 }
50
51 Node *children[256];
52 };
53
54 struct Node {
55 Node() : childCount(0) {
56 Key zeroKey = { 0, 0, 0 };
57 key = zeroKey;
58 }
59
60 Key key;
61 int childCount;
62 enum { kTinyCount = 8 };
63 union {
64 Branch *branch;
65 struct {
66 unsigned char values[kTinyCount];
67 Node *children[kTinyCount];
68 } tiny;
69 } u;
70
71 bool hasKey() const {
72 return key.virtualKey != 0 || key.unicodeChar != 0;
73 }
74 };
75
76 private:
77 SimplePool<Node, 256> m_nodePool;
78 SimplePool<Branch, 8> m_branchPool;
79 Node m_root;
80
81 public:
82 void set(const char *encoding, int encodingLen, const Key &key);
83 int lookupKey(const char *input, int inputSize,
84 Key &keyOut, bool &incompleteOut) const;
85 void dumpInputMap() const;
86
87 private:
88 Node *getChild(Node &node, unsigned char ch) {
89 return const_cast<Node*>(getChild(static_cast<const Node&>(node), ch));
90 }
91
92 const Node *getChild(const Node &node, unsigned char ch) const {
93 if (node.childCount <= Node::kTinyCount) {
94 for (int i = 0; i < node.childCount; ++i) {
95 if (node.u.tiny.values[i] == ch) {
96 return node.u.tiny.children[i];
97 }
98 }
99 return NULL;
100 } else {
101 return node.u.branch->children[ch];
102 }
103 }
104
105 void setHelper(Node &node, const char *encoding, int encodingLen, const Key &key);
106 Node &getOrCreateChild(Node &node, unsigned char ch);
107 void dumpInputMapHelper(const Node &node, std::string &encoding) const;
108 };
109
110 const InputMap::Key kKeyZero = { 0, 0, 0 };
111
112 void dumpInputMap(InputMap &inputMap);
113
114 #endif // INPUT_MAP_H