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 "csextern.h"
00025 #include "zip.h"
00026 #include "parray.h"
00027 #include "stringarray.h"
00028 
00029 struct csFileTime;
00030 
00051 class CS_CSUTIL_EXPORT csArchive
00052 {
00053 public:
00054   static char hdr_central[4];
00055   static char hdr_local[4];
00056   static char hdr_endcentral[4];
00057   static char hdr_extlocal[4];
00058 
00059 private:
00061   class ArchiveEntry
00062   {
00063   public:
00064     char *filename;
00065     ZIP_central_directory_file_header info;
00066     char *buffer;
00067     size_t buffer_pos;
00068     size_t buffer_size;
00069     char *extrafield, *comment;
00070     bool faked;
00071 
00072     ArchiveEntry (const char *name, ZIP_central_directory_file_header &cdfh);
00073     ~ArchiveEntry ();
00074     bool Append (const void *data, size_t size);
00075     bool WriteLFH (FILE *file);
00076     bool WriteCDFH (FILE *file);
00077     bool ReadExtraField (FILE *file, size_t extra_field_length);
00078     bool ReadFileComment (FILE *file, size_t file_comment_length);
00079     bool WriteFile (FILE *file);
00080     void FreeBuffer ();
00081   };
00082   friend class ArchiveEntry;
00083 
00085   class CS_CSUTIL_EXPORT ArchiveEntryVector : public csPDelArray<ArchiveEntry>
00086   {
00087   public:
00088     ArchiveEntryVector () : csPDelArray<ArchiveEntry> (256, 256) {}
00089     static int Compare (ArchiveEntry* const& Item1, ArchiveEntry* const& Item2)
00090     { return strcmp (Item1->filename, Item2->filename); }
00091     static int CompareKey (ArchiveEntry* const& Item, char const* const& Key)
00092     { return strcmp (Item->filename, Key); }
00093   };
00094 
00095   ArchiveEntryVector dir;       // Archive directory: chain head (sorted)
00096   csStringArray del;            // Files that should be deleted (sorted)
00097   csArray<ArchiveEntry*> lazy;  // Lazy operations (unsorted)
00098 
00099   char *filename;               // Archive file name
00100   FILE *file;                   // Archive file pointer.
00101 
00102   size_t comment_length;        // Archive comment length
00103   char *comment;                // Archive comment
00104 
00105   void ReadDirectory ();
00106   bool IsDeleted (const char *name) const;
00107   void UnpackTime (ush zdate, ush ztime, csFileTime &rtime) const;
00108   void PackTime (const csFileTime &ztime, ush &rdate, ush &rtime) const;
00109   bool ReadArchiveComment (FILE *file, size_t zipfile_comment_length);
00110   void LoadECDR (ZIP_end_central_dir_record &ecdr, char *buff);
00111   bool ReadCDFH (ZIP_central_directory_file_header &cdfh, FILE *file);
00112   bool ReadLFH (ZIP_local_file_header &lfh, FILE *file);
00113   bool WriteECDR (ZIP_end_central_dir_record &ecdr, FILE *file);
00114   bool WriteZipArchive ();
00115   bool WriteCentralDirectory (FILE *temp);
00116   void UpdateDirectory ();
00117   void ReadZipDirectory (FILE *infile);
00118   ArchiveEntry *InsertEntry (const char *name,
00119     ZIP_central_directory_file_header &cdfh);
00120   void ReadZipEntries (FILE *infile);
00121   char *ReadEntry (FILE *infile, ArchiveEntry *f);
00122   ArchiveEntry *CreateArchiveEntry (const char *name,
00123     size_t size = 0, bool pack = true);
00124   void ResetArchiveEntry (ArchiveEntry *f, size_t size, bool pack);
00125 
00126 public:
00128   csArchive (const char *filename);
00130   ~csArchive ();
00131 
00133   void Dir () const;
00134 
00149   void *NewFile (const char *name, size_t size = 0, bool pack = true);
00150 
00155   bool DeleteFile (const char *name);
00156 
00161   bool FileExists (const char *name, size_t *size = 0) const;
00162 
00169   char *Read (const char *name, size_t *size = 0);
00170 
00177   bool Write (void *entry, const char *data, size_t size);
00178 
00188   bool Flush ();
00189 
00191   void *GetFile (int no)
00192   { return (no >= 0) && (no < dir.Length ()) ? dir.Get (no) : 0; }
00193 
00195   void *FindName (const char *name) const;
00197   char *GetFileName (void *entry) const
00198   { return ((ArchiveEntry*)entry)->filename; }
00200   size_t GetFileSize (void *entry) const
00201   { return ((ArchiveEntry*)entry)->info.ucsize; }
00203   void GetFileTime (void *entry, csFileTime &ztime) const;
00205   void SetFileTime (void *entry, const csFileTime &ztime);
00206 
00208   char *GetName () const
00209   { return filename; }
00211   char *GetComment () const
00212   { return comment; }
00213 };
00214 
00215 inline void csArchive::GetFileTime (void *entry, csFileTime &ztime) const
00216 {
00217   if (entry)
00218   {
00219     UnpackTime (((ArchiveEntry*)entry)->info.last_mod_file_date,
00220                 ((ArchiveEntry*)entry)->info.last_mod_file_time,
00221                 ztime);
00222   } /* endif */
00223 }
00224 
00225 inline void csArchive::SetFileTime (void *entry, const csFileTime &ztime)
00226 {
00227   if (entry)
00228   {
00229     PackTime (ztime,
00230               ((ArchiveEntry*)entry)->info.last_mod_file_date,
00231               ((ArchiveEntry*)entry)->info.last_mod_file_time);
00232   } /* endif */
00233 }
00234 
00235 #endif // __CS_ARCHIVE_H__

Generated for Crystal Space by doxygen 1.2.18