Skip to content

Commit bab49d6

Browse files
committed
unicode
1 parent 62fccbf commit bab49d6

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

bin2cpp20/bin2cpp.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ bin2cpp20 - conversion tool
1919
#include <string>
2020
#include <vector>
2121

22-
#define VERSION_INFO "1.0.1"
22+
#define VERSION_INFO "1.1.0"
2323

2424
void convertStreamToVector(std::string_view name, std::istream &in, std::ostream &out);
2525
void convertStreamToArray(std::string_view name, const char *data, std::size_t length, std::ostream &out);
26-
void convertStreamToString(bool sorted, std::string_view name, std::istream &in, std::ostream &out);
26+
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>
2929
void stringOutputArray(std::array<unsigned char, N> &a);
@@ -37,12 +37,13 @@ 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");
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");
4141
Argument<std::string> arg;
4242
int value{};
4343
std::string input_file, output_file, variable_name;
4444
bool as_string {false};
4545
bool sorted {false};
46+
bool unicode {false};
4647
while((value = argz.proc(arg)) != -1) {
4748
switch(value) {
4849
case 'i':
@@ -71,6 +72,10 @@ int main(int argc, char **argv) {
7172
case 'Z':
7273
sorted = true;
7374
break;
75+
case 'u':
76+
case 'U':
77+
unicode = true;
78+
break;
7479
}
7580
}
7681
if(input_file.length() == 0) {
@@ -90,7 +95,7 @@ int main(int argc, char **argv) {
9095

9196
if(input_file == "stdin") {
9297
if(output_file.length() == 0)
93-
convertStreamToString(sorted, variable_name, std::cin, std::cout);
98+
convertStreamToString(unicode, sorted, variable_name, std::cin, std::cout);
9499
else {
95100
std::fstream file;
96101
const auto pos{output_file.rfind(".hpp")};
@@ -104,7 +109,7 @@ int main(int argc, char **argv) {
104109
file << "#ifndef __STR_H_HPP_" << variable_name << "\n";
105110
file << "#define __STR_H_HPP_" << variable_name << "\n";
106111
file << "#include<string>\n";
107-
convertStreamToString(sorted, variable_name, std::cin, file);
112+
convertStreamToString(unicode, sorted, variable_name, std::cin, file);
108113
file << "#endif\n\n";
109114
file.close();
110115
}
@@ -128,7 +133,7 @@ int main(int argc, char **argv) {
128133
} else {
129134
variable_name = "str_" + variable_name;
130135
std::istringstream in(buf.get());
131-
convertStreamToString(sorted, variable_name, in, std::cout);
136+
convertStreamToString(unicode, sorted, variable_name, in, std::cout);
132137
}
133138
return EXIT_SUCCESS;
134139
} else {
@@ -151,7 +156,7 @@ int main(int argc, char **argv) {
151156
} else {
152157
file << "#include<string>\n\n";
153158
std::istringstream in{buf.get()};
154-
convertStreamToString(sorted, variable_name, in, file);
159+
convertStreamToString(unicode, sorted, variable_name, in, file);
155160
}
156161
file << "\n\n#endif\n";
157162
file.close();
@@ -188,8 +193,11 @@ void convertStreamToArray(std::string_view name, const char *data, std::size_t l
188193
out << hex << "};\n";
189194
}
190195

191-
void convertStreamToString(bool sorted, std::string_view name, std::istream &in, std::ostream &out) {
192-
out << "inline const std::string " << name << "[] = {";
196+
void convertStreamToString(bool unicode, bool sorted, std::string_view name, std::istream &in, std::ostream &out) {
197+
if(unicode == false)
198+
out << "inline const std::string " << name << "[] = {";
199+
else
200+
out << "inline const std::wstring " << name << "[] = {";
193201
std::size_t index{};
194202
std::size_t length{};
195203
std::vector<std::string> v;
@@ -208,7 +216,10 @@ void convertStreamToString(bool sorted, std::string_view name, std::istream &in,
208216
for(auto &line : v) {
209217
index += line.length() + 1;
210218
if(line.length() > 0) {
211-
out << "\"" << convertString(line) << "\"";
219+
if(unicode == false)
220+
out << "\"" << convertString(line) << "\"";
221+
else
222+
out << "L\"" << convertString(line) << "\"";
212223
if(index < length)
213224
out << ",";
214225
}

bin2cpp20/testprog/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
#include "test.hpp"
2+
#include "test2.hpp"
23
#include <fstream>
34
#include <iostream>
45
#include <string>
56

67
int main(int argc, char **argv) {
78

89
if(argc == 1) {
9-
for(const auto &i : value1_arr) {
10+
for(const auto &i : bin_arr) {
1011
std::cout << i;
1112
}
13+
for(int i = 0; i < var2_length; ++i) {
14+
std::wcout << var2[i] << "\n";
15+
}
1216
} else if(argc == 2) {
1317
std::fstream file;
1418
file.open(argv[1], std::ios::out | std::ios::binary);
15-
file.write(reinterpret_cast<const char *>(value1_arr.data()), value1_arr.size());
19+
file.write(reinterpret_cast<const char *>(bin_arr.data()), bin_arr.size());
1620
file.close();
1721
}
1822
return 0;

bin2cpp20/testprog/test2.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef __ARR_H_HPP_var2
2+
#define __ARR_H_HPP_var2
3+
#include<string>
4+
5+
inline const std::wstring var2[] = {L"CXXFLAGS = -std=c++20 ",L"LDFLAGS = ",L"CPP_FILES := $(wildcard *.cpp)",L"OBJ_FILES := $(CPP_FILES:.cpp=.o)",L"OUTPUT_NAME = bin2cpp20",L"DEPS := $(wildcard *.hpp)",L"%.o: %.cpp $(DEPS) ",L" $(CXX) $(CXXFLAGS) -c $< -o $@",L"$(OUTPUT_NAME): $(OBJ_FILES)",L" $(CXX) $^ -o $@ $(LDFLAGS)",L"all: $(OUTPUT_NAME)",L"install: all",L" cp $(OUTPUT_NAME) /usr/local/bin",L"clean:",L" rm -f $(OUTPUT_NAME) $(OBJ_FILES)",L".PHONY: all install clean"};
6+
inline unsigned long var2_length {16};
7+
8+
9+
#endif

0 commit comments

Comments
 (0)