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

panda/src/downloadertools/pcompress.cxx

Go to the documentation of this file.
00001 // Filename: pcompress.cxx
00002 // Created by:  
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 "filename.h"
00020 #include "zStream.h"
00021 #include "notify.h"
00022 
00023 int
00024 main(int argc, char *argv[]) {
00025   if (argc < 2) {
00026     cerr << "Usage: pcompress <file> [<dest_file>]" << endl;
00027     return 1;
00028   }
00029 
00030   bool implicit_dest_file;
00031   Filename source_file = Filename::from_os_specific(argv[1]);
00032   Filename dest_file;
00033   if (argc < 3) {
00034     dest_file = source_file.get_fullpath() + ".pz";
00035     implicit_dest_file = true;
00036   } else {
00037     dest_file = Filename::from_os_specific(argv[2]);
00038     implicit_dest_file = false;
00039   }
00040 
00041   // Open source file
00042   ifstream read_stream;
00043   source_file.set_binary();
00044   if (!source_file.open_read(read_stream)) {
00045     cerr << "failed to open: " << source_file << endl;
00046     return 1;
00047   }
00048 
00049   // Determine source file length
00050   read_stream.seekg(0, ios::end);
00051   int source_file_length = read_stream.tellg();
00052   read_stream.seekg(0, ios::beg);
00053 
00054   if (source_file_length == 0) {
00055     cerr << "zero length file: " << source_file << endl;
00056     return 1;
00057   }
00058 
00059   // Open destination file
00060   ofstream write_stream;
00061   dest_file.set_binary();
00062   if (!dest_file.open_write(write_stream, true)) {
00063     cerr << "failed to open: " << dest_file << endl;
00064     return 1;
00065   }
00066 
00067   {
00068     OCompressStream compress(&write_stream, false);
00069     
00070     int ch = read_stream.get();
00071     while (!read_stream.eof() && !read_stream.fail()) {
00072       compress.put(ch);
00073       ch = read_stream.get();
00074     }
00075   }
00076 
00077   read_stream.close();
00078   write_stream.close();
00079 
00080   if (implicit_dest_file) {
00081     source_file.unlink();
00082   }
00083 
00084   return 0;
00085 }

Generated on Fri May 2 00:36:53 2003 for Panda by doxygen1.3