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

Generated for Crystal Space by doxygen 1.2.18