Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pandatool/src/eggbase/eggMultiFilter.cxx

Go to the documentation of this file.
00001 // Filename: eggMultiFilter.cxx
00002 // Created by:  drose (02Nov00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "eggMultiFilter.h"
00020 
00021 #include "notify.h"
00022 #include "eggData.h"
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //     Function: EggMultiFilter::Constructor
00026 //       Access: Public
00027 //  Description:
00028 ////////////////////////////////////////////////////////////////////
00029 EggMultiFilter::
00030 EggMultiFilter(bool allow_empty) : _allow_empty(allow_empty) {
00031   clear_runlines();
00032   add_runline("-o output.egg [opts] input.egg");
00033   add_runline("-d dirname [opts] file.egg [file.egg ...]");
00034   add_runline("-inplace [opts] file.egg [file.egg ...]");
00035 
00036   add_option
00037     ("o", "filename", 50,
00038      "Specify the filename to which the resulting egg file will be written.  "
00039      "This is only valid when there is only one input egg file on the command "
00040      "line.  If you want to process multiple files simultaneously, you must "
00041      "use either -d or -inplace.",
00042      &EggMultiFilter::dispatch_filename, &_got_output_filename, &_output_filename);
00043 
00044   add_option
00045     ("d", "dirname", 50,
00046      "Specify the name of the directory in which to write the resulting egg "
00047      "files.  If you are processing only one egg file, this may be omitted "
00048      "in lieu of the -o option.  If you are processing multiple egg files, "
00049      "this may be omitted only if you specify -inplace instead.",
00050      &EggMultiFilter::dispatch_filename, &_got_output_dirname, &_output_dirname);
00051 
00052   add_option
00053     ("inplace", "", 50,
00054      "If this option is given, the input egg files will be rewritten in "
00055      "place with the results.  This obviates the need to specify -d "
00056      "for an output directory; however, it's risky because the original "
00057      "input egg files are lost.",
00058      &EggMultiFilter::dispatch_none, &_inplace);
00059 }
00060 
00061 
00062 ////////////////////////////////////////////////////////////////////
00063 //     Function: EggMultiFilter::handle_args
00064 //       Access: Protected, Virtual
00065 //  Description: Does something with the additional arguments on the
00066 //               command line (after all the -options have been
00067 //               parsed).  Returns true if the arguments are good,
00068 //               false otherwise.
00069 ////////////////////////////////////////////////////////////////////
00070 bool EggMultiFilter::
00071 handle_args(ProgramBase::Args &args) {
00072   if (args.empty()) {
00073     if (!_allow_empty) {
00074       nout << "You must specify the egg file(s) to read on the command line.\n";
00075       return false;
00076     }
00077   } else {
00078     // These only apply if we have specified any egg files.
00079 
00080     if (_got_output_filename && args.size() == 1) {
00081       if (_got_output_dirname) {
00082         nout << "Cannot specify both -o and -d.\n";
00083         return false;
00084       } else if (_inplace) {
00085         nout << "Cannot specify both -o and -inplace.\n";
00086         return false;
00087       }
00088 
00089     } else {
00090       if (_got_output_filename) {
00091         nout << "Cannot use -o when multiple egg files are specified.\n";
00092         return false;
00093       }
00094 
00095       if (_got_output_dirname && _inplace) {
00096         nout << "Cannot specify both -inplace and -d.\n";
00097         return false;
00098 
00099       } else if (!_got_output_dirname && !_inplace) {
00100         nout << "You must specify either -inplace or -d.\n";
00101         return false;
00102       }
00103     }
00104   }
00105 
00106   // We need to set up _path_replace before we call read_egg().
00107   if (!_got_path_directory) {
00108     // Put in the name of the output directory.
00109     if (_got_output_filename) {
00110       _path_replace->_path_directory = _output_filename.get_dirname();
00111     } else if (_got_output_dirname) {
00112       _path_replace->_path_directory = _output_dirname;
00113     }
00114   }
00115 
00116   Args::const_iterator ai;
00117   for (ai = args.begin(); ai != args.end(); ++ai) {
00118     PT(EggData) data = read_egg(Filename::from_os_specific(*ai));
00119     if (data == (EggData *)NULL) {
00120       // Rather than returning false, we simply exit here, so the
00121       // ProgramBase won't try to tell the user how to run the program
00122       // just because we got a bad egg file.
00123       exit(1);
00124     }
00125 
00126     _eggs.push_back(data);
00127   }
00128 
00129   return true;
00130 }
00131 
00132 ////////////////////////////////////////////////////////////////////
00133 //     Function: EggMultiFilter::post_command_line
00134 //       Access: Protected, Virtual
00135 //  Description:
00136 ////////////////////////////////////////////////////////////////////
00137 bool EggMultiFilter::
00138 post_command_line() {
00139   Eggs::iterator ei;
00140   for (ei = _eggs.begin(); ei != _eggs.end(); ++ei) {
00141     EggData *data = (*ei);
00142     if (_got_coordinate_system) {
00143       data->set_coordinate_system(_coordinate_system);
00144     }
00145     append_command_comment(*data);
00146   }
00147 
00148   return EggMultiBase::post_command_line();
00149 }
00150 
00151 ////////////////////////////////////////////////////////////////////
00152 //     Function: EggMultiFilter::get_output_filename
00153 //       Access: Protected
00154 //  Description: Returns the output filename of the egg file with the
00155 //               given input filename.  This is based on the user's
00156 //               choice of -inplace, -o, or -d.
00157 ////////////////////////////////////////////////////////////////////
00158 Filename EggMultiFilter::
00159 get_output_filename(const Filename &source_filename) const {
00160   if (_got_output_filename) {
00161     nassertr(!_inplace && !_got_output_dirname && _eggs.size() == 1, Filename());
00162     return _output_filename;
00163 
00164   } else if (_got_output_dirname) {
00165     nassertr(!_inplace, Filename());
00166     Filename result = source_filename;
00167     result.set_dirname(_output_dirname);
00168     return result;
00169   }
00170 
00171   nassertr(_inplace, Filename());
00172   return source_filename;
00173 }
00174 
00175 ////////////////////////////////////////////////////////////////////
00176 //     Function: EggMultiFilter::write_eggs
00177 //       Access: Protected, Virtual
00178 //  Description: Writes out all of the egg files in the _eggs vector,
00179 //               to the output directory if one is specified, or over
00180 //               the input files if -inplace was specified.
00181 ////////////////////////////////////////////////////////////////////
00182 void EggMultiFilter::
00183 write_eggs() {
00184   Eggs::iterator ei;
00185   for (ei = _eggs.begin(); ei != _eggs.end(); ++ei) {
00186     EggData *data = (*ei);
00187     Filename filename = get_output_filename(data->get_egg_filename());
00188 
00189     nout << "Writing " << filename << "\n";
00190     filename.make_dir();
00191     if (!data->write_egg(filename)) {
00192       // Error writing an egg file; abort.
00193       exit(1);
00194     }
00195   }
00196 }

Generated on Fri May 2 03:18:35 2003 for Panda-Tool by doxygen1.3