-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.hpp
138 lines (112 loc) · 2.56 KB
/
util.hpp
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
#include <deque>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <boost/filesystem.hpp>
#ifndef MBED_UTIL_HPP
#define MBED_UTIL_HPP
std::string hexFor(const unsigned char c) {
std::stringstream str{};
str << std::hex << std::uppercase << std::setfill('0') << std::right;
str << "'\\x" << std::setw(2) << +c << "'";
return str.str();
}
void printHexValues(std::istream &is, std::ostream &os) {
char c;
while (true) {
is.get(c);
if (is.peek() != -1) {
os << hexFor(c) << ',';
continue;
}
os << hexFor(c);
break;
}
}
void printHexValues(std::istream &is) {
printHexValues(is, std::cout);
}
void printHexValues(std::ostream &os) {
printHexValues(std::cin, os);
}
void printHexValues() {
printHexValues(std::cin, std::cout);
}
void printVariable(
std::ostream &os,
const std::string &name
) {
os << "const char " << name << "[]";
}
void printVariable(const std::string &name) {
printVariable(std::cout, name);
}
void printExternVariable(
std::ostream &os,
const std::string &name
) {
os << "extern ";
printVariable(os, name);
os << ";";
}
void printVariableWithValue(
std::istream &is,
std::ostream &os,
const std::string &name
) {
printVariable(os, name);
os << "={";
printHexValues(is, os);
os << "};";
}
void printVariableWithValue(
std::istream &is,
const std::string &name
) {
printVariableWithValue(is, std::cout, name);
}
void printVariableWithValue(
std::ostream &os,
const std::string &name
) {
printVariableWithValue(std::cin, os, name);
}
void printVariableWithValue(const std::string &name) {
printVariableWithValue(std::cin, std::cout, name);
}
void printHeader(
std::ostream &os,
const std::string &guard
) {
os << "#ifndef MBED_" << guard << "_H""\n"
<< "#define MBED_" << guard << "_H""\n";
}
void printHeader(const std::string &guard) {
printHeader(std::cout, guard);
}
void printFooter(std::ostream &os) {
os << "\n"
"#endif";
}
void printFooter() {
printFooter(std::cout);
}
std::string transformPath(const boost::filesystem::path &p) {
auto filename = p.filename().string();
filename.insert(0, "mbed_");
return (p.parent_path() / filename).string();
}
std::string extractNameFromPath(const boost::filesystem::path &p) {
auto name = p.stem().string();
std::replace_if(name.begin(), name.end(), [](const auto &c) {
return !std::isalnum(c);
}, '_');
if (isdigit(name[0])) {
name.insert(0, "_");
}
return name;
}
std::string generateSingleName(const std::vector<std::string> &inputs) {
return inputs.empty() ? "mbed" : extractNameFromPath(inputs[0]);
}
#endif //MBED_UTIL_HPP