From 24812d4f4b8e87572288496f71504a45a0444e97 Mon Sep 17 00:00:00 2001 From: codereader Date: Mon, 28 Aug 2017 16:41:40 +0200 Subject: [PATCH] Use shared_ptrs instead of raw ones. --- plugins/archivezip/GenericFileSystem.h | 74 +++++++++++--------------- plugins/archivezip/ZipArchive.cpp | 38 +++++++------ plugins/archivezip/ZipArchive.h | 2 +- 3 files changed, 50 insertions(+), 64 deletions(-) diff --git a/plugins/archivezip/GenericFileSystem.h b/plugins/archivezip/GenericFileSystem.h index 682fdc69ea..46abf811b8 100644 --- a/plugins/archivezip/GenericFileSystem.h +++ b/plugins/archivezip/GenericFileSystem.h @@ -1,26 +1,4 @@ -/* -Copyright (C) 2001-2006, William Joseph. -All Rights Reserved. - -This file is part of GtkRadiant. - -GtkRadiant is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or -(at your option) any later version. - -GtkRadiant is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with GtkRadiant; if not, write to the Free Software -Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#if !defined(INCLUDED_FS_FILESYSTEM_H) -#define INCLUDED_FS_FILESYSTEM_H +#pragma once #include "string/string.h" #include "os/path.h" @@ -28,6 +6,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include #include +namespace archive +{ + inline unsigned int path_get_depth(const char* path) { unsigned int depth = 0; @@ -83,25 +64,27 @@ class GenericFileSystem } }; - class Entry - { - file_type* m_file; - public: - Entry() : m_file(NULL) - { - } - Entry(file_type* file) : m_file(file) - { - } - file_type* file() const - { - return m_file; - } - bool is_directory() const - { - return file() == NULL; - } - }; + class Entry + { + std::shared_ptr _file; + public: + Entry() + {} + + Entry(const std::shared_ptr& file) : + _file(file) + {} + + std::shared_ptr& file() + { + return _file; + } + + bool is_directory() const + { + return !_file; + } + }; typedef std::map Entries; Entries m_entries; @@ -120,6 +103,11 @@ class GenericFileSystem return m_entries.end(); } + void clear() + { + m_entries.clear(); + } + /// \brief Returns the file at \p path. /// Creates all directories below \p path if they do not exist. /// O(log n) on average. @@ -192,4 +180,4 @@ class GenericFileSystem } }; -#endif +} diff --git a/plugins/archivezip/ZipArchive.cpp b/plugins/archivezip/ZipArchive.cpp index d149a28654..8d5e3422d5 100644 --- a/plugins/archivezip/ZipArchive.cpp +++ b/plugins/archivezip/ZipArchive.cpp @@ -28,10 +28,7 @@ ZipArchive::ZipArchive(const std::string& fullPath) : ZipArchive::~ZipArchive() { - for (ZipFileSystem::iterator i = _filesystem.begin(); i != _filesystem.end(); ++i) - { - delete i->second.file(); - } + _filesystem.clear(); } ArchiveFilePtr ZipArchive::openFile(const std::string& name) @@ -40,7 +37,7 @@ ArchiveFilePtr ZipArchive::openFile(const std::string& name) if (i != _filesystem.end() && !i->second.is_directory()) { - ZipRecord* file = i->second.file(); + const std::shared_ptr& file = i->second.file(); FileInputStream::size_type position = 0; @@ -63,11 +60,12 @@ ArchiveFilePtr ZipArchive::openFile(const std::string& name) switch (file->mode) { case ZipRecord::eStored: - return ArchiveFilePtr(new StoredArchiveFile(name, _fullPath, position, file->stream_size, file->file_size)); + return std::make_shared(name, _fullPath, position, file->stream_size, file->file_size); case ZipRecord::eDeflated: - return ArchiveFilePtr(new DeflatedArchiveFile(name, _fullPath, position, file->stream_size, file->file_size)); + return std::make_shared(name, _fullPath, position, file->stream_size, file->file_size); } } + return ArchiveFilePtr(); } @@ -77,7 +75,7 @@ ArchiveTextFilePtr ZipArchive::openTextFile(const std::string& name) if (i != _filesystem.end() && !i->second.is_directory()) { - ZipRecord* file = i->second.file(); + const std::shared_ptr& file = i->second.file(); { // Guard against concurrent access @@ -97,20 +95,21 @@ ArchiveTextFilePtr ZipArchive::openTextFile(const std::string& name) switch (file->mode) { case ZipRecord::eStored: - return ArchiveTextFilePtr(new StoredArchiveTextFile(name, + return std::make_shared(name, _fullPath, _containingFolder, _istream.tell(), - file->stream_size)); + file->stream_size); case ZipRecord::eDeflated: - return ArchiveTextFilePtr(new DeflatedArchiveTextFile(name, + return std::make_shared(name, _fullPath, _containingFolder, _istream.tell(), - file->stream_size)); + file->stream_size); } } + return ArchiveTextFilePtr(); } @@ -123,7 +122,8 @@ void ZipArchive::forEachFile(VisitorFunc visitor, const std::string& root) { _filesystem.traverse(visitor, root); } -bool ZipArchive::read_record() { +bool ZipArchive::readZipRecord() +{ zip_magic magic; istream_read_zip_magic(_istream, magic); @@ -180,7 +180,7 @@ bool ZipArchive::read_record() { if (os::isDirectory(path)) { - _filesystem[path] = 0; + _filesystem[path].file().reset(); } else { @@ -188,16 +188,14 @@ bool ZipArchive::read_record() { if (!file.is_directory()) { - rMessage() << "Warning: zip archive " - << _fullPath << " contains duplicated file: " - << path << std::endl; + rWarning() << "Zip archive " << _fullPath << " contains duplicated file: " << path << std::endl; } else { - file = new ZipRecord(position, + file.file().reset(new ZipRecord(position, compressed_size, uncompressed_size, - (compression_mode == Z_DEFLATED) ? ZipRecord::eDeflated : ZipRecord::eStored); + (compression_mode == Z_DEFLATED) ? ZipRecord::eDeflated : ZipRecord::eStored)); } } @@ -225,7 +223,7 @@ bool ZipArchive::loadZipFile() for (unsigned int i = 0; i < disk_trailer.z_entries; ++i) { - if (!read_record()) + if (!readZipRecord()) { return false; } diff --git a/plugins/archivezip/ZipArchive.h b/plugins/archivezip/ZipArchive.h index 51239d3fe7..0cd4a9d877 100644 --- a/plugins/archivezip/ZipArchive.h +++ b/plugins/archivezip/ZipArchive.h @@ -54,7 +54,7 @@ class ZipArchive : void forEachFile(VisitorFunc visitor, const std::string& root) override; private: - bool read_record(); + bool readZipRecord(); bool loadZipFile(); };