Debian Codegraph @

stdlib/to_c_array.cpp

raw
1 // Converts stdin string to a comma-separated list of byte values and prints the
2 // result. Used for transforming the standard library into a C array.
3 //
4 // Usage:
5 // to_c_array <infile> <outfile>
6 #include <fstream>
7 #include <iostream>
8 #include <iterator>
9
10 int main(int argc, char *argv[])
11 {
12 if (argc < 3) {
13 std::cerr << "usage: to_c_array <infile> <outfile>\n";
14 return 1;
15 }
16
17 std::ifstream in_file(argv[1]);
18 if (!in_file.is_open()) {
19 std::cerr << "Can't open input file.";
20 return 1;
21 }
22
23 std::ofstream out_file(argv[2]);
24 if (!out_file.is_open()) {
25 std::cerr << "Can't open output file.";
26 return 1;
27 }
28
29 char c;
30 bool first_character = true;
31 while (in_file.get(c)) {
32 if (first_character) {
33 first_character = false;
34 } else {
35 out_file << ",";
36 }
37 // Write byte value of c to stdout.
38 out_file << (int)c;
39 }
40 out_file << ",0";
41
42 return 0;
43 }