main.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <AK/JsonArray.h>
  2. #include <AK/JsonObject.h>
  3. #include <AK/JsonValue.h>
  4. #include <AK/LogStream.h>
  5. #include <LibCore/CFile.h>
  6. #include <stdio.h>
  7. int main(int argc, char** argv)
  8. {
  9. if (argc != 2) {
  10. printf("usage: %s <form-file>\n", argv[0]);
  11. return 0;
  12. }
  13. CFile file(argv[1]);
  14. if (!file.open(CIODevice::ReadOnly)) {
  15. fprintf(stderr, "Error: Cannot open %s: %s\n", argv[1], file.error_string());
  16. return 1;
  17. }
  18. auto file_contents = file.read_all();
  19. auto json = JsonValue::from_string(file_contents);
  20. if (!json.is_object()) {
  21. fprintf(stderr, "Malformed input\n");
  22. return 1;
  23. }
  24. auto name = json.as_object().get("name").to_string();
  25. auto widgets = json.as_object().get("widgets");
  26. if (!widgets.is_array()) {
  27. fprintf(stderr, "Malformed input\n");
  28. return 1;
  29. }
  30. dbg() << "struct UI_" << name << " {";
  31. dbg() << " GWidget* main_widget;";
  32. widgets.as_array().for_each([&](auto& value) {
  33. ASSERT(value.is_object());
  34. const JsonObject& widget_object = value.as_object();
  35. auto name = widget_object.get("name").to_string();
  36. auto class_name = widget_object.get("class").to_string();
  37. dbg() << " " << class_name << "* " << name << ";";
  38. });
  39. dbg() << " UI_" << name << "();";
  40. dbg() << "};";
  41. dbg() << "UI_" << name << "::UI_" << name << "()";
  42. dbg() << "{";
  43. dbg() << " main_widget = new GWidget(nullptr);";
  44. widgets.as_array().for_each([&](auto& value) {
  45. ASSERT(value.is_object());
  46. const JsonObject& widget_object = value.as_object();
  47. auto name = widget_object.get("name").to_string();
  48. auto class_name = widget_object.get("class").to_string();
  49. dbg() << " " << name << " = new " << class_name << "(main_widget);";
  50. widget_object.for_each_member([&](auto& property_name, auto& property_value) {
  51. if (property_name == "class")
  52. return;
  53. String value;
  54. if (property_value.is_null())
  55. value = "{}";
  56. else
  57. value = property_value.serialized();
  58. dbg() << " " << name << "->set_" << property_name << "(" << value << ");";
  59. });
  60. dbg() << "";
  61. });
  62. dbg() << "}";
  63. return 0;
  64. }