File: main.cpp

package info (click to toggle)
k3d 0.8.0.2-18
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 40,948 kB
  • sloc: cpp: 171,303; ansic: 24,129; xml: 6,995; python: 5,796; makefile: 671; sh: 22
file content (102 lines) | stat: -rw-r--r-- 3,056 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <boost/algorithm/string.hpp>
#include <boost/cstdint.hpp>
#include <boost/program_options.hpp>

#include <fstream>
#include <iomanip>
#include <iostream>

namespace po = boost::program_options;

void generate_resource(const std::string& InputPath, std::istream& Input, const std::string& ResourcePath, std::ostream& Output)
{
	Output << "//////////////////////////////////////////\n";
	Output << "// Generated file, do not edit by hand! //\n";
	Output << "//////////////////////////////////////////\n\n";

	Output << "#include <k3dsdk/resource/resource.h>\n\n";

	Output << "// Resource compiled from " << InputPath << "\n\n";

	Output << "static unsigned char resource_data[] = {";
	int64_t byte_count = 0;
	Output << std::hex << std::setfill('0');
	for(int c = Input.get(); Input; c = Input.get(), ++byte_count)
	{
		if(0 != byte_count)
			Output << ", ";

		if(0 == byte_count % 8)
			Output << "\n\t";

		Output << "0x" << std::setw(2) << c;
	}
	Output << std::dec << "\n};\n\n";

	Output << "static bool success = k3d::resource::initialize(\"" << ResourcePath << "\", &resource_data[0], " << byte_count << ");\n\n";
}

int main(int argc, char* argv[])
{
	try
	{
		po::options_description visible_options("Usage: k3d-resource-compiler [OPTIONS] --input FILE --path RESOURCE --output FILE");
		visible_options.add_options()
			("help,h", "Prints this help message and exits.")
			("input", po::value<std::string>(), "Input file to be compiled.")
			("path", po::value<std::string>(), "Output resource path.")
			("output", po::value<std::string>(), "Output resource file. Use \"-\" for stdout.")
			;

		po::variables_map options;
		po::store(po::parse_command_line(argc, argv, visible_options), options);
		po::notify(options);

		if(options.count("help"))
		{
			std::cout << visible_options << "\n";
			return 0;
		}

		if(!options.count("input"))
			throw std::runtime_error("missing input file");
		std::string input_path = options["input"].as<std::string>();
		std::ifstream input_stream(input_path.c_str());

		if(!options.count("path"))
			throw std::runtime_error("missing output path");
		std::string resource_path = options["path"].as<std::string>();

		// This is a lousy damn hack, but the !@#$! msys shell automatically converts relative paths into
		// absolute paths, so we wrap the resource path in XML-style <path>...</path> 
		boost::replace_first(resource_path, "<path>", "");
		boost::replace_last(resource_path, "</path>", "");

		std::string output_path = "-";
		if(options.count("output"))
			output_path = options["output"].as<std::string>();

		if(output_path == "-")
		{
			generate_resource(input_path, input_stream, resource_path, std::cout);
		}
		else
		{
			std::ofstream output_stream(output_path.c_str());
			generate_resource(input_path, input_stream, resource_path, output_stream);
		}

		return 0;
	}
	catch(std::exception& e)
	{
		std::cerr << "K-3D Resource Compiler: " << e.what() << std::endl;
	}
	catch(...)
	{
		std::cerr << "K-3D Resource Compiler: unknown exception" << std::endl;
	}

	return 1;
}