Skip to content

Commit

Permalink
Use shared_ptrs instead of raw ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Aug 28, 2017
1 parent c04b6b5 commit 24812d4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 64 deletions.
74 changes: 31 additions & 43 deletions plugins/archivezip/GenericFileSystem.h
@@ -1,33 +1,14 @@
/*
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"

#include <map>
#include <iostream>

namespace archive
{

inline unsigned int path_get_depth(const char* path)
{
unsigned int depth = 0;
Expand Down Expand Up @@ -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_type> _file;
public:
Entry()
{}

Entry(const std::shared_ptr<file_type>& file) :
_file(file)
{}

std::shared_ptr<file_type>& file()
{
return _file;
}

bool is_directory() const
{
return !_file;
}
};

typedef std::map<Path, Entry> Entries;
Entries m_entries;
Expand All @@ -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.
Expand Down Expand Up @@ -192,4 +180,4 @@ class GenericFileSystem
}
};

#endif
}
38 changes: 18 additions & 20 deletions plugins/archivezip/ZipArchive.cpp
Expand Up @@ -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)
Expand All @@ -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<ZipRecord>& file = i->second.file();

FileInputStream::size_type position = 0;

Expand All @@ -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<StoredArchiveFile>(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<DeflatedArchiveFile>(name, _fullPath, position, file->stream_size, file->file_size);
}
}

return ArchiveFilePtr();
}

Expand All @@ -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<ZipRecord>& file = i->second.file();

{
// Guard against concurrent access
Expand All @@ -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<StoredArchiveTextFile>(name,
_fullPath,
_containingFolder,
_istream.tell(),
file->stream_size));
file->stream_size);

case ZipRecord::eDeflated:
return ArchiveTextFilePtr(new DeflatedArchiveTextFile(name,
return std::make_shared<DeflatedArchiveTextFile>(name,
_fullPath,
_containingFolder,
_istream.tell(),
file->stream_size));
file->stream_size);
}
}

return ArchiveTextFilePtr();
}

Expand All @@ -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);

Expand Down Expand Up @@ -180,24 +180,22 @@ bool ZipArchive::read_record() {

if (os::isDirectory(path))
{
_filesystem[path] = 0;
_filesystem[path].file().reset();
}
else
{
ZipFileSystem::entry_type& file = _filesystem[path];

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));
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/archivezip/ZipArchive.h
Expand Up @@ -54,7 +54,7 @@ class ZipArchive :
void forEachFile(VisitorFunc visitor, const std::string& root) override;

private:
bool read_record();
bool readZipRecord();
bool loadZipFile();
};

Expand Down

0 comments on commit 24812d4

Please sign in to comment.