Panda3D
 All Classes Functions Variables Enumerations
xFileTrans.cxx
1 // Filename: xFileTrans.cxx
2 // Created by: drose (03Oct04)
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 #include "xFileTrans.h"
16 #include "xFile.h"
17 #include "pystub.h"
18 
19 ////////////////////////////////////////////////////////////////////
20 // Function: XFileTrans::Constructor
21 // Access: Public
22 // Description:
23 ////////////////////////////////////////////////////////////////////
24 XFileTrans::
25 XFileTrans() :
26  WithOutputFile(true, false, true)
27 {
28  // Indicate the extension name we expect the user to supply for
29  // output files.
30  _preferred_extension = ".x";
31 
32  set_program_brief("reads and writes DirectX .x files");
33  set_program_description
34  ("This program reads a DirectX retained-mode file (.x) and writes an "
35  "essentially equivalent .x file. It is primarily useful for "
36  "debugging the X file parser that is part of the Pandatool library.");
37 
38  clear_runlines();
39  add_runline("[opts] input.x output.x");
40  add_runline("[opts] -o output.x input.x");
41 
42  add_option
43  ("o", "filename", 0,
44  "Specify the filename to which the resulting .x file will be written. "
45  "If this option is omitted, the last parameter name is taken to be the "
46  "name of the output file.",
47  &XFileTrans::dispatch_filename, &_got_output_filename, &_output_filename);
48 }
49 
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: XFileTrans::run
53 // Access: Public
54 // Description:
55 ////////////////////////////////////////////////////////////////////
56 void XFileTrans::
57 run() {
58  nout << "Reading " << _input_filename << "\n";
59 
60  XFile file;
61  if (!file.read(_input_filename)) {
62  nout << "Unable to read.\n";
63  exit(1);
64  }
65 
66  if (!file.write(get_output())) {
67  nout << "Unable to write.\n";
68  exit(1);
69  }
70 }
71 
72 
73 ////////////////////////////////////////////////////////////////////
74 // Function: XFileTrans::handle_args
75 // Access: Protected, Virtual
76 // Description:
77 ////////////////////////////////////////////////////////////////////
78 bool XFileTrans::
79 handle_args(ProgramBase::Args &args) {
80  if (!check_last_arg(args, 1)) {
81  return false;
82  }
83 
84  if (args.empty()) {
85  nout << "You must specify the .x file to read on the command line.\n";
86  return false;
87 
88  } else if (args.size() != 1) {
89  nout << "You must specify only one .x file to read on the command line.\n";
90  return false;
91  }
92 
93  _input_filename = args[0];
94 
95  return true;
96 }
97 
98 
99 int main(int argc, char *argv[]) {
100  // A call to pystub() to force libpystub.so to be linked in.
101  pystub();
102 
103  XFileTrans prog;
104  prog.parse_command_line(argc, argv);
105  prog.run();
106  return 0;
107 }
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 write(Filename filename) const
Opens the indicated filename for output and writes a parseable description of all the known distribut...
Definition: xFile.cxx:146
A program to read a X file and output an essentially similar X file.
Definition: xFileTrans.h:29
bool read(Filename filename)
Opens and reads the indicated .x file by name.
Definition: xFile.cxx:79
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...
This represents the complete contents of an X file (file.x) in memory.
Definition: xFile.h:35