Panda3D
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.
17 static const int col_width = 11;
18 
19 /**
20  *
21  */
22 BinToC::
23 BinToC() :
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  */
67 void BinToC::
68 run() {
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  unsigned int ch;
103  ch = in.get();
104  while (!in.fail() && ch != EOF) {
105  if (col == 0) {
106  out << "\n ";
107  } else if (col == col_width) {
108  out << ",\n ";
109  col = 0;
110  } else {
111  out << ", ";
112  }
113  out << "0x" << std::setw(2) << ch;
114  col++;
115  count++;
116  ch = in.get();
117  }
118  out << "\n};\n\n"
119  << static_keyword << length_type << _table_name << "_len = "
120  << std::dec << count << ";\n\n";
121 }
122 
123 /**
124  *
125  */
126 bool BinToC::
127 handle_args(ProgramBase::Args &args) {
128  if (args.size() == 2 && !_got_output_filename) {
129  // The second argument, if present, is implicitly the output file.
130  _got_output_filename = true;
131  _output_filename = args[1];
132  args.pop_back();
133  }
134 
135  if (args.size() != 1) {
136  nout << "You must specify exactly one input file to read on the command line.\n";
137  return false;
138  }
139 
140  _input_filename = Filename::binary_filename(args[0]);
141  return true;
142 }
143 
144 
145 int main(int argc, char *argv[]) {
146  BinToC prog;
147  prog.parse_command_line(argc, argv);
148  prog.run();
149  return 0;
150 }
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.
Definition: filename.cxx:1863
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
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,...