| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

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