File: gui2cpp.cpp

package info (click to toggle)
clanlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 28,372 kB
  • ctags: 16,520
  • sloc: cpp: 101,145; sh: 8,752; xml: 6,410; makefile: 1,740; ansic: 463; perl: 424; php: 247
file content (236 lines) | stat: -rw-r--r-- 6,509 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
	GUI2CPP,
	Written in the unholy year 2003 by Magnus Norddahl.

	This is a little commandline tool that sync's a c++ source file and
	header file with a ClanLib GUI definition file.
*/

#include "gui2cpp.h"
#include <iostream>

/////////////////////////////////////////////////////////////////////////////
// Application startup:

int main(int argc, char **argv)
{
	if (argc != 4)
	{
		std::cout << "Syntax: gui2cpp <gui file> <source file> <header file>" << std::endl << std::endl;
		return 255;
	}

#ifdef WIN32
	CL_SetupCore::set_instance(GetModuleHandle(0));
#endif
	CL_SetupCore::init();
	CL_SetupDisplay::init();
	CL_SetupGUI::init();

	try
	{
		GUI2CPP gui2cpp(argv[1], argv[2], argv[3]);
		if (gui2cpp.load() == false) return 1;
		if (gui2cpp.convert() == false) return 2;
		if (gui2cpp.save() == false) return 3;
	}
	catch (CL_Error err)
	{
		std::cout << err.message.c_str() << std::endl;
		return 254;
	}

	CL_SetupGUI::deinit();
	CL_SetupDisplay::deinit();
	CL_SetupCore::deinit();

	return 0;
}

/////////////////////////////////////////////////////////////////////////////
// GUI2CPP construction:

GUI2CPP::GUI2CPP(
	const std::string &gui_file,
	const std::string &source_file,
	const std::string &header_file)
	: gui_file(gui_file), source_file(source_file), header_file(header_file)
{
}

GUI2CPP::~GUI2CPP()
{
}

/////////////////////////////////////////////////////////////////////////////
// GUI2CPP attributes:

/////////////////////////////////////////////////////////////////////////////
// GUI2CPP operations:

bool GUI2CPP::load()
{
	gui_document.load(new CL_InputSource_File(gui_file), true);
	
	bool result;
	result = load_file(source_data, source_file);
	if (!result) return false;

	result = load_file(header_data, header_file);
	if (!result) return false;

	return true;
}

bool GUI2CPP::convert()
{
	bool result;

	old_header_variables = find_block(header_data, "CL_HeaderVariables");
	old_header_functions = find_block(header_data, "CL_HeaderFunctions");
	old_source_connections = find_block(source_data, "CL_SourceConnections");

	result = parse_components(gui_document.get_document_element());
	if (!result) return false;

	result = inject_block(header_data, "CL_HeaderVariables", new_header_variables);
	if (!result) return false;

	result = inject_block(header_data, "CL_HeaderFunctions", new_header_functions);
	if (!result) return false;

	result = inject_block(source_data, "CL_SourceConnections", new_source_connections);
	if (!result) return false;

	return true;
}

bool GUI2CPP::save()
{
	bool result;
	result = save_file(source_data, source_file);
	if (!result) return false;

	result = save_file(header_data, header_file);
	if (!result) return false;

	return true;
}

/////////////////////////////////////////////////////////////////////////////
// GUI2CPP implementation:

bool GUI2CPP::parse_components(const CL_DomElement &componentsElement)
{
	for (CL_DomNode node = componentsElement.get_first_child(); !node.is_null(); node = node.get_next_sibling())
	{
		if (!node.is_element()) continue;

		CL_DomElement element = node.to_element();
		std::string component_type = element.get_tag_name();
		std::string component_name = element.get_attribute("name");

		std::cout << "name: " << component_name.c_str() << ", type: " << component_type.c_str() << std::endl;

		// Setup header variables:
		if (!component_name.empty())
		{
			std::string class_name = CL_ComponentType::get_class_name(component_type);
			new_header_variables.append(class_name + " *" + component_name + ";\r\n");
			new_source_connections.append("component_manager.get_component(\""+component_name+"\", &"+component_name+");\r\n");
		}

		// Setup connections:
		CL_DomElement connections = element.named_item("connections").to_element();
		if (connections.is_element())
		{
			for (CL_DomNode connectionNode = connections.get_first_child(); !connectionNode.is_null(); connectionNode = connectionNode.get_next_sibling())
			{
				if (!connectionNode.is_element()) continue;

				CL_DomElement connection = connectionNode.to_element();
				if (connection.get_tag_name() != "connection") continue;

				// todo: Update new_header_functions
				// todo: Update new_source_connections
				// todo: Search old_header_functions for this slot.
				//  If its not there; append function body template to cpp file.
			}
		}

		// Parse any child components, if any.
		if (element.named_item("components").is_element())
		{
			bool result = parse_components(element.named_item("components").to_element());
			if (result == false) return false;
		}
	}
	return true;
}

bool GUI2CPP::load_file(std::string &file_data, const std::string &filename)
{
	CL_InputSource_File input(filename);
	int size = input.size();
	char *data = new char[size];
	size = input.read(data, size);
	file_data = std::string(data, size);
	delete[] data;
	return true;
}

bool GUI2CPP::save_file(const std::string &file_data, const std::string &filename)
{
	CL_OutputSource_File output(filename);
	output.write(file_data.data(), (int) file_data.size());
	return true;
}

std::string GUI2CPP::find_block(const std::string &file_data, const std::string &block_name)
{
	std::string begin_marker = "// {{" + block_name;
	std::string end_marker = "// }}" + block_name;

	std::string::size_type start_pos = file_data.find(begin_marker);
	if (start_pos == std::string::npos) return std::string();

	std::string::size_type end_pos = file_data.find(end_marker, start_pos);
	if (end_pos == std::string::npos) return std::string();

	return file_data.substr(start_pos, end_pos-start_pos);
}

bool GUI2CPP::inject_block(
	std::string &file_data,
	const std::string &block_name,
	const std::string &block_data)
{
	// todo: Inject with tab/whitespace indentation derived from the file data.

	std::string begin_marker = "// {{" + block_name;
	std::string end_marker = "// }}" + block_name;

	std::string::size_type start_pos = file_data.find(begin_marker);
	if (start_pos != std::string::npos)
	{
		std::string::size_type end_pos = file_data.find(end_marker, start_pos);
		if (end_pos == std::string::npos)
		{
			std::cout << "Found begin marker, but unable to find end marker: " << end_marker.c_str() << std::endl;
			return false;
		}

		file_data =
			file_data.substr(0, start_pos) +
			begin_marker + "\r\n" + block_data + end_marker +
			file_data.substr(end_pos+end_marker.length());
	}
	else
	{
		file_data.append(begin_marker + "\r\n");
		file_data.append(block_data);
		file_data.append(end_marker + "\r\n");
	}

	return true;
}