00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "withOutputFile.h"
00016 #include "executionEnvironment.h"
00017 #include "zStream.h"
00018
00019 #include "pnotify.h"
00020
00021
00022
00023
00024
00025
00026 WithOutputFile::
00027 WithOutputFile(bool allow_last_param, bool allow_stdout,
00028 bool binary_output) {
00029 _allow_last_param = allow_last_param;
00030 _allow_stdout = allow_stdout;
00031 _binary_output = binary_output;
00032 _got_output_filename = false;
00033 _output_ptr = (ostream *)NULL;
00034 _owns_output_ptr = false;
00035 }
00036
00037
00038
00039
00040
00041
00042 WithOutputFile::
00043 ~WithOutputFile() {
00044 if (_owns_output_ptr) {
00045 delete _output_ptr;
00046 _owns_output_ptr = false;
00047 }
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057 ostream &WithOutputFile::
00058 get_output() {
00059 if (_output_ptr == (ostream *)NULL) {
00060 if (!_got_output_filename) {
00061
00062 if (!_allow_stdout) {
00063 nout << "No output filename specified.\n";
00064 exit(1);
00065 }
00066 _output_ptr = &cout;
00067 _owns_output_ptr = false;
00068
00069 } else {
00070
00071 unlink(_output_filename.c_str());
00072 _output_filename.make_dir();
00073
00074 bool pz_file = false;
00075 #ifdef HAVE_ZLIB
00076 if (_output_filename.get_extension() == "pz") {
00077
00078
00079 pz_file = true;
00080 }
00081 #endif // HAVE_ZLIB
00082
00083 if (_binary_output || pz_file) {
00084 _output_filename.set_binary();
00085 } else {
00086 _output_filename.set_text();
00087 }
00088
00089 _output_stream.clear();
00090 if (!_output_filename.open_write(_output_stream)) {
00091 nout << "Unable to write to " << _output_filename << "\n";
00092 exit(1);
00093 }
00094 nout << "Writing " << _output_filename << "\n";
00095 _output_ptr = &_output_stream;
00096 _owns_output_ptr = false;
00097
00098 #ifdef HAVE_ZLIB
00099 if (pz_file) {
00100 _output_ptr = new OCompressStream(_output_ptr, _owns_output_ptr);
00101 _owns_output_ptr = true;
00102 }
00103 #endif // HAVE_ZLIB
00104 }
00105 }
00106 return *_output_ptr;
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116 void WithOutputFile::
00117 close_output() {
00118 if (_owns_output_ptr) {
00119 delete _output_ptr;
00120 _owns_output_ptr = false;
00121 }
00122 _output_ptr = NULL;
00123 _output_stream.close();
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 bool WithOutputFile::
00136 has_output_filename() const {
00137 return _got_output_filename;
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147 Filename WithOutputFile::
00148 get_output_filename() const {
00149 if (_got_output_filename) {
00150 return _output_filename;
00151 }
00152 return Filename();
00153 }
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 bool WithOutputFile::
00169 check_last_arg(ProgramBase::Args &args, int minimum_args) {
00170 if (_allow_last_param && !_got_output_filename &&
00171 (int)args.size() > minimum_args) {
00172 Filename filename = Filename::from_os_specific(args.back());
00173
00174 if (!_preferred_extension.empty() &&
00175 ("." + filename.get_extension()) != _preferred_extension) {
00176
00177 if (!_allow_stdout) {
00178 nout << "Output filename " << filename
00179 << " does not end in " << _preferred_extension
00180 << ". If this is really what you intended, "
00181 "use the -o output_file syntax.\n";
00182 return false;
00183 }
00184
00185 } else {
00186
00187 _got_output_filename = true;
00188 _output_filename = filename;
00189 args.pop_back();
00190
00191 if (!verify_output_file_safe()) {
00192 return false;
00193 }
00194 }
00195 }
00196
00197 return true;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 bool WithOutputFile::
00215 verify_output_file_safe() const {
00216 nassertr(_got_output_filename, false);
00217
00218 if (_output_filename.exists()) {
00219 nout << "The output filename " << _output_filename << " already exists. "
00220 "If you wish to overwrite it, you must use the -o option to specify "
00221 "the output filename, instead of simply specifying it as the last "
00222 "parameter.\n";
00223 return false;
00224 }
00225 return true;
00226 }