Panda3D
Loading...
Searching...
No Matches
binToC.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file binToC.cxx
10 * @author drose
11 * @date 2003-07-18
12 */
13
14#include "binToC.h"
15
16// The number of bytes across the page to write.
17static const int col_width = 11;
18
19/**
20 *
21 */
22BinToC::
23BinToC() :
24 WithOutputFile(true, true, false)
25{
26 clear_runlines();
27 add_runline("input output.c");
28 add_runline("input -o output.c");
29 add_runline("input >output.c");
30
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.");
37
38 add_option
39 ("n", "name", 0,
40 "Specify the name of the table that is generated.",
41 &BinToC::dispatch_string, nullptr, &_table_name);
42
43 add_option
44 ("static", "", 0,
45 "Flag the table with the keyword 'static'.",
46 &BinToC::dispatch_none, &_static_table);
47
48 add_option
49 ("string", "", 0,
50 "Define the table suitablly to pass to a string constructor.",
51 &BinToC::dispatch_none, &_for_string);
52
53 add_option
54 ("o", "filename", 0,
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 "
58 "other parameters.",
59 &BinToC::dispatch_filename, &_got_output_filename, &_output_filename);
60
61 _table_name = "data";
62}
63
64/**
65 *
66 */
67void BinToC::
68run() {
69 std::ifstream in;
70 if (!_input_filename.open_read(in)) {
71 nout << "Unable to read " << _input_filename << ".\n";
72 exit(1);
73 }
74
75 std::ostream &out = get_output();
76 std::string static_keyword;
77 if (_static_table) {
78 static_keyword = "static ";
79 }
80
81 std::string table_type = "const unsigned char ";
82 std::string length_type = "const int ";
83 if (_for_string) {
84 // Actually, declaring the table as "const char" causes VC7 to yell about
85 // truncating all of the values >= 0x80. table_type = "const char ";
86 length_type = "const size_t ";
87 }
88
89 out << "\n"
90 << "/*\n"
91 << " * This table was generated by the command:\n"
92 << " *\n"
93 << " * " << get_exec_command() << "\n"
94 << " */\n"
95 << "\n"
96 << "#include <stddef.h>\n"
97 << "\n"
98 << static_keyword << table_type << _table_name << "[] = {";
99 out << std::hex << std::setfill('0');
100 int count = 0;
101 int col = 0;
102 int ch = in.get();
103 while (!in.fail() && ch != EOF) {
104 if (col == 0) {
105 out << "\n ";
106 } else if (col == col_width) {
107 out << ",\n ";
108 col = 0;
109 } else {
110 out << ", ";
111 }
112 out << "0x" << std::setw(2) << (unsigned int)ch;
113 col++;
114 count++;
115 ch = in.get();
116 }
117 out << "\n};\n\n"
118 << static_keyword << length_type << _table_name << "_len = "
119 << std::dec << count << ";\n\n";
120}
121
122/**
123 *
124 */
125bool BinToC::
126handle_args(ProgramBase::Args &args) {
127 if (args.size() == 2 && !_got_output_filename) {
128 // The second argument, if present, is implicitly the output file.
129 _got_output_filename = true;
130 _output_filename = args[1];
131 args.pop_back();
132 }
133
134 if (args.size() != 1) {
135 nout << "You must specify exactly one input file to read on the command line.\n";
136 return false;
137 }
138
139 _input_filename = Filename::binary_filename(args[0]);
140 return true;
141}
142
143
144int main(int argc, char *argv[]) {
145 BinToC prog;
146 prog.parse_command_line(argc, argv);
147 prog.run();
148 return 0;
149}
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A utility program to read a (binary) file and output a table that can be compiled via a C compiler to...
Definition binToC.h:27
bool open_read(std::ifstream &stream) const
Opens the indicated ifstream for reading the file, if possible.
std::string get_exec_command() const
Returns the command that invoked this program, as a shell-friendly string, suitable for pasting into ...
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_...
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,...