20 static const int col_width = 11;
32 add_runline(
"input output.c");
33 add_runline(
"input -o output.c");
34 add_runline(
"input >output.c");
36 set_program_brief(
"convert binary data to a compilable C table");
37 set_program_description
38 (
"bin2c is a simple utility program to read a disk file, presumably "
39 "one with binary contents, and output a table that can be "
40 "compiled via a C compiler to generate the same data. It's handy "
41 "for portably importing binary data into a library or executable.");
45 "Specify the name of the table that is generated.",
46 &BinToC::dispatch_string, NULL, &_table_name);
50 "Flag the table with the keyword 'static'.",
51 &BinToC::dispatch_none, &_static_table);
55 "Define the table suitablly to pass to a string constructor.",
56 &BinToC::dispatch_none, &_for_string);
60 "Specify the filename to which the resulting C code will be written. "
61 "If this option is omitted, the last parameter name is taken to be the "
62 "name of the output file, or standard output is used if there are no "
64 &BinToC::dispatch_filename, &_got_output_filename, &_output_filename);
78 nout <<
"Unable to read " << _input_filename <<
".\n";
83 string static_keyword;
85 static_keyword =
"static ";
88 string table_type =
"const unsigned char ";
89 string length_type =
"const int ";
94 length_type =
"const size_t ";
99 <<
" * This table was generated by the command:\n"
104 <<
"#include <stddef.h>\n"
106 << static_keyword << table_type << _table_name <<
"[] = {";
107 out << hex << setfill(
'0');
112 while (!in.fail() && !in.eof()) {
115 }
else if (col == col_width) {
121 out <<
"0x" << setw(2) << ch;
127 << static_keyword << length_type << _table_name <<
"_len = "
128 << dec << count <<
";\n\n";
138 if (args.size() == 2 && !_got_output_filename) {
140 _got_output_filename =
true;
141 _output_filename = args[1];
145 if (args.size() != 1) {
146 nout <<
"You must specify exactly one input file to read on the command line.\n";
150 _input_filename = Filename::binary_filename(args[0]);
155 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_...
bool open_read(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...
This is the bare functionality (intended to be inherited from along with ProgramBase or some derivati...
ostream & get_output()
Returns an output stream that corresponds to the user's intended egg file output–either stdout...
string get_exec_command() const
Returns the command that invoked this program, as a shell-friendly string, suitable for pasting into ...