CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

csutil/archive.h

00001 /*
00002     ZIP archive support for Crystal Space 3D library
00003     Copyright (C) 1998,1999 by Andrew Zabolotny <bit@eltech.ru>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_ARCHIVE_H__
00021 #define __CS_ARCHIVE_H__
00022 
00023 #include <stdio.h>
00024 #include "zip.h"
00025 #include "parray.h"
00026 #include "stringarray.h"
00027 
00028 struct csFileTime;
00029 
00050 class csArchive
00051 {
00052 public:
00053   static char hdr_central[4];
00054   static char hdr_local[4];
00055   static char hdr_endcentral[4];
00056   static char hdr_extlocal[4];
00057 
00058 private:
00060   class ArchiveEntry
00061   {
00062   public:
00063     char *filename;
00064     ZIP_central_directory_file_header info;
00065     char *buffer;
00066     size_t buffer_pos;
00067     size_t buffer_size;
00068     char *extrafield, *comment;
00069     bool faked;
00070 
00071     ArchiveEntry (const char *name, ZIP_central_directory_file_header &cdfh);
00072     ~ArchiveEntry ();
00073     bool Append (const void *data, size_t size);
00074     bool WriteLFH (FILE *file);
00075     bool WriteCDFH (FILE *file);
00076     bool ReadExtraField (FILE *file, size_t extra_field_length);
00077     bool ReadFileComment (FILE *file, size_t file_comment_length);
00078     bool WriteFile (FILE *file);
00079     void FreeBuffer ();
00080   };
00081   friend class ArchiveEntry;
00082 
00084   class ArchiveEntryVector : public csPDelArray<ArchiveEntry>
00085   {
00086   public:
00087     ArchiveEntryVector () : csPDelArray<ArchiveEntry> (256, 256) {}
00088     static int Compare (ArchiveEntry* const& Item1, ArchiveEntry* const& Item2)
00089     { return strcmp (Item1->filename, Item2->filename); }
00090     static int CompareKey (ArchiveEntry* const& Item, void* Key)
00091     { return strcmp (Item->filename, (char *)Key); }
00092   };
00093 
00094   ArchiveEntryVector dir;       // Archive directory: chain head (sorted)
00095   csStringArray del;            // Files that should be deleted (sorted)
00096   csArray<ArchiveEntry*> lazy;  // Lazy operations (unsorted)
00097 
00098   char *filename;               // Archive file name
00099   FILE *file;                   // Archive file pointer.
00100 
00101   size_t comment_length;        // Archive comment length
00102   char *comment;                // Archive comment
00103 
00104   void ReadDirectory ();
00105   bool IsDeleted (const char *name) const;
00106   void UnpackTime (ush zdate, ush ztime, csFileTime &rtime) const;
00107   void PackTime (const csFileTime &ztime, ush &rdate, ush &rtime) const;
00108   bool ReadArchiveComment (FILE *file, size_t zipfile_comment_length);
00109   void LoadECDR (ZIP_end_central_dir_record &ecdr, char *buff);
00110   bool ReadCDFH (ZIP_central_directory_file_header &cdfh, FILE *file);
00111   bool ReadLFH (ZIP_local_file_header &lfh, FILE *file);
00112   bool WriteECDR (ZIP_end_central_dir_record &ecdr, FILE *file);
00113   bool WriteZipArchive ();
00114   bool WriteCentralDirectory (FILE *temp);
00115   void UpdateDirectory ();
00116   void ReadZipDirectory (FILE *infile);
00117   ArchiveEntry *InsertEntry (const char *name,
00118     ZIP_central_directory_file_header &cdfh);
00119   void ReadZipEntries (FILE *infile);
00120   char *ReadEntry (FILE *infile, ArchiveEntry *f);
00121   ArchiveEntry *CreateArchiveEntry (const char *name,
00122     size_t size = 0, bool pack = true);
00123   void ResetArchiveEntry (ArchiveEntry *f, size_t size, bool pack);
00124 
00125 public:
00127   csArchive (const char *filename);
00129   ~csArchive ();
00130 
00132   void Dir () const;
00133 
00148   void *NewFile (const char *name, size_t size = 0, bool pack = true);
00149 
00154   bool DeleteFile (const char *name);
00155 
00160   bool FileExists (const char *name, size_t *size = 0) const;
00161 
00168   char *Read (const char *name, size_t *size = 0);
00169 
00176   bool Write (void *entry, const char *data, size_t size);
00177 
00187   bool Flush ();
00188 
00190   void *GetFile (int no)
00191   { return (no >= 0) && (no < dir.Length ()) ? dir.Get (no) : 0; }
00192 
00194   void *FindName (const char *name) const;
00196   char *GetFileName (void *entry) const
00197   { return ((ArchiveEntry*)entry)->filename; }
00199   size_t GetFileSize (void *entry) const
00200   { return ((ArchiveEntry*)entry)->info.ucsize; }
00202   void GetFileTime (void *entry, csFileTime &ztime) const;
00204   void SetFileTime (void *entry, const csFileTime &ztime);
00205 
00207   char *GetName () const
00208   { return filename; }
00210   char *GetComment () const
00211   { return comment; }
00212 };
00213 
00214 inline void csArchive::GetFileTime (void *entry, csFileTime &ztime) const
00215 {
00216   if (entry)
00217   {
00218     UnpackTime (((ArchiveEntry*)entry)->info.last_mod_file_date,
00219                 ((ArchiveEntry*)entry)->info.last_mod_file_time,
00220                 ztime);
00221   } /* endif */
00222 }
00223 
00224 inline void csArchive::SetFileTime (void *entry, const csFileTime &ztime)
00225 {
00226   if (entry)
00227   {
00228     PackTime (ztime,
00229               ((ArchiveEntry*)entry)->info.last_mod_file_date,
00230               ((ArchiveEntry*)entry)->info.last_mod_file_time);
00231   } /* endif */
00232 }
00233 
00234 #endif // __CS_ARCHIVE_H__

Generated for Crystal Space by doxygen 1.2.18