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 "iutil/databuff.h" 00030 #include "csutil/csstring.h" 00031 #include "csutil/databuf.h" 00032 #include "csutil/parray.h" 00033 #include "csutil/ref.h" 00034 #include "csutil/stringarray.h" 00035 #include "csutil/zip.h" 00036 00037 struct csFileTime; 00038 00058 class CS_CRYSTALSPACE_EXPORT csArchive 00059 { 00060 public: 00061 static const char hdr_central[4]; 00062 static const char hdr_local[4]; 00063 static const char hdr_endcentral[4]; 00064 static const char hdr_extlocal[4]; 00065 00066 private: 00068 class ArchiveEntry 00069 { 00070 public: 00071 char *filename; 00072 ZIP_central_directory_file_header info; 00073 char *buffer; 00074 size_t buffer_pos; 00075 size_t buffer_size; 00076 char *extrafield, *comment; 00077 bool faked; 00078 00079 ArchiveEntry (const char *name, ZIP_central_directory_file_header &cdfh); 00080 ~ArchiveEntry (); 00081 bool Append (const void *data, size_t size); 00082 bool WriteLFH (FILE *file); 00083 bool WriteCDFH (FILE *file); 00084 bool ReadExtraField (FILE *file, size_t extra_field_length); 00085 bool ReadFileComment (FILE *file, size_t file_comment_length); 00086 bool WriteFile (FILE *file); 00087 void FreeBuffer (); 00088 }; 00089 friend class ArchiveEntry; 00090 00092 class CS_CRYSTALSPACE_EXPORT ArchiveEntryVector 00093 : public csPDelArray<ArchiveEntry> 00094 { 00095 public: 00096 ArchiveEntryVector () : csPDelArray<ArchiveEntry> (256, 256) {} 00097 static int Compare (ArchiveEntry* const& Item1, ArchiveEntry* const& Item2) 00098 { return strcmp (Item1->filename, Item2->filename); } 00099 static int CompareKey (ArchiveEntry* const& Item, char const* const& Key) 00100 { return strcmp (Item->filename, Key); } 00101 }; 00102 00103 ArchiveEntryVector dir; // Archive directory: chain head (sorted) 00104 csStringArray del; // Files that should be deleted (sorted) 00105 csArray<ArchiveEntry*> lazy; // Lazy operations (unsorted) 00106 00107 char *filename; // Archive file name 00108 FILE *file; // Archive file pointer. 00109 00110 size_t comment_length; // Archive comment length 00111 char *comment; // Archive comment 00112 00113 void ReadDirectory (); 00114 bool IsDeleted (const char *name) const; 00115 void UnpackTime (ush zdate, ush ztime, csFileTime &rtime) const; 00116 void PackTime (const csFileTime &ztime, ush &rdate, ush &rtime) const; 00117 bool ReadArchiveComment (FILE *file, size_t zipfile_comment_length); 00118 void LoadECDR (ZIP_end_central_dir_record &ecdr, char *buff); 00119 bool ReadCDFH (ZIP_central_directory_file_header &cdfh, FILE *file); 00120 bool ReadLFH (ZIP_local_file_header &lfh, FILE *file); 00121 bool WriteECDR (ZIP_end_central_dir_record &ecdr, FILE *file); 00122 bool WriteZipArchive (); 00123 bool WriteCentralDirectory (FILE *temp); 00124 void UpdateDirectory (); 00125 void ReadZipDirectory (FILE *infile); 00126 ArchiveEntry *InsertEntry (const char *name, 00127 ZIP_central_directory_file_header &cdfh); 00128 void ReadZipEntries (FILE *infile); 00129 bool ReadEntry (FILE *infile, ArchiveEntry *f, char* buf); 00130 ArchiveEntry *CreateArchiveEntry (const char *name, 00131 size_t size = 0, bool pack = true); 00132 void ResetArchiveEntry (ArchiveEntry *f, size_t size, bool pack); 00133 00134 public: 00136 csArchive (const char *filename); 00138 ~csArchive (); 00139 00141 void Dir () const; 00142 00157 void *NewFile (const char *name, size_t size = 0, bool pack = true); 00158 00163 bool DeleteFile (const char *name); 00164 00169 bool FileExists (const char *name, size_t *size = 0) const; 00170 00177 char *Read (const char *name, size_t *size = 0); 00178 00183 template<typename Allocator> 00184 csPtr<iDataBuffer> Read (const char *name, Allocator& alloc) 00185 { 00186 ArchiveEntry *f = (ArchiveEntry *) FindName (name); 00187 00188 if (!f) 00189 return 0; 00190 00191 csRef<iDataBuffer> buf; 00192 buf.AttachNew (new CS::DataBuffer<Allocator> (f->info.ucsize, alloc)); 00193 if (!ReadEntry (file, f, buf->GetData())) 00194 return 0; 00195 return csPtr<iDataBuffer> (buf); 00196 } 00197 00202 csPtr<iDataBuffer> Read (const char *name) 00203 { 00204 CS::Memory::AllocatorMalloc alloc; 00205 return Read (name, alloc); 00206 } 00207 00214 bool Write (void *entry, const char *data, size_t size); 00215 00225 bool Flush (); 00226 00228 void *GetFile (size_t no) 00229 { return (no < dir.GetSize ()) ? dir.Get (no) : 0; } 00230 00232 void *FindName (const char *name) const; 00234 char *GetFileName (void *entry) const 00235 { return ((ArchiveEntry*)entry)->filename; } 00237 size_t GetFileSize (void *entry) const 00238 { return ((ArchiveEntry*)entry)->info.ucsize; } 00240 void GetFileTime (void *entry, csFileTime &ztime) const; 00242 void SetFileTime (void *entry, const csFileTime &ztime); 00243 00245 char *GetName () const 00246 { return filename; } 00248 char *GetComment () const 00249 { return comment; } 00250 }; 00251 00252 inline void csArchive::GetFileTime (void *entry, csFileTime &ztime) const 00253 { 00254 if (entry) 00255 { 00256 UnpackTime (((ArchiveEntry*)entry)->info.last_mod_file_date, 00257 ((ArchiveEntry*)entry)->info.last_mod_file_time, 00258 ztime); 00259 } 00260 } 00261 00262 inline void csArchive::SetFileTime (void *entry, const csFileTime &ztime) 00263 { 00264 if (entry) 00265 { 00266 PackTime (ztime, 00267 ((ArchiveEntry*)entry)->info.last_mod_file_date, 00268 ((ArchiveEntry*)entry)->info.last_mod_file_time); 00269 } 00270 } 00271 00272 #endif // __CS_ARCHIVE_H__
Generated for Crystal Space 1.4.1 by doxygen 1.7.1