view tools/bcc/main.cpp @ 154:614ac46dac3e

Client: add basic network support
author David Demelier <markand@malikania.fr>
date Wed, 13 Dec 2017 13:48:56 +0100
parents 858621081b95
children 4b292c20124c
line wrap: on
line source

/*
 * main.cpp -- create binary data from assets
 *
 * Copyright (c) 2013-2017 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";
}