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