mwcxx/bindgen.cxx
2026-04-17 19:42:14 +09:00

393 lines
8.8 KiB
C++

#include <xemil.h>
#include <map>
#include <string>
#include <fstream>
#include <sstream>
#include <locale>
struct typeinfo {
std::string type;
std::string name;
std::string struct_name;
int pointer;
};
struct property {
typeinfo type;
int common;
typeinfo arg_type;
};
std::map<std::string, std::ofstream> files;
std::map<std::string, property> props;
std::locale locale;
static std::string get_ctype(typeinfo type, int name = 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(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 first_upper(std::string str) {
return uppercase(str.substr(0, 1)) + str.substr(1);
}
static void scan_property(xl_node_t* node) {
property prop;
char* s;
xl_node_t* child = node->first_child;
if(node->type != XL_NODE_NODE) return;
prop.type = xml_to_type(node);
prop.common = ATTR_TO_BOOL(node, "common");
while(child != NULL) {
if(child->type == XL_NODE_NODE) prop.arg_type = xml_to_type(child);
child = child->next;
}
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 std::string widget_property_header(std::string name, std::string indent = "") {
std::stringstream strm;
strm << indent + get_ctype(props[name].type, 0) + " Get" + first_upper(name) + "();" << std::endl;
strm << indent + "void Set" + first_upper(name) + "(" + get_ctype(props[name].type, 0) + " value);" << std::endl;
return strm.str();
}
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" + first_upper(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" + first_upper(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_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 scan_widget_property_source(xl_node_t* node, std::string name) {
xl_node_t* child = node->first_child;
if(node->type != XL_NODE_NODE) return "";
return widget_property_source(name, xl_get_attribute(node, "name"));
}
static std::string scan_widget_properties_source(xl_node_t* node, std::string name) {
std::string r = "";
xl_node_t* child = node->first_child;
while(child != NULL) {
r += scan_widget_property_source(child, name);
child = child->next;
}
return r;
}
static void scan_widget(xl_node_t* node) {
std::string name;
xl_node_t** nodes;
int i;
if(node->type != XL_NODE_NODE) return;
nodes = xl_get_path(node, "properties");
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 << " 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(nodes != NULL) {
std::string r = "";
for(i = 0; nodes[i] != NULL; i++) {
r += scan_widget_properties_header(nodes[i], "\t\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 << "}" << 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 << "}" << std::endl;
OFS << std::endl;
if(nodes != NULL) {
for(i = 0; nodes[i] != NULL; i++) {
OFS << scan_widget_properties_source(nodes[i], name);
}
}
#undef OFS
if(nodes != NULL) free(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() {
std::string r = "";
#define OFS files["WBaseAuto.h"]
OFS = std::ofstream("include/MwCXX/Widget/BaseAuto.h");
for(auto p = props.begin(); p != props.end(); p++) {
if(!p->second.common) continue;
OFS << widget_property_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;
r += widget_property_source("Base", p->first);
}
if(r != "") OFS << std::endl
<< r;
#undef OFS
files["objs"] << " src/widget/baseauto.o";
}
int main(int argc, char** argv) {
xemil_t* h;
int fail = 0;
if(argc < 2) {
printf("Usage: %s XML\n", argv[0]);
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);
}
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) {
fprintf(stderr, "Failed to parse XML\n");
return 1;
}
return 0;
}