00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "eggMultiFilter.h"
00020
00021 #include "notify.h"
00022 #include "eggData.h"
00023
00024
00025
00026
00027
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
00064
00065
00066
00067
00068
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
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
00107 if (!_got_path_directory) {
00108
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
00121
00122
00123 exit(1);
00124 }
00125
00126 _eggs.push_back(data);
00127 }
00128
00129 return true;
00130 }
00131
00132
00133
00134
00135
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
00153
00154
00155
00156
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
00177
00178
00179
00180
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
00193 exit(1);
00194 }
00195 }
00196 }