diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/bcc/main.cpp	Wed Dec 07 20:51:19 2016 +0100
@@ -0,0 +1,73 @@
+/*
+ * main.cpp -- create binary data from assets
+ *
+ * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <cerrno>
+#include <cstring>
+#include <fstream>
+#include <iomanip>
+#include <iostream>
+#include <iterator>
+
+int main(int argc, char** argv)
+{
+    -- argc;
+    ++ argv;
+
+    if (argc < 3) {
+        std::cerr << "usage mlk-bcc variable input output" << std::endl;
+        return 1;
+    }
+
+    std::ifstream input(argv[1], std::ifstream::in | std::ifstream::binary);
+
+    if (!input) {
+        std::cerr << argv[1] << ": " << std::strerror(errno) << std::endl;
+        return 1;
+    }
+
+    std::ofstream output(argv[2], std::ofstream::out | std::ofstream::trunc);
+
+    if (!output) {
+        std::cerr << argv[2] << ": " << std::strerror(errno) << std::endl;
+        return 1;
+    }
+
+    std::istreambuf_iterator<char> it(input);
+    std::istreambuf_iterator<char> end;
+
+    output << "const char " << argv[0] << "[] = {\n";
+
+    for (int i = 0; it != end; ) {
+        if (i == 0) {
+            output << "    ";
+        }
+
+        output << std::setw(4) << static_cast<int>(*it++);
+
+        if (it != end) {
+            output << ",";
+
+            if (i++ == 11) {
+                i = 0;
+                output << "\n";
+            }
+        }
+    }
+
+    output << "\n};\n";
+}