@@ -22,7 +22,7 @@ bin2cpp20 - conversion tool
2222#define VERSION_INFO " 1.1.1"
2323
2424void 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);
2626void convertStreamToString (bool unicode, bool sorted, std::string_view name, std::istream &in, std::ostream &out);
2727void stringOutputVector (const std::vector<unsigned char > &v);
2828template <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
172179void 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