17 static const int col_width = 11;
27 add_runline(
"input output.c");
28 add_runline(
"input -o output.c");
29 add_runline(
"input >output.c");
31 set_program_brief(
"convert binary data to a compilable C table");
32 set_program_description
33 (
"bin2c is a simple utility program to read a disk file, presumably " 34 "one with binary contents, and output a table that can be " 35 "compiled via a C compiler to generate the same data. It's handy " 36 "for portably importing binary data into a library or executable.");
40 "Specify the name of the table that is generated.",
41 &BinToC::dispatch_string,
nullptr, &_table_name);
45 "Flag the table with the keyword 'static'.",
46 &BinToC::dispatch_none, &_static_table);
50 "Define the table suitablly to pass to a string constructor.",
51 &BinToC::dispatch_none, &_for_string);
55 "Specify the filename to which the resulting C code will be written. " 56 "If this option is omitted, the last parameter name is taken to be the " 57 "name of the output file, or standard output is used if there are no " 59 &BinToC::dispatch_filename, &_got_output_filename, &_output_filename);
71 nout <<
"Unable to read " << _input_filename <<
".\n";
76 std::string static_keyword;
78 static_keyword =
"static ";
81 std::string table_type =
"const unsigned char ";
82 std::string length_type =
"const int ";
86 length_type =
"const size_t ";
91 <<
" * This table was generated by the command:\n" 96 <<
"#include <stddef.h>\n" 98 << static_keyword << table_type << _table_name <<
"[] = {";
99 out << std::hex << std::setfill(
'0');
104 while (!in.fail() && ch != EOF) {
107 }
else if (col == col_width) {
113 out <<
"0x" << std::setw(2) << ch;
119 << static_keyword << length_type << _table_name <<
"_len = " 120 << std::dec << count <<
";\n\n";
128 if (args.size() == 2 && !_got_output_filename) {
130 _got_output_filename =
true;
131 _output_filename = args[1];
135 if (args.size() != 1) {
136 nout <<
"You must specify exactly one input file to read on the command line.\n";
140 _input_filename = Filename::binary_filename(args[0]);
145 int main(
int argc,
char *argv[]) {
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool open_read(std::ifstream &stream) const
Opens the indicated ifstream for reading the file, if possible.
A utility program to read a (binary) file and output a table that can be compiled via a C compiler to...
std::string get_exec_command() const
Returns the command that invoked this program, as a shell-friendly string, suitable for pasting into ...
This is the bare functionality (intended to be inherited from along with ProgramBase or some derivati...
std::ostream & get_output()
Returns an output stream that corresponds to the user's intended egg file output–either stdout,...