Mercurial > repos > blastem
annotate tern.c @ 1524:b96f9fae757f nuklear_ui
Fix Windows build, added Windows default_font_path implementation
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 07 Feb 2018 00:07:12 -0800 |
parents | 63659fb92db4 |
children | 193b804c9845 |
rev | line source |
---|---|
467
140af5509ce7
Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents:
431
diff
changeset
|
1 /* |
140af5509ce7
Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents:
431
diff
changeset
|
2 Copyright 2013 Michael Pavone |
498
51bf87f76d15
Pull shader file names from config file.
Mike Pavone <pavone@retrodev.com>
parents:
467
diff
changeset
|
3 This file is part of BlastEm. |
467
140af5509ce7
Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents:
431
diff
changeset
|
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text. |
140af5509ce7
Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents:
431
diff
changeset
|
5 */ |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 #include "tern.h" |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 #include <stddef.h> |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 #include <stdlib.h> |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
9 #include <string.h> |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
10 #include <stdio.h> |
792
724bbec47f86
Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents:
768
diff
changeset
|
11 #include "util.h" |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
13 tern_node * tern_insert(tern_node * head, char const * key, tern_val value, uint8_t valtype) |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 tern_node ** cur = &head; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 while(*key) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 if (*cur) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 while(*cur && (*cur)->el != *key) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 if (*key < (*cur)->el) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 cur = &(*cur)->left; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 } else { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 cur = &(*cur)->right; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 if (!*cur) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 *cur = malloc(sizeof(tern_node)); |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 (*cur)->left = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 (*cur)->right = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 (*cur)->straight.next = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 (*cur)->el = *key; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
34 (*cur)->valtype = TVAL_NONE; |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 cur = &((*cur)->straight.next); |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 key++; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 while(*cur && (*cur)->el) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 cur = &(*cur)->left; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 if (!*cur) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 *cur = malloc(sizeof(tern_node)); |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 (*cur)->left = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 (*cur)->right = NULL; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 (*cur)->el = 0; |
1491
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
48 (*cur)->valtype = TVAL_NONE; |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
49 } |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
50 if ((*cur)->valtype == TVAL_PTR) { |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
51 //not freeing tern nodes can also cause leaks, but handling freeing those here is problematic |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
52 //since updating a sub-tree may involve creating a new root node |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
53 free((*cur)->straight.value.ptrval); |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 (*cur)->straight.value = value; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
56 (*cur)->valtype = valtype; |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 return head; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
60 uint8_t tern_find(tern_node * head, char const * key, tern_val *ret) |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 tern_node * cur = head; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 while (cur) |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 if (cur->el == *key) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 if (*key) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 cur = cur->straight.next; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 key++; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 } else { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 *ret = cur->straight.value; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
71 return cur->valtype; |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 } else if (*key < cur->el) { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 cur = cur->left; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 } else { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 cur = cur->right; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 } |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
79 return TVAL_NONE; |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 |
1186
110251ea369e
Consting up some parameters to ternary tree functions
Michael Pavone <pavone@retrodev.com>
parents:
792
diff
changeset
|
82 tern_node * tern_find_prefix(tern_node * head, char const * key) |
431
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
83 { |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
84 tern_node * cur = head; |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
85 while (cur && *key) |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
86 { |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
87 if (cur->el == *key) { |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
88 cur = cur->straight.next; |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
89 key++; |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
90 } else if (*key < cur->el) { |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
91 cur = cur->left; |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
92 } else { |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
93 cur = cur->right; |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
94 } |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
95 } |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
96 return cur; |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
97 } |
440efd7d27a9
Read key bindings from config file
Mike Pavone <pavone@retrodev.com>
parents:
429
diff
changeset
|
98 |
1186
110251ea369e
Consting up some parameters to ternary tree functions
Michael Pavone <pavone@retrodev.com>
parents:
792
diff
changeset
|
99 intptr_t tern_find_int(tern_node * head, char const * key, intptr_t def) |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 tern_val ret; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
102 uint8_t valtype = tern_find(head, key, &ret); |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
103 if (valtype == TVAL_INT) { |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 return ret.intval; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 return def; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 |
1186
110251ea369e
Consting up some parameters to ternary tree functions
Michael Pavone <pavone@retrodev.com>
parents:
792
diff
changeset
|
109 tern_node * tern_insert_int(tern_node * head, char const * key, intptr_t value) |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 tern_val val; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 val.intval = value; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
113 return tern_insert(head, key, val, TVAL_INT); |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 |
1186
110251ea369e
Consting up some parameters to ternary tree functions
Michael Pavone <pavone@retrodev.com>
parents:
792
diff
changeset
|
116 void * tern_find_ptr_default(tern_node * head, char const * key, void * def) |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
117 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 tern_val ret; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
119 uint8_t valtype = tern_find(head, key, &ret); |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
120 if (valtype == TVAL_PTR) { |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
121 return ret.ptrval; |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
122 } |
498
51bf87f76d15
Pull shader file names from config file.
Mike Pavone <pavone@retrodev.com>
parents:
467
diff
changeset
|
123 return def; |
51bf87f76d15
Pull shader file names from config file.
Mike Pavone <pavone@retrodev.com>
parents:
467
diff
changeset
|
124 } |
51bf87f76d15
Pull shader file names from config file.
Mike Pavone <pavone@retrodev.com>
parents:
467
diff
changeset
|
125 |
1186
110251ea369e
Consting up some parameters to ternary tree functions
Michael Pavone <pavone@retrodev.com>
parents:
792
diff
changeset
|
126 void * tern_find_ptr(tern_node * head, char const * key) |
498
51bf87f76d15
Pull shader file names from config file.
Mike Pavone <pavone@retrodev.com>
parents:
467
diff
changeset
|
127 { |
51bf87f76d15
Pull shader file names from config file.
Mike Pavone <pavone@retrodev.com>
parents:
467
diff
changeset
|
128 return tern_find_ptr_default(head, key, NULL); |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
129 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
130 |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
131 tern_node *tern_find_node(tern_node *head, char const *key) |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
132 { |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
133 tern_val ret; |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
134 uint8_t valtype = tern_find(head, key, &ret); |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
135 if (valtype == TVAL_NODE) { |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
136 return ret.ptrval; |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
137 } |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
138 return NULL; |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
139 } |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
140 |
1522
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
141 uint8_t tern_delete(tern_node **head, char const *key, tern_val *out) |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
142 { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
143 tern_node *cur = *head, **last = head; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
144 while (cur) |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
145 { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
146 if (cur->el == *key) { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
147 if (*key) { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
148 last = &cur->straight.next; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
149 cur = cur->straight.next; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
150 key++; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
151 } else { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
152 break; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
153 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
154 } else if (*key < cur->el) { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
155 last = &cur->left; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
156 cur = cur->left; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
157 } else { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
158 last = &cur->right; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
159 cur = cur->right; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
160 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
161 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
162 if (!cur) { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
163 return TVAL_NONE; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
164 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
165 *last = cur->right; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
166 uint8_t valtype = cur->valtype; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
167 if (out) { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
168 *out = cur->straight.value; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
169 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
170 free(cur); |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
171 return valtype; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
172 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
173 |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
174 tern_val tern_find_path_default(tern_node *head, char const *key, tern_val def, uint8_t req_valtype) |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
175 { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
176 tern_val ret; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
177 while (*key) |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
178 { |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
179 uint8_t valtype = tern_find(head, key, &ret); |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
180 if (!valtype) { |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
181 return def; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
182 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
183 key = key + strlen(key) + 1; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
184 if (*key) { |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
185 if (valtype != TVAL_NODE) { |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
186 return def; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
187 } |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
188 head = ret.ptrval; |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
189 } else if (req_valtype && req_valtype != valtype) { |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
190 return def; |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
191 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
192 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
193 return ret; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
194 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
195 |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
196 tern_val tern_find_path(tern_node *head, char const *key, uint8_t valtype) |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
197 { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
198 tern_val def; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
199 def.ptrval = NULL; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
200 return tern_find_path_default(head, key, def, valtype); |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
201 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
202 |
1186
110251ea369e
Consting up some parameters to ternary tree functions
Michael Pavone <pavone@retrodev.com>
parents:
792
diff
changeset
|
203 tern_node * tern_insert_ptr(tern_node * head, char const * key, void * value) |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 { |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 tern_val val; |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
206 val.ptrval = value; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
207 return tern_insert(head, key, val, TVAL_PTR); |
429
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
208 } |
f6fdde540791
Added ternary tree implementation and a simple test program for it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
209 |
1186
110251ea369e
Consting up some parameters to ternary tree functions
Michael Pavone <pavone@retrodev.com>
parents:
792
diff
changeset
|
210 tern_node * tern_insert_node(tern_node *head, char const *key, tern_node *value) |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
211 { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
212 tern_val val; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
213 val.ptrval = value; |
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
214 return tern_insert(head, key, val, TVAL_NODE); |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
215 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
216 |
1491
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
217 tern_node *tern_insert_path(tern_node *head, char const *key, tern_val val, uint8_t valtype) |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
218 { |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
219 const char *next_key = key + strlen(key) + 1; |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
220 if (*next_key) { |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
221 tern_node *child = tern_find_node(head, key); |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
222 child = tern_insert_path(child, next_key, val, valtype); |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
223 return tern_insert_node(head, key, child); |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
224 } else { |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
225 return tern_insert(head, key, val, valtype); |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
226 } |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
227 } |
e890971f3757
Somewhat fleshed out video settings view
Michael Pavone <pavone@retrodev.com>
parents:
1326
diff
changeset
|
228 |
1522
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
229 uint8_t tern_delete_path(tern_node **head, char const *key, tern_val *out) |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
230 { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
231 const char *next_key = key + strlen(key) + 1; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
232 if (*next_key) { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
233 tern_node *child = tern_find_node(*head, key); |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
234 if (!child) { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
235 return TVAL_NONE; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
236 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
237 tern_node *tmp = child; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
238 uint8_t valtype = tern_delete_path(&tmp, next_key, out); |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
239 if (tmp != child) { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
240 *head = tern_insert_node(*head, key, tmp); |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
241 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
242 return valtype; |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
243 } else { |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
244 return tern_delete(head, key, out); |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
245 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
246 } |
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
247 |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
248 uint32_t tern_count(tern_node *head) |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
249 { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
250 uint32_t count = 0; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
251 if (head->left) { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
252 count += tern_count(head->left); |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
253 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
254 if (head->right) { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
255 count += tern_count(head->right); |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
256 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
257 if (!head->el) { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
258 count++; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
259 } else if (head->straight.next) { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
260 count += tern_count(head->straight.next); |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
261 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
262 return count; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
263 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
264 |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
265 #define MAX_ITER_KEY 127 |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
266 void tern_foreach_int(tern_node *head, iter_fun fun, void *data, char *keybuf, int pos) |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
267 { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
268 if (!head->el) { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
269 keybuf[pos] = 0; |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
270 fun(keybuf, head->straight.value, head->valtype, data); |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
271 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
272 if (head->left) { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
273 tern_foreach_int(head->left, fun, data, keybuf, pos); |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
274 } |
1522
63659fb92db4
Key binding menu is now functional
Michael Pavone <pavone@retrodev.com>
parents:
1491
diff
changeset
|
275 if (head->el && head->straight.next) { |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
276 if (pos == MAX_ITER_KEY) { |
792
724bbec47f86
Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
Michael Pavone <pavone@retrodev.com>
parents:
768
diff
changeset
|
277 fatal_error("tern_foreach_int: exceeded maximum key size"); |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
278 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
279 keybuf[pos] = head->el; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
280 tern_foreach_int(head->straight.next, fun, data, keybuf, pos+1); |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
281 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
282 if (head->right) { |
768
2f48a3c187c6
Add support for reading cartridge memory map from ROM database, though without EEPROM support for now
Michael Pavone <pavone@retrodev.com>
parents:
766
diff
changeset
|
283 tern_foreach_int(head->right, fun, data, keybuf, pos); |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
284 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
285 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
286 |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
287 void tern_foreach(tern_node *head, iter_fun fun, void *data) |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
288 { |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
289 //lame, but good enough for my purposes |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
290 char key[MAX_ITER_KEY+1]; |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
291 tern_foreach_int(head, fun, data, key, 0); |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
292 } |
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
293 |
631
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
294 char * tern_int_key(uint32_t key, char * buf) |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
295 { |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
296 char * cur = buf; |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
297 while (key) |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
298 { |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
299 *(cur++) = (key & 0x7F) + 1; |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
300 key >>= 7; |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
301 } |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
302 *cur = 0; |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
303 return buf; |
de6f00204fa2
Add support for disassembling VOS program modules
Michael Pavone <pavone@retrodev.com>
parents:
498
diff
changeset
|
304 } |
766
1b2f8280ba81
WIP changes to support reading cart memory map from ROM DB
Michael Pavone <pavone@retrodev.com>
parents:
631
diff
changeset
|
305 |
1293
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
306 void tern_free(tern_node *head) |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
307 { |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
308 if (head->left) { |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
309 tern_free(head->left); |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
310 } |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
311 if (head->right) { |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
312 tern_free(head->right); |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
313 } |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
314 if (head->el) { |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
315 tern_free(head->straight.next); |
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
316 } |
1326
071e761bcdcf
Fix a deficiency in the way types were handled in my ternary tree. Fixes in which some paths that were constructed from a template with variables would sometimes get an extra garbage character thrown in
Michael Pavone <pavone@retrodev.com>
parents:
1293
diff
changeset
|
317 free(head); |
1293
72ea3885e7b5
Don't leak a ternary tree when building the menu's initial path
Michael Pavone <pavone@retrodev.com>
parents:
1186
diff
changeset
|
318 } |