summaryrefslogtreecommitdiff
path: root/tools/generate_adaptor.cpp
blob: 4c2f3202756f3dd59f64c8b5bc6fe7a9395d31c4 (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
 *
 *  D-Bus++ - C++ bindings for D-Bus
 *
 *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
 *
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>

#include "generator_utils.h"
#include "generate_adaptor.h"

using namespace std;
using namespace DBus;

extern const char *tab;
extern const char *header;
extern const char *dbus_includes;

/*! Generate adaptor code for a XML introspection
  */
void generate_adaptor(Xml::Document &doc, const char *filename)
{
	ostringstream body;
	ostringstream head;
	vector <string> include_vector;

	head << header;
	string filestring = filename;
	underscorize(filestring);

	string cond_comp = "__dbusxx__" + filestring + "__ADAPTOR_MARSHAL_H";

	head << "#ifndef " << cond_comp << endl
	<< "#define " << cond_comp << endl;

	head << dbus_includes;

	Xml::Node &root = *(doc.root);
	Xml::Nodes interfaces = root["interface"];

	// iterate over all interface definitions
	for (Xml::Nodes::iterator i = interfaces.begin(); i != interfaces.end(); ++i)
	{
		Xml::Node &iface = **i;
		Xml::Nodes methods = iface["method"];
		Xml::Nodes signals = iface["signal"];
		Xml::Nodes properties = iface["property"];
		Xml::Nodes ms;
		ms.insert(ms.end(), methods.begin(), methods.end());
		ms.insert(ms.end(), signals.begin(), signals.end());

		// gets the name of a interface: <interface name="XYZ">
		string ifacename = iface.get("name");

		// these interface names are skipped.
		if (ifacename == "org.freedesktop.DBus.Introspectable"
		        ||ifacename == "org.freedesktop.DBus.Properties")
		{
			cerr << "skipping interface " << ifacename << endl;
			continue;
		}

		istringstream ss(ifacename);
		string nspace;
		unsigned int nspaces = 0;

		// prints all the namespaces defined with <interface name="X.Y.Z">
		while (ss.str().find('.', ss.tellg()) != string::npos)
		{
			getline(ss, nspace, '.');

			body << "namespace " << nspace << " {" << endl;

			++nspaces;
		}
		body << endl;

		string ifaceclass;

		getline(ss, ifaceclass);

		// a "_adaptor" is added to class name to distinguish between proxy and adaptor
		ifaceclass += "_adaptor";

		cerr << "generating code for interface " << ifacename << "..." << endl;

		// the code from class definiton up to opening of the constructor is generated...
		body << "class " << ifaceclass << endl
		<< ": public ::DBus::InterfaceAdaptor" << endl
		<< "{" << endl
		<< "public:" << endl
		<< endl
		<< tab << ifaceclass << "()" << endl
		<< tab << ": ::DBus::InterfaceAdaptor(\"" << ifacename << "\")" << endl
		<< tab << "{" << endl;

		// generates code to bind the properties
		for (Xml::Nodes::iterator pi = properties.begin(); pi != properties.end(); ++pi)
		{
			Xml::Node &property = **pi;

			body << tab << tab << "bind_property("
			<< property.get("name") << ", "
			<< "\"" << property.get("type") << "\", "
			<< (property.get("access").find("read") != string::npos
			    ? "true"
			    : "false")
			<< ", "
			<< (property.get("access").find("write") != string::npos
			    ? "true"
			    : "false")
			<< ");" << endl;
		}

		// generate code to register all methods
		for (Xml::Nodes::iterator mi = methods.begin(); mi != methods.end(); ++mi)
		{
			Xml::Node &method = **mi;

			body << tab << tab << "register_method("
			<< ifaceclass << ", " << method.get("name") << ", "<< stub_name(method.get("name"))
			<< ");" << endl;
		}

		body << tab << "}" << endl
		<< endl;

		body << tab << "::DBus::IntrospectedInterface *const introspect() const " << endl
		<< tab << "{" << endl;

		// generate the introspect arguments
		for (Xml::Nodes::iterator mi = ms.begin(); mi != ms.end(); ++mi)
		{
			Xml::Node &method = **mi;
			Xml::Nodes args = method["arg"];

			body << tab << tab << "static ::DBus::IntrospectedArgument " << method.get("name") << "_args[] = " << endl
			<< tab << tab << "{" << endl;

			for (Xml::Nodes::iterator ai = args.begin(); ai != args.end(); ++ai)
			{
				Xml::Node &arg = **ai;

				body << tab << tab << tab << "{ ";

				if (arg.get("name").length())
				{
					body << "\"" << arg.get("name") << "\", ";
				}
				else
				{
					body << "0, ";
				}
				body << "\"" << arg.get("type") << "\", "
				<< (arg.get("direction") == "in" ? "true" : "false")
				<< " }," << endl;
			}
			body << tab << tab << tab << "{ 0, 0, 0 }" << endl
			<< tab << tab << "};" << endl;
		}

		body << tab << tab << "static ::DBus::IntrospectedMethod " << ifaceclass << "_methods[] = " << endl
		<< tab << tab << "{" << endl;

		// generate the introspect methods
		for (Xml::Nodes::iterator mi = methods.begin(); mi != methods.end(); ++mi)
		{
			Xml::Node &method = **mi;

			body << tab << tab << tab << "{ \"" << method.get("name") << "\", " << method.get("name") << "_args }," << endl;
		}

		body << tab << tab << tab << "{ 0, 0 }" << endl
		<< tab << tab << "};" << endl;

		body << tab << tab << "static ::DBus::IntrospectedMethod " << ifaceclass << "_signals[] = " << endl
		<< tab << tab << "{" << endl;

		for (Xml::Nodes::iterator si = signals.begin(); si != signals.end(); ++si)
		{
			Xml::Node &method = **si;

			body << tab << tab << tab << "{ \"" << method.get("name") << "\", " << method.get("name") << "_args }," << endl;
		}

		body << tab << tab << tab << "{ 0, 0 }" << endl
		<< tab << tab << "};" << endl;

		body << tab << tab << "static ::DBus::IntrospectedProperty " << ifaceclass << "_properties[] = " << endl
		<< tab << tab << "{" << endl;

		for (Xml::Nodes::iterator pi = properties.begin(); pi != properties.end(); ++pi)
		{
			Xml::Node &property = **pi;

			body << tab << tab << tab << "{ "
			<< "\"" << property.get("name") << "\", "
			<< "\"" << property.get("type") << "\", "
			<< (property.get("access").find("read") != string::npos
			    ? "true"
			    : "false")
			<< ", "
			<< (property.get("access").find("write") != string::npos
			    ? "true"
			    : "false")
			<< " }," << endl;
		}


		body << tab << tab << tab << "{ 0, 0, 0, 0 }" << endl
		<< tab << tab << "};" << endl;

		// generate the Introspected interface
		body << tab << tab << "static ::DBus::IntrospectedInterface " << ifaceclass << "_interface = " << endl
		<< tab << tab << "{" << endl
		<< tab << tab << tab << "\"" << ifacename << "\"," << endl
		<< tab << tab << tab << ifaceclass << "_methods," << endl
		<< tab << tab << tab << ifaceclass << "_signals," << endl
		<< tab << tab << tab << ifaceclass << "_properties" << endl
		<< tab << tab << "};" << endl
		<< tab << tab << "return &" << ifaceclass << "_interface;" << endl
		<< tab << "}" << endl
		<< endl;

		body << "public:" << endl
		<< endl
		<< tab << "/* properties exposed by this interface, use" << endl
		<< tab << " * property() and property(value) to get and set a particular property" << endl
		<< tab << " */" << endl;

		// generate the properties code
		for (Xml::Nodes::iterator pi = properties.begin(); pi != properties.end(); ++pi)
		{
			Xml::Node &property = **pi;
			string name = property.get("name");
			string type = property.get("type");
			string type_name = signature_to_type(type);

			body << tab << "::DBus::PropertyAdaptor< " << type_name << " > " << name << ";" << endl;
		}

		body << endl;

		body << "public:" << endl
		<< endl
		<< tab << "/* methods exported by this interface," << endl
		<< tab << " * you will have to implement them in your ObjectAdaptor" << endl
		<< tab << " */" << endl;

		// generate the methods code
		for (Xml::Nodes::iterator mi = methods.begin(); mi != methods.end(); ++mi)
		{
			Xml::Node &method = **mi;
			Xml::Nodes args = method["arg"];
			Xml::Nodes args_in = args.select("direction","in");
			Xml::Nodes args_out = args.select("direction","out");
			Xml::Nodes annotations = args["annotation"];
			Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
			string arg_object;

			if (annotations_object.size() > 0)
			{
				arg_object = annotations_object.front()->get("value");
			}

			body << tab << "virtual ";

			// return type is 'void' if none or multible return values
			if (args_out.size() == 0 || args_out.size() > 1)
			{
				body << "void ";
			}
			else if (args_out.size() == 1)
			{
				// generate basic or object return type
				if (arg_object.length())
				{
					body << arg_object << " ";
				}
				else
				{
					body << signature_to_type(args_out.front()->get("type")) << " ";
				}
			}

			// generate the method name
			body << method.get("name") << "(";

			// generate the methods 'in' variables
			unsigned int i = 0;
			for (Xml::Nodes::iterator ai = args_in.begin(); ai != args_in.end(); ++ai, ++i)
			{
				Xml::Node &arg = **ai;
				Xml::Nodes annotations = arg["annotation"];
				Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
				string arg_name = arg.get("name");
				string arg_object;

				if (annotations_object.size() > 0)
				{
					arg_object = annotations_object.front()->get("value");
				}

				// generate basic signature only if no object name available...
				if (!arg_object.length())
				{
					body << "const " << signature_to_type(arg.get("type")) << "& ";
				}
				// ...or generate object style if available
				else
				{
					body << "const " << arg_object << "& ";

					// store a object name to later generate header includes
					include_vector.push_back (arg_object);
				}

				if (arg_name.length())
					body << arg_name;

				if ((i+1 != args_in.size() || args_out.size() > 1))
					body << ", ";
			}

			// generate the method 'out' variables if multibe 'out' values exist
			if (args_out.size() > 1)
			{
				unsigned int i = 0;
				for (Xml::Nodes::iterator ao = args_out.begin(); ao != args_out.end(); ++ao, ++i)
				{
					Xml::Node &arg = **ao;
					Xml::Nodes annotations = arg["annotation"];
					Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
					string arg_name = arg.get("name");
					string arg_object;

					if (annotations_object.size() > 0)
					{
						arg_object = annotations_object.front()->get("value");
					}

					// generate basic signature only if no object name available...
					if (!arg_object.length())
					{
						body << "const " << signature_to_type(arg.get("type")) << "& ";
					}
					// ...or generate object style if available
					else
					{
						body << "const " << arg_object << "& ";

						// store a object name to later generate header includes
						include_vector.push_back (arg_object);
					}

					if (arg_name.length())
						body << " " << arg_name;

					if (i+1 != args_out.size())
						body << ", ";
				}
			}
			body << ") = 0;" << endl;
		}

		body << endl
		<< "public:" << endl
		<< endl
		<< tab << "/* signal emitters for this interface" << endl
		<< tab << " */" << endl;

		// generate the signals code
		for (Xml::Nodes::iterator si = signals.begin(); si != signals.end(); ++si)
		{
			Xml::Node &signal = **si;
			Xml::Nodes args = signal["arg"];

			body << tab << "void " << signal.get("name") << "(";

			// generate the signal arguments
			unsigned int i = 0;
			for (Xml::Nodes::iterator a = args.begin(); a != args.end(); ++a, ++i)
			{
				Xml::Node &arg = **a;
				Xml::Nodes annotations = arg["annotation"];
				Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
				string arg_object;

				if (annotations_object.size() > 0)
				{
					arg_object = annotations_object.front()->get("value");
				}

				// generate basic signature only if no object name available...
				if (!arg_object.length())
				{
					body << "const " << signature_to_type(arg.get("type")) << "& arg" << i+1;
				}
				// ...or generate object style if available
				else
				{
					body << "const " << arg_object << "& arg" << i+1;

					// store a object name to later generate header includes
					include_vector.push_back (arg_object);
				}

				if (i+1 != args.size())
					body << ", ";
			}

			body << ")" << endl
			<< tab << "{" << endl
			<< tab << tab << "::DBus::SignalMessage sig(\"" << signal.get("name") <<"\");" << endl;

			// generate the signal body
			if (args.size() > 0)
			{
				body << tab << tab << "::DBus::MessageIter wi = sig.writer();" << endl;

				unsigned int i = 0;
				for (Xml::Nodes::iterator a = args.begin(); a != args.end(); ++a, ++i)
				{
					Xml::Node &arg = **a;
					Xml::Nodes annotations = arg["annotation"];
					Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
					string arg_object;

					if (annotations_object.size() > 0)
					{
						arg_object = annotations_object.front()->get("value");
					}

					if (arg_object.length())
					{
						body << tab << tab << signature_to_type(arg.get("type")) << " _arg" << i+1 << ";" << endl;
						body << tab << tab << "_arg" << i+1 << " << " << "arg" << i+1 << ";" << endl;

						body << tab << tab << "wi << _arg" << i+1 << ";" << endl;
					}
					else
					{
						body << tab << tab << "wi << arg" << i+1 << ";" << endl;
					}
				}
			}

			// emit the signal in method body
			body << tab << tab << "emit_signal(sig);" << endl
			<< tab << "}" << endl;
		}

		body << endl
		<< "private:" << endl
		<< endl
		<< tab << "/* unmarshalers (to unpack the DBus message before calling the actual interface method)" << endl
		<< tab << " */" << endl;

		// generate the unmarshalers
		for (Xml::Nodes::iterator mi = methods.begin(); mi != methods.end(); ++mi)
		{
			Xml::Node &method = **mi;
			Xml::Nodes args = method["arg"];
			Xml::Nodes args_in = args.select("direction","in");
			Xml::Nodes args_out = args.select("direction","out");

			body << tab << "::DBus::Message " << stub_name(method.get("name")) << "(const ::DBus::CallMessage &call)" << endl
			<< tab << "{" << endl
			<< tab << tab << "::DBus::MessageIter ri = call.reader();" << endl
			<< endl;

			// generate the 'in' variables
			unsigned int i = 1;
			for (Xml::Nodes::iterator ai = args_in.begin(); ai != args_in.end(); ++ai, ++i)
			{
				Xml::Node &arg = **ai;

				body << tab << tab << signature_to_type(arg.get("type")) << " argin" << i << ";" << " ";
				body << "ri >> argin" << i << ";" << endl;
			}

			// generate the 'in' object variables
			i = 1;
			for (Xml::Nodes::iterator ai = args_in.begin(); ai != args_in.end(); ++ai, ++i)
			{
				Xml::Node &arg = **ai;
				Xml::Nodes annotations = arg["annotation"];
				Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
				string arg_object;

				if (annotations_object.size() > 0)
				{
					arg_object = annotations_object.front()->get("value");
				}

				if (arg_object.length())
				{
					body << tab << tab << arg_object << " _argin" << i << ";";
					body << " " << "_argin" << i << " << " << "argin" << i << ";" << endl;
				}
			}

			// generate 'out' variables
			if (args_out.size() > 0)
			{
				unsigned int i = 1;
				for (Xml::Nodes::iterator ao = args_out.begin(); ao != args_out.end(); ++ao, ++i)
				{
					Xml::Node &arg = **ao;

					body << tab << tab << signature_to_type(arg.get("type")) << " argout" << i;

					if (args_out.size() == 1) // a single 'out' parameter will be assigned
					{
						body << " = ";
					}
					else // multible 'out' parameters will be handled as parameters below
					{
						body << ";" << endl;
					}
				}
			}

			// generate 'out' object variables
			if (args_out.size() > 0)
			{
				unsigned int i = 1;
				for (Xml::Nodes::iterator ao = args_out.begin(); ao != args_out.end(); ++ao, ++i)
				{
					Xml::Node &arg = **ao;
					Xml::Nodes annotations = arg["annotation"];
					Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
					string arg_object;

					if (annotations_object.size() > 0)
					{
						arg_object = annotations_object.front()->get("value");
					}

					// generate object types
					if (arg_object.length())
					{
						body << tab << tab << arg_object << " _argout" << i << ";" << endl;
					}
				}
			}

			// generate in '<<' operation
			i = 0;
			for (Xml::Nodes::iterator ai = args_in.begin(); ai != args_in.end(); ++ai, ++i)
			{
				Xml::Node &arg = **ai;
				Xml::Nodes annotations = arg["annotation"];
				Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
				string arg_object;

				if (annotations_object.size() > 0)
				{
					arg_object = annotations_object.front()->get("value");
				}
			}

			// do correct indent
			if (args_out.size() != 1 )
			{
				body << tab << tab;
			}

			body << method.get("name") << "(";

			// generate call stub parameters
			i = 0;
			for (Xml::Nodes::iterator ai = args_in.begin(); ai != args_in.end(); ++ai, ++i)
			{
				Xml::Node &arg = **ai;
				Xml::Nodes annotations = arg["annotation"];
				Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
				string arg_object;

				if (annotations_object.size() > 0)
				{
					arg_object = annotations_object.front()->get("value");
				}

				if (arg_object.length())
				{
					body << "_argin" << i+1;
				}
				else
				{
					body << "argin" << i+1;
				}

				if ((i+1 != args_in.size() || args_out.size() > 1))
					body << ", ";
			}

			if (args_out.size() > 1)
			{
				i = 0;
				for (Xml::Nodes::iterator ao = args_out.begin(); ao != args_out.end(); ++ao, ++i)
				{
					Xml::Node &arg = **ao;
					Xml::Nodes annotations = arg["annotation"];
					Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
					string arg_object;

					if (annotations_object.size() > 0)
					{
						arg_object = annotations_object.front()->get("value");
					}

					if (arg_object.length())
					{
						body << "_argout" << i+1;
					}
					else
					{
						body << "argout" << i+1;
					}

					if (i+1 != args_out.size())
						body << ", ";
				}
			}

			body << ");" << endl;

			body << tab << tab << "::DBus::ReturnMessage reply(call);" << endl;

			if (args_out.size() > 0)
			{
				body << tab << tab << "::DBus::MessageIter wi = reply.writer();" << endl;

				// generate out '<<' operation
				i = 0;
				for (Xml::Nodes::iterator ao = args_out.begin(); ao != args_out.end(); ++ao, ++i)
				{
					Xml::Node &arg = **ao;
					Xml::Nodes annotations = arg["annotation"];
					Xml::Nodes annotations_object = annotations.select("name","org.freedesktop.DBus.Object");
					string arg_object;

					if (annotations_object.size() > 0)
					{
						arg_object = annotations_object.front()->get("value");
					}

					if (arg_object.length())
					{
						body << tab << tab << "argout" << i+1 << " << " << "_argout" << i+1 << ";" << endl;
					}
				}

				for (unsigned int i = 0; i < args_out.size(); ++i)
				{
					body << tab << tab << "wi << argout" << i+1 << ";" << endl;
				}
			}

			body << tab << tab << "return reply;" << endl;

			body << tab << "}" << endl;
		}

		body << "};" << endl
		<< endl;

		for (unsigned int i = 0; i < nspaces; ++i)
		{
			body << "} ";
		}
		body << endl;
	}

	body << "#endif //" << cond_comp << endl;

	// remove all duplicates in the header include vector
	vector<string>::const_iterator vec_end_it = unique (include_vector.begin (), include_vector.end ());

	for (vector<string>::const_iterator vec_it = include_vector.begin ();
	        vec_it != vec_end_it;
	        ++vec_it)
	{
		const string &include = *vec_it;

		head << "#include " << "\"" << include << ".h" << "\"" << endl;
	}
	head << endl;

	ofstream file(filename);
	if (file.bad())
	{
		cerr << "unable to write file " << filename << endl;
		exit(-1);
	}

	file << head.str ();
	file << body.str ();

	file.close();
}