|
1 | 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
2 | | -// Copyright © 2023 The TokTok team. |
| 2 | +// Copyright © 2023-2024 The TokTok team. |
3 | 3 |
|
4 | 4 | // this file can be used to generate event.c files |
5 | 5 | // requires c++17 |
@@ -119,22 +119,54 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT |
119 | 119 | exit(1); |
120 | 120 | } |
121 | 121 |
|
| 122 | + bool need_stdlib_h = false; |
| 123 | + bool need_string_h = false; |
| 124 | + bool need_tox_unpack_h = false; |
| 125 | + for (const auto& t : event_types) { |
| 126 | + std::visit( |
| 127 | + overloaded{ |
| 128 | + [&](const EventTypeTrivial& t) { |
| 129 | + if (bin_unpack_name_from_type(t.type).rfind("tox_", 0) == 0) { |
| 130 | + need_tox_unpack_h = true; |
| 131 | + } |
| 132 | + }, |
| 133 | + [&](const EventTypeByteRange&) { |
| 134 | + need_stdlib_h = true; |
| 135 | + need_string_h = true; |
| 136 | + } |
| 137 | + }, |
| 138 | + t |
| 139 | + ); |
| 140 | + } |
| 141 | + |
122 | 142 | f << R"(/* SPDX-License-Identifier: GPL-3.0-or-later |
123 | | - * Copyright © 2023 The TokTok team. |
| 143 | + * Copyright © 2023-2024 The TokTok team. |
124 | 144 | */ |
125 | 145 |
|
126 | 146 | #include "events_alloc.h" |
127 | 147 |
|
128 | | -#include <assert.h> |
129 | | -#include <stdlib.h> |
130 | | -#include <string.h> |
| 148 | +#include <assert.h>)"; |
| 149 | + if (need_stdlib_h) { |
| 150 | + f << R"( |
| 151 | +#include <stdlib.h>)"; |
| 152 | + } |
| 153 | + if (need_string_h) { |
| 154 | + f << R"( |
| 155 | +#include <string.h>)"; |
| 156 | + } |
| 157 | + f << R"( |
131 | 158 |
|
132 | 159 | #include "../bin_pack.h" |
133 | 160 | #include "../bin_unpack.h" |
134 | 161 | #include "../ccompat.h" |
| 162 | +#include "../mem.h" |
135 | 163 | #include "../tox.h" |
136 | | -#include "../tox_events.h" |
137 | | -#include "../tox_unpack.h" |
| 164 | +#include "../tox_events.h")"; |
| 165 | + if (need_tox_unpack_h) { |
| 166 | + f << R"( |
| 167 | +#include "../tox_unpack.h")"; |
| 168 | + } |
| 169 | + f << R"( |
138 | 170 |
|
139 | 171 |
|
140 | 172 | /***************************************************** |
@@ -205,10 +237,11 @@ void generate_event_impl(const std::string& event_name, const std::vector<EventT |
205 | 237 | f << " " << event_name_l << "->" << t.name_data << " = nullptr;\n"; |
206 | 238 | f << " " << event_name_l << "->" << t.name_length << " = 0;\n"; |
207 | 239 | f << " }\n\n"; |
208 | | - f << " " << event_name_l << "->" << t.name_data << " = (uint8_t *)malloc(" << t.name_length << ");\n\n"; |
209 | | - f << " if (" << event_name_l << "->" << t.name_data << " == nullptr) {\n"; |
| 240 | + f << " uint8_t *" << t.name_data << "_copy = (uint8_t *)malloc(" << t.name_length << ");\n\n"; |
| 241 | + f << " if (" << t.name_data << "_copy == nullptr) {\n"; |
210 | 242 | f << " return false;\n }\n\n"; |
211 | | - f << " memcpy(" << event_name_l << "->" << t.name_data << ", " << t.name_data << ", " << t.name_length << ");\n"; |
| 243 | + f << " memcpy(" << t.name_data << "_copy, " << t.name_data << ", " << t.name_length << ");\n"; |
| 244 | + f << " " << event_name_l << "->" << t.name_data << " = " << t.name_data << "_copy;\n"; |
212 | 245 | f << " " << event_name_l << "->" << t.name_length << " = " << t.name_length << ";\n"; |
213 | 246 | f << " return true;\n"; |
214 | 247 | } |
@@ -626,7 +659,6 @@ int main(int argc, char** argv) { |
626 | 659 | } |
627 | 660 | }, |
628 | 661 |
|
629 | | -#if 0 // not yet :) |
630 | 662 | { |
631 | 663 | "Group_Peer_Name", |
632 | 664 | { |
@@ -768,7 +800,6 @@ int main(int argc, char** argv) { |
768 | 800 | EventTypeTrivial{"Tox_Group_Mod_Event", "mod_type"}, |
769 | 801 | } |
770 | 802 | }, |
771 | | -#endif |
772 | 803 | }; |
773 | 804 |
|
774 | 805 | if (argc < 2) { |
|
0 commit comments