CrystalSpace

Public API Reference

csutil/archive.h

Go to the documentation of this file.
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 
00027 #include "csextern.h"
00028 
00029 #include "csutil/parray.h"
00030 #include "csutil/stringarray.h"
00031 #include "csutil/zip.h"
00032 
00033 struct csFileTime;
00034 
00054 class CS_CRYSTALSPACE_EXPORT csArchive
00055 {
00056 public:
00057   static char hdr_central[4];
00058   static char hdr_local[4];
00059   static char hdr_endcentral[4];
00060   static char hdr_extlocal[4];
00061 
00062 private:
00064   class ArchiveEntry
00065   {
00066   public:
00067     char *filename;
00068     ZIP_central_directory_file_header info;
00069     char *buffer;
00070     size_t buffer_pos;
00071     size_t buffer_size;
00072     char *extrafield, *comment;
00073     bool faked;
00074 
00075     ArchiveEntry (const char *name, ZIP_central_directory_file_header &cdfh);
00076     ~ArchiveEntry ();
00077     bool Append (const void *data, size_t size);
00078     bool WriteLFH (FILE *file);
00079     bool WriteCDFH (FILE *file);
00080     bool ReadExtraField (FILE *file, size_t extra_field_length);
00081     bool ReadFileComment (FILE *file, size_t file_comment_length);
00082     bool WriteFile (FILE *file);
00083     void FreeBuffer ();
00084   };
00085   friend class ArchiveEntry;
00086 
00088   class CS_CRYSTALSPACE_EXPORT ArchiveEntryVector
00089         : public csPDelArray<ArchiveEntry>
00090   {
00091   public:
00092     ArchiveEntryVector () : csPDelArray<ArchiveEntry> (256, 256) {}
00093     static int Compare (ArchiveEntry* const& Item1, ArchiveEntry* const& Item2)
00094     { return strcmp (Item1->filename, Item2->filename); }
00095     static int CompareKey (ArchiveEntry* const& Item, char const* const& Key)
00096     { return strcmp (Item->filename, Key); }
00097   };
00098 
00099   ArchiveEntryVector dir;       // Archive directory: chain head (sorted)
00100   csStringArray del;            // Files that should be deleted (sorted)
00101   csArray<ArchiveEntry*> lazy;  // Lazy operations (unsorted)
00102 
00103   char *filename;               // Archive file name
00104   FILE *file;                   // Archive file pointer.
00105 
00106   size_t comment_length;        // Archive comment length
00107   char *comment;                // Archive comment
00108 
00109   void ReadDirectory ();
00110   bool IsDeleted (const char *name) const;
00111   void UnpackTime (ush zdate, ush ztime, csFileTime &rtime) const;
00112   void PackTime (const csFileTime &ztime, ush &rdate, ush &rtime) const;
00113   bool ReadArchiveComment (FILE *file, size_t zipfile_comment_length);
00114   void LoadECDR (ZIP_end_central_dir_record &ecdr, char *buff);
00115   bool ReadCDFH (ZIP_central_directory_file_header &cdfh, FILE *file);
00116   bool ReadLFH (ZIP_local_file_header &lfh, FILE *file);
00117   bool WriteECDR (ZIP_end_central_dir_record &ecdr, FILE *file);
00118   bool WriteZipArchive ();
00119   bool WriteCentralDirectory (FILE *temp);
00120   void UpdateDirectory ();
00121   void ReadZipDirectory (FILE *infile);
00122   ArchiveEntry *InsertEntry (const char *name,
00123     ZIP_central_directory_file_header &cdfh);
00124   void ReadZipEntries (FILE *infile);
00125   char *ReadEntry (FILE *infile, ArchiveEntry *f);
00126   ArchiveEntry *CreateArchiveEntry (const char *name,
00127     size_t size = 0, bool pack = true);
00128   void ResetArchiveEntry (ArchiveEntry *f, size_t size, bool pack);
00129 
00130 public:
00132   csArchive (const char *filename);
00134   ~csArchive ();
00135 
00137   void Dir () const;
00138 
00153   void *NewFile (const char *name, size_t size = 0, bool pack = true);
00154 
00159   bool DeleteFile (const char *name);
00160 
00165   bool FileExists (const char *name, size_t *size = 0) const;
00166 
00173   char *Read (const char *name, size_t *size = 0);
00174 
00181   bool Write (void *entry, const char *data, size_t size);
00182 
00192   bool Flush ();
00193 
00195   void *GetFile (size_t no)
00196   { return (no < dir.Length ()) ? dir.Get (no) : 0; }
00197 
00199   void *FindName (const char *name) const;
00201   char *GetFileName (void *entry) const
00202   { return ((ArchiveEntry*)entry)->filename; }
00204   size_t GetFileSize (void *entry) const
00205   { return ((ArchiveEntry*)entry)->info.ucsize; }
00207   void GetFileTime (void *entry, csFileTime &ztime) const;
00209   void SetFileTime (void *entry, const csFileTime &ztime);
00210 
00212   char *GetName () const
00213   { return filename; }
00215   char *GetComment () const
00216   { return comment; }
00217 };
00218 
00219 inline void csArchive::GetFileTime (void *entry, csFileTime &ztime) const
00220 {
00221   if (entry)
00222   {
00223     UnpackTime (((ArchiveEntry*)entry)->info.last_mod_file_date,
00224                 ((ArchiveEntry*)entry)->info.last_mod_file_time,
00225                 ztime);
00226   }
00227 }
00228 
00229 inline void csArchive::SetFileTime (void *entry, const csFileTime &ztime)
00230 {
00231   if (entry)
00232   {
00233     PackTime (ztime,
00234               ((ArchiveEntry*)entry)->info.last_mod_file_date,
00235               ((ArchiveEntry*)entry)->info.last_mod_file_time);
00236   }
00237 }
00238 
00239 #endif // __CS_ARCHIVE_H__

Generated for Crystal Space by doxygen 1.4.6