Panda3D

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 
00019 #ifndef HAVE_GETOPT
00020   #include "gnu_getopt.h"
00021 #else
00022   #ifdef PHAVE_GETOPT_H
00023     #include <getopt.h>
00024   #endif
00025 #endif
00026 
00027 void
00028 usage() {
00029   cerr
00030     << "\nUsage:\n"
00031     << "   punzip file.pz [file2.pz file3.pz ...]\n"
00032     << "   punzip -c <file >dest_file\n"
00033     << "   punzip -o dest_file file.pz\n\n"
00034     
00035     << "This program reverses the operation of a previous pzip command.  It\n"
00036     << "uncompresses the contents of the named source file(s) and removes the .pz\n"
00037     << "extension.\n\n";
00038 }
00039 
00040 int
00041 main(int argc, char *argv[]) {
00042   extern char *optarg;
00043   extern int optind;
00044   const char *optstr = "o:ch";
00045 
00046   Filename dest_filename;
00047   bool got_dest_filename = false;
00048   bool use_stdout = false;
00049 
00050   int flag = getopt(argc, argv, optstr);
00051 
00052   while (flag != EOF) {
00053     switch (flag) {
00054     case 'o':
00055       dest_filename = Filename::from_os_specific(optarg);
00056       got_dest_filename = true;
00057       break;
00058 
00059     case 'c':
00060       use_stdout = true;
00061       break;
00062 
00063     case 'h':
00064     case '?':
00065     default:
00066       usage();
00067       return 1;
00068     }
00069     flag = getopt(argc, argv, optstr);
00070   }
00071 
00072   argc -= (optind-1);
00073   argv += (optind-1);
00074 
00075   if (use_stdout) {
00076     if (argc > 1) {
00077       cerr << "No filenames allowed in conjunction with -c.\n";
00078       return 1;
00079     }
00080 
00081     bool success = decompress_stream(cin, cout);
00082     if (!success) {
00083       cerr << "Failure compressing standard input\n";
00084       return 1;
00085     }
00086     return 0;
00087   }
00088 
00089   if (argc < 2) {
00090     usage();
00091     return 1;
00092   }
00093 
00094   if (got_dest_filename && argc > 2) {
00095     cerr << "Only one input file allowed in conjunction with -o.\n";
00096     return 1;
00097   }
00098 
00099   bool all_ok = true;
00100   for (int i = 1; i < argc; i++) {
00101     Filename source_file = Filename::from_os_specific(argv[i]);
00102     if (!got_dest_filename && source_file.get_extension() != "pz") {
00103       cerr << source_file 
00104            << " doesn't end in .pz; can't derive filename of output file.\n";
00105       all_ok = false;
00106 
00107     } else {
00108       Filename dest_file = dest_filename;
00109       if (!got_dest_filename) {
00110         dest_file = source_file.get_fullpath_wo_extension();
00111       }
00112 
00113       // Open source file
00114       pifstream read_stream;
00115       source_file.set_binary();
00116       if (!source_file.open_read(read_stream)) {
00117         cerr << "Couldn't read: " << source_file << endl;
00118         all_ok = false;
00119 
00120       } else {
00121         // Open destination file
00122         pofstream write_stream;
00123         dest_file.set_binary();
00124         if (!dest_file.open_write(write_stream, true)) {
00125           cerr << "Failed to open: " << dest_file << endl;
00126           all_ok = false;
00127 
00128         } else {
00129           cerr << dest_file << "\n";
00130           bool success = decompress_stream(read_stream, write_stream);
00131           
00132           read_stream.close();
00133           write_stream.close();
00134           
00135           if (!success) {
00136             cerr << "Failure decompressing " << source_file << "\n";
00137             all_ok = false;
00138             dest_file.unlink();
00139             
00140           } else {
00141             if (!got_dest_filename) {
00142               source_file.unlink();
00143             }
00144           }
00145         }
00146       }
00147     }
00148   }
00149 
00150   if (all_ok) {
00151     return 0;
00152   } else {
00153     return 1;
00154   }
00155 }
 All Classes Functions Variables Enumerations