blob: 5686f6b050880da1c4bf8de0d488d5abef5ee999 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001/*
2 *
3 * D-Bus++ - C++ bindings for D-Bus
4 *
5 * Copyright (C) 2005-2007 Paolo Durante <shackan@gmail.com>
6 *
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <iostream>
25#include <cstdlib>
26
27#include "generator_utils.h"
28
29using namespace std;
30
31const char *tab = " ";
32
33const char *header = "\n/*\n * This file was automatically generated by dbusxx-xml2cpp; DO NOT EDIT!\n */\n\n";
34
35const char *dbus_includes = "\n#include <dbus-c++/dbus.h>\n#include <cassert>\n";
36
37void underscorize(string &str)
38{
39 for (unsigned int i = 0; i < str.length(); ++i)
40 {
41 if (!isalpha(str[i]) && !isdigit(str[i])) str[i] = '_';
42 }
43}
44
45string stub_name(string name)
46{
47 underscorize(name);
48
49 return "_" + name + "_stub";
50}
51
52const char *atomic_type_to_string(char t)
53{
54 static struct
55 {
56 char type;
57 const char *name;
58 } atos[] =
59 {
60 { 'y', "uint8_t" },
61 { 'b', "bool" },
62 { 'n', "int16_t" },
63 { 'q', "uint16_t" },
64 { 'i', "int32_t" },
65 { 'u', "uint32_t" },
66 { 'x', "int64_t" },
67 { 't', "uint64_t" },
68 { 'd', "double" },
69 { 's', "std::string" },
70 { 'o', "::DBus::Path" },
71 { 'g', "::DBus::Signature" },
72 { 'v', "::DBus::Variant" },
73 { '\0', "" }
74 };
75 int i;
76
77 for (i = 0; atos[i].type; ++i)
78 {
79 if (atos[i].type == t) break;
80 }
81 return atos[i].name;
82}
83
84static void _parse_signature(const string &signature, string &type, unsigned int &i, bool only_once = false)
85{
86 /*cout << "signature: " << signature << endl;
87 cout << "type: " << type << endl;
88 cout << "i: " << i << ", signature[i]: " << signature[i] << endl;*/
89
90 for (; i < signature.length(); ++i)
91 {
92 switch (signature[i])
93 {
94 case 'a':
95 {
96 switch (signature[++i])
97 {
98 case '{':
99 {
100 type += "std::map< ";
101 ++i;
102 _parse_signature(signature, type, i);
103 type += " >";
104
105 break;
106 }
107 case '(':
108 {
109 type += "std::vector< ::DBus::Struct< ";
110 ++i;
111 _parse_signature(signature, type, i);
112 type += " > >";
113
114 break;
115 }
116 default:
117 {
118 type += "std::vector< ";
119 _parse_signature(signature, type, i, true);
120
121 type += " >";
122
123 break;
124 }
125 }
126 break;
127 }
128 case '(':
129 {
130 type += "::DBus::Struct< ";
131 ++i;
132
133 _parse_signature(signature, type, i);
134
135 type += " >";
136 break;
137 }
138 case ')':
139 case '}':
140 {
141 return;
142 }
143 default:
144 {
145 const char *atom = atomic_type_to_string(signature[i]);
146 if (!atom)
147 {
148 cerr << "invalid signature" << endl;
149 exit(-1);
150 }
151 type += atom;
152
153 break;
154 }
155 }
156
157 if (only_once)
158 return;
159
160 if (i + 1 < signature.length() && signature[i + 1] != ')' && signature[i + 1] != '}')
161 {
162 type += ", ";
163 }
164 }
165}
166
167string signature_to_type(const string &signature)
168{
169 string type;
170 unsigned int i = 0;
171 _parse_signature(signature, type, i);
172 return type;
173}