Debian Codegraph @

Utilities/GUI2CPP/gui2cpp.cpp

raw
1 /*
2 GUI2CPP,
3 Written in the unholy year 2003 by Magnus Norddahl.
4
5 This is a little commandline tool that sync's a c++ source file and
6 header file with a ClanLib GUI definition file.
7 */
8
9 #include "gui2cpp.h"
10 #include <iostream>
11
12 /////////////////////////////////////////////////////////////////////////////
13 // Application startup:
14
15 int main(int argc, char **argv)
16 {
17 if (argc != 4)
18 {
19 std::cout << "Syntax: gui2cpp <gui file> <source file> <header file>" << std::endl << std::endl;
20 return 255;
21 }
22
23 #ifdef WIN32
24 CL_SetupCore::set_instance(GetModuleHandle(0));
25 #endif
26 CL_SetupCore::init();
27 CL_SetupDisplay::init();
28 CL_SetupGUI::init();
29
30 try
31 {
32 GUI2CPP gui2cpp(argv[1], argv[2], argv[3]);
33 if (gui2cpp.load() == false) return 1;
34 if (gui2cpp.convert() == false) return 2;
35 if (gui2cpp.save() == false) return 3;
36 }
37 catch (CL_Error err)
38 {
39 std::cout << err.message.c_str() << std::endl;
40 return 254;
41 }
42
43 CL_SetupGUI::deinit();
44 CL_SetupDisplay::deinit();
45 CL_SetupCore::deinit();
46
47 return 0;
48 }
49
50 /////////////////////////////////////////////////////////////////////////////
51 // GUI2CPP construction:
52
53 GUI2CPP::GUI2CPP(
54 const std::string &gui_file,
55 const std::string &source_file,
56 const std::string &header_file)
57 : gui_file(gui_file), source_file(source_file), header_file(header_file)
58 {
59 }
60
61 GUI2CPP::~GUI2CPP()
62 {
63 }
64
65 /////////////////////////////////////////////////////////////////////////////
66 // GUI2CPP attributes:
67
68 /////////////////////////////////////////////////////////////////////////////
69 // GUI2CPP operations:
70
71 bool GUI2CPP::load()
72 {
73 gui_document.load(new CL_InputSource_File(gui_file), true);
74
75 bool result;
76 result = load_file(source_data, source_file);
77 if (!result) return false;
78
79 result = load_file(header_data, header_file);
80 if (!result) return false;
81
82 return true;
83 }
84
85 bool GUI2CPP::convert()
86 {
87 bool result;
88
89 old_header_variables = find_block(header_data, "CL_HeaderVariables");
90 old_header_functions = find_block(header_data, "CL_HeaderFunctions");
91 old_source_connections = find_block(source_data, "CL_SourceConnections");
92
93 result = parse_components(gui_document.get_document_element());
94 if (!result) return false;
95
96 result = inject_block(header_data, "CL_HeaderVariables", new_header_variables);
97 if (!result) return false;
98
99 result = inject_block(header_data, "CL_HeaderFunctions", new_header_functions);
100 if (!result) return false;
101
102 result = inject_block(source_data, "CL_SourceConnections", new_source_connections);
103 if (!result) return false;
104
105 return true;
106 }
107
108 bool GUI2CPP::save()
109 {
110 bool result;
111 result = save_file(source_data, source_file);
112 if (!result) return false;
113
114 result = save_file(header_data, header_file);
115 if (!result) return false;
116
117 return true;
118 }
119
120 /////////////////////////////////////////////////////////////////////////////
121 // GUI2CPP implementation:
122
123 bool GUI2CPP::parse_components(const CL_DomElement &componentsElement)
124 {
125 for (CL_DomNode node = componentsElement.get_first_child(); !node.is_null(); node = node.get_next_sibling())
126 {
127 if (!node.is_element()) continue;
128
129 CL_DomElement element = node.to_element();
130 std::string component_type = element.get_tag_name();
131 std::string component_name = element.get_attribute("name");
132
133 std::cout << "name: " << component_name.c_str() << ", type: " << component_type.c_str() << std::endl;
134
135 // Setup header variables:
136 if (!component_name.empty())
137 {
138 std::string class_name = CL_ComponentType::get_class_name(component_type);
139 new_header_variables.append(class_name + " *" + component_name + ";\r\n");
140 new_source_connections.append("component_manager.get_component(\""+component_name+"\", &"+component_name+");\r\n");
141 }
142
143 // Setup connections:
144 CL_DomElement connections = element.named_item("connections").to_element();
145 if (connections.is_element())
146 {
147 for (CL_DomNode connectionNode = connections.get_first_child(); !connectionNode.is_null(); connectionNode = connectionNode.get_next_sibling())
148 {
149 if (!connectionNode.is_element()) continue;
150
151 CL_DomElement connection = connectionNode.to_element();
152 if (connection.get_tag_name() != "connection") continue;
153
154 // todo: Update new_header_functions
155 // todo: Update new_source_connections
156 // todo: Search old_header_functions for this slot.
157 // If its not there; append function body template to cpp file.
158 }
159 }
160
161 // Parse any child components, if any.
162 if (element.named_item("components").is_element())
163 {
164 bool result = parse_components(element.named_item("components").to_element());
165 if (result == false) return false;
166 }
167 }
168 return true;
169 }
170
171 bool GUI2CPP::load_file(std::string &file_data, const std::string &filename)
172 {
173 CL_InputSource_File input(filename);
174 int size = input.size();
175 char *data = new char[size];
176 size = input.read(data, size);
177 file_data = std::string(data, size);
178 delete[] data;
179 return true;
180 }
181
182 bool GUI2CPP::save_file(const std::string &file_data, const std::string &filename)
183 {
184 CL_OutputSource_File output(filename);
185 output.write(file_data.data(), (int) file_data.size());
186 return true;
187 }
188
189 std::string GUI2CPP::find_block(const std::string &file_data, const std::string &block_name)
190 {
191 std::string begin_marker = "// {{" + block_name;
192 std::string end_marker = "// }}" + block_name;
193
194 std::string::size_type start_pos = file_data.find(begin_marker);
195 if (start_pos == std::string::npos) return std::string();
196
197 std::string::size_type end_pos = file_data.find(end_marker, start_pos);
198 if (end_pos == std::string::npos) return std::string();
199
200 return file_data.substr(start_pos, end_pos-start_pos);
201 }
202
203 bool GUI2CPP::inject_block(
204 std::string &file_data,
205 const std::string &block_name,
206 const std::string &block_data)
207 {
208 // todo: Inject with tab/whitespace indentation derived from the file data.
209
210 std::string begin_marker = "// {{" + block_name;
211 std::string end_marker = "// }}" + block_name;
212
213 std::string::size_type start_pos = file_data.find(begin_marker);
214 if (start_pos != std::string::npos)
215 {
216 std::string::size_type end_pos = file_data.find(end_marker, start_pos);
217 if (end_pos == std::string::npos)
218 {
219 std::cout << "Found begin marker, but unable to find end marker: " << end_marker.c_str() << std::endl;
220 return false;
221 }
222
223 file_data =
224 file_data.substr(0, start_pos) +
225 begin_marker + "\r\n" + block_data + end_marker +
226 file_data.substr(end_pos+end_marker.length());
227 }
228 else
229 {
230 file_data.append(begin_marker + "\r\n");
231 file_data.append(block_data);
232 file_data.append(end_marker + "\r\n");
233 }
234
235 return true;
236 }