This commit is contained in:
Nishi 2026-04-18 01:05:20 +09:00
commit 933f276e1a
Signed by: nishi
GPG key ID: 27EF69B208EB9343
73 changed files with 1350 additions and 306 deletions

View file

@ -1,568 +0,0 @@
#include <xemil.h>
#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <locale>
#include <iostream>
struct typeinfo {
std::string type;
std::string name;
std::string struct_name;
int pointer;
};
struct property {
typeinfo type;
int common;
};
struct handler {
int common;
int has_arg;
typeinfo arg_type;
};
std::map<std::string, std::ofstream> files;
std::map<std::string, property> props;
std::map<std::string, handler> handlers;
std::locale locale;
static std::string get_ctype(typeinfo type, int name = 1, int do_ptr = 1) {
std::string ret = "";
if(type.type == "integer") {
ret += "int";
} else if(type.type == "string") {
ret += "MwCXX::String";
} else if(type.type == "pixmap") {
ret += "MwLLPixmap";
} else if(type.type == "pointer") {
ret += "void*";
} else if(type.type == "struct") {
ret += type.struct_name;
}
if(do_ptr && type.pointer) ret += "*";
if(name && type.name != "") {
ret += " " + type.name;
}
return ret;
}
static std::string get_type(typeinfo type) {
if(type.type == "integer") {
return "Integer";
} else if(type.type == "string") {
return "Text";
}
return "Void";
}
static std::string uppercase(std::string str) {
std::string r = str;
for(int i = 0; i < r.size(); i++) {
r[i] = std::toupper(r[i], locale);
}
return r;
}
static std::string lowercase(std::string str) {
std::string r = str;
for(int i = 0; i < r.size(); i++) {
r[i] = std::tolower(r[i], locale);
}
return r;
}
#define ATTR_TO_BOOL(node, x) ((s = xl_get_attribute(node, (x))) != NULL) && (strcmp(s, "no") != 0);
static typeinfo xml_to_type(xl_node_t* node) {
char* s;
typeinfo ret;
ret.type = node->name;
ret.name = ((s = xl_get_attribute(node, "name")) != NULL) ? s : "";
ret.struct_name = ((s = xl_get_attribute(node, "defname")) != NULL) ? s : "";
ret.pointer = ATTR_TO_BOOL(node, "pointer");
return ret;
}
static std::string upperfirst(std::string str) {
return uppercase(str.substr(0, 1)) + str.substr(1);
}
static void scan_property(xl_node_t* node) {
property prop;
char* s;
if(node->type != XL_NODE_NODE) return;
prop.type = xml_to_type(node);
prop.common = ATTR_TO_BOOL(node, "common");
props[xl_get_attribute(node, "name")] = prop;
}
static void scan_properties(xl_node_t* node) {
xl_node_t* child = node->first_child;
while(child != NULL) {
scan_property(child);
child = child->next;
}
}
static void scan_handler(xl_node_t* node) {
handler handl;
char* s;
xl_node_t* child = node->first_child;
if(node->type != XL_NODE_NODE) return;
handl.common = ATTR_TO_BOOL(node, "common");
handl.has_arg = 0;
while(child != NULL) {
if(child->type == XL_NODE_NODE) {
handl.has_arg = 1;
handl.arg_type = xml_to_type(child);
}
child = child->next;
}
handlers[xl_get_attribute(node, "name")] = handl;
}
static void scan_handlers(xl_node_t* node) {
xl_node_t* child = node->first_child;
while(child != NULL) {
scan_handler(child);
child = child->next;
}
}
static std::string widget_property_header(std::string name, std::string indent = "") {
std::stringstream strm;
strm << indent + get_ctype(props[name].type, 0) + " Get" + upperfirst(name) + "();" << std::endl;
strm << indent + "void Set" + upperfirst(name) + "(" + get_ctype(props[name].type, 0) + " value);" << std::endl;
return strm.str();
}
static std::string scan_widget_property_header(xl_node_t* node, std::string indent) {
xl_node_t* child = node->first_child;
if(node->type != XL_NODE_NODE) return "";
return widget_property_header(xl_get_attribute(node, "name"), indent);
}
static std::string scan_widget_properties_header(xl_node_t* node, std::string indent) {
std::string r = "";
xl_node_t* child = node->first_child;
while(child != NULL) {
r += scan_widget_property_header(child, indent);
child = child->next;
}
return r;
}
static std::string widget_property_source(std::string widget, std::string name) {
std::stringstream strm;
strm << get_ctype(props[name].type, 0) + " MwCXX::" + widget + "::Get" + upperfirst(name) + "() {" << std::endl;
strm << " return (" + get_ctype(props[name].type, 0) + ")MwGet" + get_type(props[name].type) + "(this->widget, MwN" + name + ");" << std::endl;
strm << "}" << std::endl;
strm << std::endl;
strm << "void MwCXX::" + widget + "::Set" + upperfirst(name) + "(" + get_ctype(props[name].type, 0) + " value) {" << std::endl;
if(props[name].type.type == "string") {
strm << "#ifndef MWCXX_USE_C_STRING" << std::endl;
strm << " MwSetText(this->widget, MwN" + name + ", value.c_str());" << std::endl;
strm << "#else" << std::endl;
}
strm << " MwSet" + get_type(props[name].type) + "(this->widget, MwN" + name + ", (" + get_ctype(props[name].type, 0) + ")value);" << std::endl;
if(props[name].type.type == "string") {
strm << "#endif" << std::endl;
}
strm << "}" << std::endl;
strm << std::endl;
return strm.str();
}
static std::string scan_widget_property_source(xl_node_t* node, std::string widget) {
xl_node_t* child = node->first_child;
if(node->type != XL_NODE_NODE) return "";
return widget_property_source(widget, xl_get_attribute(node, "name"));
}
static std::string scan_widget_properties_source(xl_node_t* node, std::string widget) {
std::string r = "";
xl_node_t* child = node->first_child;
while(child != NULL) {
r += scan_widget_property_source(child, widget);
child = child->next;
}
return r;
}
static std::string widget_handler_header(std::string name, std::string indent = "") {
std::stringstream strm;
strm << indent + "virtual void On" + upperfirst(name) + "(" + (handlers[name].has_arg ? (get_ctype(handlers[name].arg_type, 0, 0) + " value") : "") + ") {};" << std::endl;
return strm.str();
}
static std::string scan_widget_handler_header(xl_node_t* node, std::string indent) {
xl_node_t* child = node->first_child;
if(node->type != XL_NODE_NODE) return "";
return widget_handler_header(xl_get_attribute(node, "name"), indent);
}
static std::string scan_widget_handlers_header(xl_node_t* node, std::string indent) {
std::string r = "";
xl_node_t* child = node->first_child;
while(child != NULL) {
r += scan_widget_handler_header(child, indent);
child = child->next;
}
return r;
}
static std::string widget_handler_source(std::string widget, std::string name) {
std::stringstream strm;
std::string cast = "";
if(handlers[name].has_arg) {
cast = "(" + get_ctype(handlers[name].arg_type, 0) + ")";
if(handlers[name].arg_type.pointer) cast = "*" + cast;
if(handlers[name].arg_type.type == "string") cast += "(char*)";
cast += "call";
}
strm << "static void __On" + upperfirst(name) + "(MwWidget handle, void* user, void* call) {" << std::endl;
strm << " ((MwCXX::" + widget + "*)user)->On" + upperfirst(name) + "(" + cast + ");" << std::endl;
strm << "}" << std::endl;
strm << std::endl;
return strm.str();
}
static std::string scan_widget_handler_source(xl_node_t* node, std::string widget) {
xl_node_t* child = node->first_child;
if(node->type != XL_NODE_NODE) return "";
return widget_handler_source(widget, xl_get_attribute(node, "name"));
}
static std::string scan_widget_handlers_source(xl_node_t* node, std::string widget) {
std::string r = "";
xl_node_t* child = node->first_child;
while(child != NULL) {
r += scan_widget_handler_source(child, widget);
child = child->next;
}
return r;
}
static void scan_widget(xl_node_t* node) {
std::string name;
xl_node_t** p_nodes;
xl_node_t** h_nodes;
xl_node_t* child;
int i;
if(node->type != XL_NODE_NODE) return;
p_nodes = xl_get_path(node, "properties");
h_nodes = xl_get_path(node, "handlers");
name = xl_get_attribute(node, "name");
#define OFS files["W" + name + ".h"]
OFS = std::ofstream("include/MwCXX/Widget/" + name + ".h");
OFS << "#ifndef __MWCXX_WIDGET_" + uppercase(name) + "_H__" << std::endl;
OFS << "#define __MWCXX_WIDGET_" + uppercase(name) + "_H__" << std::endl;
OFS << std::endl;
OFS << "#include <MwCXX/MachDep.h>" << std::endl;
OFS << "#include <MwCXX/Widget/Base.h>" << std::endl;
OFS << std::endl;
OFS << "#include <Mw/Widget/" + name + ".h>" << std::endl;
OFS << std::endl;
OFS << "namespace MwCXX {" << std::endl;
OFS << " class " + name + " : public MwCXX::Base {" << std::endl;
OFS << " protected:" << std::endl;
OFS << " virtual void RegisterHandlers();" << std::endl;
OFS << std::endl;
OFS << " public:" << std::endl;
OFS << " " + name + "(MwWidget widget);" << std::endl;
OFS << " " + name + "(MwCXX::String name, MwCXX::Base* parent, int x, int y, unsigned int width, unsigned int height);" << std::endl;
if(p_nodes != NULL) {
std::string r = "";
for(i = 0; p_nodes[i] != NULL; i++) {
r += scan_widget_properties_header(p_nodes[i], "\t\t");
}
if(r != "") OFS << std::endl
<< r;
}
if(h_nodes != NULL) {
std::string r = "";
for(i = 0; h_nodes[i] != NULL; i++) {
r += scan_widget_handlers_header(h_nodes[i], "\t\t");
}
if(r != "") OFS << std::endl
<< r;
}
OFS << " };" << std::endl;
OFS << "}; // namespace MwCXX" << std::endl;
OFS << std::endl;
OFS << "#endif" << std::endl;
#undef OFS
#define OFS files["W" + name + ".cxx"]
OFS = std::ofstream("src/widget/" + lowercase(name) + ".cxx");
if(name == "OpenGL" || name == "Vulkan") OFS << "#define MW_" + uppercase(name) + "_NO_INCLUDE" << std::endl;
OFS << "#include <MwCXX/Widget/" + name + ".h>" << std::endl;
OFS << std::endl;
OFS << "MwCXX::" + name + "::" + name + "(MwWidget widget) : MwCXX::Base(widget) {" << std::endl;
OFS << " this->RegisterHandlers();" << std::endl;
OFS << "}" << std::endl;
OFS << std::endl;
OFS << "MwCXX::" + name + "::" + name + "(MwCXX::String name, MwCXX::Base* parent, int x, int y, unsigned int width, unsigned int height) : MwCXX::Base(Mw" + name + "Class, name, parent, x, y, width, height) {" << std::endl;
OFS << " this->RegisterHandlers();" << std::endl;
OFS << "}" << std::endl;
OFS << std::endl;
if(p_nodes != NULL) {
for(i = 0; p_nodes[i] != NULL; i++) {
OFS << scan_widget_properties_source(p_nodes[i], name);
}
}
if(h_nodes != NULL) {
for(i = 0; h_nodes[i] != NULL; i++) {
OFS << scan_widget_handlers_source(h_nodes[i], name);
}
}
OFS << "void MwCXX::" + name + "::RegisterHandlers() {" << std::endl;
if(h_nodes != NULL) {
for(i = 0; h_nodes[i] != NULL; i++) {
for(child = h_nodes[i]->first_child; child != NULL; child = child->next) {
std::string hname;
if(child->type != XL_NODE_NODE) continue;
hname = xl_get_attribute(child, "name");
OFS << " MwAddUserHandler(this->widget, MwN" + hname + "Handler, __On" + upperfirst(hname) + ", (void*)this);" << std::endl;
}
}
}
OFS << "}" << std::endl;
#undef OFS
if(p_nodes != NULL) free(p_nodes);
if(h_nodes != NULL) free(h_nodes);
files["objs"] << " src/widget/" + lowercase(name) + ".o";
}
static void scan_widgets(xl_node_t* node) {
xl_node_t* child = node->first_child;
while(child != NULL) {
scan_widget(child);
child = child->next;
}
}
static void base_widget() {
int f;
#define OFS files["WBaseAuto.h"]
OFS = std::ofstream("include/MwCXX/Widget/BaseAuto.h");
f = 0;
for(auto p = props.begin(); p != props.end(); p++) {
if(!p->second.common) continue;
OFS << widget_property_header(p->first);
f = 1;
}
if(f) OFS << std::endl;
for(auto p = handlers.begin(); p != handlers.end(); p++) {
if(!p->second.common) continue;
OFS << widget_handler_header(p->first);
}
#undef OFS
#define OFS files["WBaseAuto.cxx"]
OFS = std::ofstream("src/widget/baseauto.cxx");
OFS << "#include <MwCXX/Widget/Base.h>" << std::endl;
for(auto p = props.begin(); p != props.end(); p++) {
if(!p->second.common) continue;
OFS << widget_property_source("Base", p->first);
}
for(auto h = handlers.begin(); h != handlers.end(); h++) {
if(!h->second.common) continue;
OFS << widget_handler_source("Base", h->first);
}
OFS << "void MwCXX::Base::RegisterHandlers() {" << std::endl;
for(auto h = handlers.begin(); h != handlers.end(); h++) {
if(!h->second.common) continue;
OFS << " MwAddUserHandler(this->widget, MwN" + h->first + "Handler, __On" + upperfirst(h->first) + ", (void*)this);" << std::endl;
}
OFS << "}" << std::endl;
#undef OFS
files["objs"] << " src/widget/baseauto.o";
}
int main(int argc, char** argv) {
xemil_t* h;
int fail = 0;
if(argc < 2) {
std::cerr << "Usage: " + std::string(argv[0]) + " XML" << std::endl;
return 1;
}
locale = std::locale::classic();
h = xl_open_file(argv[1]);
if(h != NULL) {
h->param.new_text = 1;
if(xl_parse(h)) {
xl_node_t** nodes;
files["objs"] = std::ofstream("objs.mk");
files["objs"] << "OBJS +=";
if((nodes = xl_get_path(h->root, "properties")) != NULL) {
for(int i = 0; nodes[i] != NULL; i++) {
scan_properties(nodes[i]);
}
free(nodes);
}
if((nodes = xl_get_path(h->root, "handlers")) != NULL) {
for(int i = 0; nodes[i] != NULL; i++) {
scan_handlers(nodes[i]);
}
free(nodes);
}
base_widget();
if((nodes = xl_get_path(h->root, "widgets")) != NULL) {
for(int i = 0; nodes[i] != NULL; i++) {
scan_widgets(nodes[i]);
}
free(nodes);
}
files["objs"] << std::endl;
for(auto f = files.begin(); f != files.end(); f++) {
f->second.close();
}
} else {
fail = 1;
}
xl_close(h);
} else {
fail = 1;
}
if(fail) {
std::cerr << "Failed to parse XML" << std::endl;
return 1;
}
return 0;
}