Panda3D
 All Classes Functions Variables Enumerations
punzip.cxx
00001 // Filename: punzip.cxx
00002 // Created by:  
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "filename.h"
00016 #include "compress_string.h"
00017 #include "pnotify.h"
00018 #include "panda_getopt.h"
00019 #include "preprocess_argv.h"
00020 
00021 void
00022 usage() {
00023   cerr
00024     << "\nUsage:\n"
00025     << "   punzip file.pz [file2.pz file3.pz ...]\n"
00026     << "   punzip -c <file >dest_file\n"
00027     << "   punzip -o dest_file file.pz\n\n"
00028     
00029     << "This program reverses the operation of a previous pzip command.  It\n"
00030     << "uncompresses the contents of the named source file(s) and removes the .pz\n"
00031     << "extension.\n\n";
00032 }
00033 
00034 int
00035 main(int argc, char **argv) {
00036   extern char *optarg;
00037   extern int optind;
00038   const char *optstr = "o:ch";
00039 
00040   Filename dest_filename;
00041   bool got_dest_filename = false;
00042   bool use_stdout = false;
00043 
00044   preprocess_argv(argc, argv);
00045   int flag = getopt(argc, argv, optstr);
00046 
00047   while (flag != EOF) {
00048     switch (flag) {
00049     case 'o':
00050       dest_filename = Filename::from_os_specific(optarg);
00051       got_dest_filename = true;
00052       break;
00053 
00054     case 'c':
00055       use_stdout = true;
00056       break;
00057 
00058     case 'h':
00059     case '?':
00060     default:
00061       usage();
00062       return 1;
00063     }
00064     flag = getopt(argc, argv, optstr);
00065   }
00066 
00067   argc -= (optind-1);
00068   argv += (optind-1);
00069 
00070   if (use_stdout) {
00071     if (argc > 1) {
00072       cerr << "No filenames allowed in conjunction with -c.\n";
00073       return 1;
00074     }
00075 
00076     bool success = decompress_stream(cin, cout);
00077     if (!success) {
00078       cerr << "Failure compressing standard input\n";
00079       return 1;
00080     }
00081     return 0;
00082   }
00083 
00084   if (argc < 2) {
00085     usage();
00086     return 1;
00087   }
00088 
00089   if (got_dest_filename && argc > 2) {
00090     cerr << "Only one input file allowed in conjunction with -o.\n";
00091     return 1;
00092   }
00093 
00094   bool all_ok = true;
00095   for (int i = 1; i < argc; i++) {
00096     Filename source_file = Filename::from_os_specific(argv[i]);
00097     if (!got_dest_filename && source_file.get_extension() != "pz") {
00098       cerr << source_file 
00099            << " doesn't end in .pz; can't derive filename of output file.\n";
00100       all_ok = false;
00101 
00102     } else {
00103       Filename dest_file = dest_filename;
00104       if (!got_dest_filename) {
00105         dest_file = source_file.get_fullpath_wo_extension();
00106       }
00107 
00108       // Open source file
00109       pifstream read_stream;
00110       source_file.set_binary();
00111       if (!source_file.open_read(read_stream)) {
00112         cerr << "Couldn't read: " << source_file << endl;
00113         all_ok = false;
00114 
00115       } else {
00116         // Open destination file
00117         pofstream write_stream;
00118         dest_file.set_binary();
00119         if (!dest_file.open_write(write_stream, true)) {
00120           cerr << "Failed to open: " << dest_file << endl;
00121           all_ok = false;
00122 
00123         } else {
00124           cerr << dest_file << "\n";
00125           bool success = decompress_stream(read_stream, write_stream);
00126           
00127           read_stream.close();
00128           write_stream.close();
00129           
00130           if (!success) {
00131             cerr << "Failure decompressing " << source_file << "\n";
00132             all_ok = false;
00133             dest_file.unlink();
00134             
00135           } else {
00136             if (!got_dest_filename) {
00137               source_file.unlink();
00138             }
00139           }
00140         }
00141       }
00142     }
00143   }
00144 
00145   if (all_ok) {
00146     return 0;
00147   } else {
00148     return 1;
00149   }
00150 }
 All Classes Functions Variables Enumerations