Skip to content

Commit b44dc73

Browse files
authored
added constexpr
added constexpr
1 parent 2f12c84 commit b44dc73

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

bin2cpp20/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
1+
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
22
project(bin2cpp20 LANGUAGES CXX)
33
set(CMAKE_CXX_STANDARD 20)
44
set(CMAKE_CXX_STANDARD_REQURIED ON)
55
set(CMAKE_CXX_EXTENSIONS OFF)
66
add_executable(bin2cpp20 bin2cpp.cpp)
77
include_directories({CMAKE_SOURCE_DIR})
8-
install(TARGETS bin2cpp20 DESTINATION bin)
8+
install(TARGETS bin2cpp20 DESTINATION bin)

bin2cpp20/bin2cpp.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ bin2cpp20 - conversion tool
2222
#define VERSION_INFO "1.1.1"
2323

2424
void convertStreamToVector(std::string_view name, std::istream &in, std::ostream &out);
25-
void convertStreamToArray(std::string_view name, const char *data, std::size_t length, std::ostream &out);
25+
void convertStreamToArray(bool c_expr, std::string_view name, const char *data, std::size_t length, std::ostream &out);
2626
void convertStreamToString(bool unicode, bool sorted, std::string_view name, std::istream &in, std::ostream &out);
2727
void stringOutputVector(const std::vector<unsigned char> &v);
2828
template <std::size_t N>
@@ -37,13 +37,15 @@ int main(int argc, char **argv) {
3737
}
3838
try {
3939
Argz<std::string> argz{argc, argv};
40-
argz.addOptionSingleValue('i', "input file/stdin").addOptionDoubleValue('I', "input", "input file/stdin").addOptionSingleValue('o', "output").addOptionDoubleValue('O', "output", "output file").addOptionSingle('h', "help").addOptionDouble('H', "help", "help message").addOptionSingleValue('v', "variable name").addOptionDoubleValue('V', "variable", "variable name").addOptionSingle('s', "string output").addOptionDouble('S', "string", "string output").addOptionSingle('z', "sort").addOptionDouble('Z', "sort", "sort string").addOptionSingle('u', "Unicode").addOptionDouble('U', "unicode", "unicode");
40+
argz.addOptionSingleValue('i', "input file/stdin").addOptionDoubleValue('I', "input", "input file/stdin").addOptionSingle('c', "Use constexpr").addOptionDouble('C', "constexpr", "Use constexpr").addOptionSingleValue('o', "output").addOptionDoubleValue('O', "output", "output file").addOptionSingle('h', "help").addOptionDouble('H', "help", "help message").addOptionSingleValue('v', "variable name").addOptionDoubleValue('V', "variable", "variable name").addOptionSingle('s', "string output").addOptionDouble('S', "string", "string output").addOptionSingle('z', "sort").addOptionDouble('Z', "sort", "sort string").addOptionSingle('u', "Unicode").addOptionDouble('U', "unicode", "unicode");
4141
Argument<std::string> arg;
4242
int value{};
4343
std::string input_file, output_file, variable_name;
4444
bool as_string {false};
45-
bool sorted {false};
46-
bool unicode {false};
45+
bool sorted{false};
46+
bool unicode{false};
47+
bool c_expr{false};
48+
4749
while((value = argz.proc(arg)) != -1) {
4850
switch(value) {
4951
case 'i':
@@ -76,6 +78,11 @@ int main(int argc, char **argv) {
7678
case 'U':
7779
unicode = true;
7880
break;
81+
case 'c':
82+
case 'C':
83+
c_expr = true;
84+
break;
85+
7986
}
8087
}
8188
if(input_file.length() == 0) {
@@ -129,7 +136,7 @@ int main(int argc, char **argv) {
129136
if(output_file.length() == 0) {
130137
if(as_string == false) {
131138
variable_name = "bin_" + variable_name;
132-
convertStreamToArray(variable_name + "_arr", buf.get(), len, std::cout);
139+
convertStreamToArray(c_expr, variable_name + "_arr", buf.get(), len, std::cout);
133140
} else {
134141
variable_name = "str_" + variable_name;
135142
std::istringstream in(buf.get());
@@ -152,7 +159,7 @@ int main(int argc, char **argv) {
152159
if(as_string == false) {
153160
file << "#include<array>\n\n";
154161
variable_name = "bin_" + variable_name;
155-
convertStreamToArray(variable_name + "_arr", buf.get(), len, file);
162+
convertStreamToArray(c_expr, variable_name + "_arr", buf.get(), len, file);
156163
} else {
157164
file << "#include<string>\n\n";
158165
std::istringstream in{buf.get()};
@@ -170,7 +177,7 @@ int main(int argc, char **argv) {
170177
}
171178

172179
void convertStreamToVector(std::string_view name, std::istream &in, std::ostream &out) {
173-
out << "inline const std::vector<unsigned char> " << name << " {";
180+
out << "inline const std::vector<unsigned chassr> " << name << " {";
174181
while(!in.eof()) {
175182
uint8_t c{};
176183
in.read(reinterpret_cast<char *>(&c), sizeof(uint8_t));
@@ -183,8 +190,12 @@ void convertStreamToVector(std::string_view name, std::istream &in, std::ostream
183190
out << "};\n";
184191
}
185192

186-
void convertStreamToArray(std::string_view name, const char *data, std::size_t length, std::ostream &out) {
187-
out << "inline const std::array<unsigned char, " << length + 1 << "> " << name << " {";
193+
void convertStreamToArray(bool c_expr, std::string_view name, const char *data, std::size_t length, std::ostream &out) {
194+
std::string e_type{"inline"};
195+
if(c_expr == true)
196+
e_type = "constexpr";
197+
198+
out << e_type << " std::array<unsigned char, " << length + 1 << "> " << name << " {";
188199
for(std::size_t i = 0; i < length; ++i) {
189200
const std::string hex{std::format("0x{:X}", static_cast<uint8_t>(data[i]))};
190201
out << hex << ",";

0 commit comments

Comments
 (0)