view tools/bcc/main.cpp @ 195:ae3741be8c38

Tests: don't build test libraries if disabled While here, fix a hardcoded string in add_custom_command
author David Demelier <markand@malikania.fr>
date Mon, 29 Oct 2018 13:25:36 +0100
parents 3107ce017c3a
children
line wrap: on
line source

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