-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathutil.cpp
151 lines (116 loc) · 2.79 KB
/
util.cpp
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
#include "simppl/detail/util.h"
#include <cstring>
#include <cassert>
#include <cstdio>
namespace simppl
{
namespace dbus
{
namespace detail
{
char* create_objectpath(const char* iface, const char* role)
{
size_t capacity = strlen(role) + strlen(iface) + 3;
char* objectpath = new char[capacity];
sprintf(objectpath, "/%s/%s", iface, role);
char* p = objectpath;
while(*p)
{
if (*p == '.')
*p = '/';
++p;
}
return objectpath;
}
char* create_busname(const char* iface, const char* role)
{
size_t capacity = strlen(role) + strlen(iface) + 2;
char* busname = new char[capacity];
sprintf(busname, "%s.%s", iface, role);
return busname;
}
std::vector<std::string> extract_interfaces(std::size_t iface_count, const char* mangled_iface_list)
{
assert(mangled_iface_list);
assert(strcmp(mangled_iface_list, "simppl::make_typelist<") > 0);
// The `mangled_iface` string will be on the form:
//
// simppl::make_typelist<
// ns1::interface1<Args...>,
// ns2::interface2<Args...>
// >
//
// We are just interested in the "ns1::interface1" part.
std::vector<std::string> interfaces;
interfaces.reserve(iface_count);
// skip the "simppl::make_typelist<" part
const char* begin = mangled_iface_list + strlen("simppl::make_typelist<");
do {
const char* end = strchr(begin, '<');
interfaces.push_back(make_interface_name(begin, end));
begin = find_next_interface(end);
} while (begin);
return interfaces;
}
std::string make_interface_name(const char* src, const char* end)
{
assert(end > src);
std::string name(end - src, '\0');
auto dst = name.begin();
for (; src != end; ++dst, ++src) {
if (*src == ':') {
*dst = '.';
++src;
} else {
*dst = *src;
}
}
name.resize(dst-name.begin());
return name;
}
const char* find_next_interface(const char* s) {
assert(s && "input cannot be null");
assert(*s == '<' && "expected '<'");
++s;
int level = 1;
for (; s && level > 0; ++s) {
if (*s == '<') {
++level;
} else if (*s == '>') {
--level;
}
}
assert(level == 0 && "unbalanced angle brackets");
for (; isspace(*s); ++s)
;
if (*s == ',') {
for (++s; isspace(*s); ++s)
;
return s;
} else if (*s == '>') {
return nullptr;
} else {
assert(false && "expected ',' or '>'");
return nullptr;
}
}
char* make_error_name(char* str)
{
char* to = str;
char* from = str;
while(*from)
{
if (*from == ':')
{
*to++ = '.';
from += 2;
}
else
*to++ = *from++;
}
*to = '\0';
return str;
}
} // namespace detail
} // namespace dbus
} // namespace simppl