6 const char* hexes
= "0123456789ABCDEF";
10 encoder(std::ostream
& _output
) : output(_output
)
14 size_t operator()(unsigned char* buf
, size_t bufuse
, bool eof
)
17 std::ostringstream out
;
24 unsigned char ch
= buf
[i
];
28 out
<< "\\n\"" << std::endl
;
33 out
<< "\\x" << hexes
[(ch
>> 4)] << hexes
[ch
& 15];
34 } else if(ch
== '\"') {
36 } else if(ch
== '\\') {
41 out
<< "\\x" << hexes
[(ch
>> 4)] << hexes
[ch
& 15];
60 void do_encode(std::istream
& input
, std::ostream
& output
)
68 input
.read(buf
+ bufuse
, 4096 - bufuse
);
69 bufuse
+= input
.gcount();
73 size_t bytes
= e(reinterpret_cast<unsigned char*>(buf
), bufuse
, eof
);
74 memmove(buf
, buf
+ bytes
, bufuse
- bytes
);
76 if(eof
&& !bufuse
) break;
81 int main(int argc
, char** argv
)
84 std::cerr
<< "Usage: txt2cstr <symbol> <file>" << std::endl
;
87 std::ifstream
in(argv
[2], std::ios::binary
);
88 std::cout
<< "const char* " << argv
[1] << " =" << std::endl
;
89 do_encode(in
, std::cout
);
90 std::cout
<< ";" << std::endl
;