comparison tools/bcc/main.cpp @ 47:7097a91b08a8

Tools: add mlk-bcc tool, closes #596
author David Demelier <markand@malikania.fr>
date Wed, 07 Dec 2016 20:51:19 +0100
parents
children 858621081b95
comparison
equal deleted inserted replaced
46:b0593a3e2ca8 47:7097a91b08a8
1 /*
2 * main.cpp -- create binary data from assets
3 *
4 * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <cerrno>
20 #include <cstring>
21 #include <fstream>
22 #include <iomanip>
23 #include <iostream>
24 #include <iterator>
25
26 int main(int argc, char** argv)
27 {
28 -- argc;
29 ++ argv;
30
31 if (argc < 3) {
32 std::cerr << "usage mlk-bcc variable input output" << std::endl;
33 return 1;
34 }
35
36 std::ifstream input(argv[1], std::ifstream::in | std::ifstream::binary);
37
38 if (!input) {
39 std::cerr << argv[1] << ": " << std::strerror(errno) << std::endl;
40 return 1;
41 }
42
43 std::ofstream output(argv[2], std::ofstream::out | std::ofstream::trunc);
44
45 if (!output) {
46 std::cerr << argv[2] << ": " << std::strerror(errno) << std::endl;
47 return 1;
48 }
49
50 std::istreambuf_iterator<char> it(input);
51 std::istreambuf_iterator<char> end;
52
53 output << "const char " << argv[0] << "[] = {\n";
54
55 for (int i = 0; it != end; ) {
56 if (i == 0) {
57 output << " ";
58 }
59
60 output << std::setw(4) << static_cast<int>(*it++);
61
62 if (it != end) {
63 output << ",";
64
65 if (i++ == 11) {
66 i = 0;
67 output << "\n";
68 }
69 }
70 }
71
72 output << "\n};\n";
73 }