From 5283a59db971ec2038149bf26ad9b3cbc4924449 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 14 Feb 2017 18:00:20 +0300 Subject: Add virtlist for file names enumeration in virtfs. --- src/utils/files.cpp | 21 +++++++++++---------- src/utils/virtfs.cpp | 19 +++++++++++++++---- src/utils/virtfs.h | 5 +++-- src/utils/virtfstools.cpp | 25 +++++++++++++------------ src/utils/virtlist.cpp | 34 ++++++++++++++++++++++++++++++++++ src/utils/virtlist.h | 39 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 28 deletions(-) create mode 100644 src/utils/virtlist.cpp create mode 100644 src/utils/virtlist.h (limited to 'src/utils') diff --git a/src/utils/files.cpp b/src/utils/files.cpp index e1685effb..57964f2f3 100644 --- a/src/utils/files.cpp +++ b/src/utils/files.cpp @@ -30,6 +30,7 @@ #include "utils/paths.h" #include "utils/virtfs.h" #include "utils/virtfstools.h" +#include "utils/virtlist.h" #include "utils/stringutils.h" #include @@ -48,8 +49,8 @@ void Files::extractLocale() VirtFs::addZipToSearchPath(fileName2, Append_false); const std::string localDir = std::string(getenv("APPDIR")).append("/"); - char **rootDirs = VirtFs::enumerateFiles("locale"); - for (char **i = rootDirs; *i; i++) + VirtList *const rootDirs = VirtFs::enumerateFiles("locale"); + FOR_EACH (StringVectCIter, i, rootDirs->names) { const std::string dir = std::string("locale/").append(*i); if (VirtFs::isDirectory(dir.c_str())) @@ -108,8 +109,8 @@ void Files::copyPhysFsDir(const std::string &restrict inDir, const std::string &restrict outDir) { mkdir_r(outDir.c_str()); - char **files = VirtFs::enumerateFiles(inDir.c_str()); - for (char **i = files; *i; i++) + VirtList *const files = VirtFs::enumerateFiles(inDir); + FOR_EACH (StringVectCIter, i, files->names) { const std::string file = std::string(inDir).append("/").append(*i); const std::string outDir2 = std::string(outDir).append("/").append(*i); @@ -210,8 +211,8 @@ int Files::copyFile(const std::string &restrict srcName, void Files::getFiles(const std::string &path, StringVect &list) { - char **const fonts = VirtFs::enumerateFiles(path.c_str()); - for (char *const *i = fonts; *i; i++) + VirtList *const fonts = VirtFs::enumerateFiles(path); + FOR_EACH (StringVectCIter, i, fonts->names) { if (!VirtFs::isDirectory((path + *i).c_str())) list.push_back(*i); @@ -221,8 +222,8 @@ void Files::getFiles(const std::string &path, StringVect &list) void Files::getDirs(const std::string &path, StringVect &list) { - char **const fonts = VirtFs::enumerateFiles(path.c_str()); - for (char *const *i = fonts; *i; i++) + VirtList *const fonts = VirtFs::enumerateFiles(path); + FOR_EACH (StringVectCIter, i, fonts->names) { if (VirtFs::isDirectory((path + *i).c_str())) list.push_back(*i); @@ -232,8 +233,8 @@ void Files::getDirs(const std::string &path, StringVect &list) void Files::getFilesWithDir(const std::string &path, StringVect &list) { - char **const fonts = VirtFs::enumerateFiles(path.c_str()); - for (char *const *i = fonts; *i; i++) + VirtList *const fonts = VirtFs::enumerateFiles(path); + FOR_EACH (StringVectCIter, i, fonts->names) { if (!VirtFs::isDirectory((path + *i).c_str())) list.push_back(path + *i); diff --git a/src/utils/virtfs.cpp b/src/utils/virtfs.cpp index 8b068f28e..6b87f87e9 100644 --- a/src/utils/virtfs.cpp +++ b/src/utils/virtfs.cpp @@ -23,6 +23,7 @@ #include "utils/checkutils.h" #include "utils/virtfile.h" #include "utils/virtfileprivate.h" +#include "utils/virtlist.h" #include #include @@ -85,9 +86,19 @@ namespace VirtFs return PHYSFS_exists(fname); } - char **enumerateFiles(const char *restrict const dir) + VirtList *enumerateFiles(const std::string &restrict dir) { - return PHYSFS_enumerateFiles(dir); + char** handle = PHYSFS_enumerateFiles(dir.c_str()); + VirtList *const files = new VirtList; + if (handle == nullptr) + return files; + for (char **i = handle; *i; i++) + { + std::string str = *i; + files->names.push_back(str); + } + PHYSFS_freeList(handle); + return files; } bool isDirectory(const char *restrict const fname) @@ -95,9 +106,9 @@ namespace VirtFs return PHYSFS_isDirectory(fname); } - void freeList(void *restrict const listVar) + void freeList(VirtList *restrict const handle) { - PHYSFS_freeList(listVar); + delete handle; } VirtFile *openRead(const char *restrict const filename) diff --git a/src/utils/virtfs.h b/src/utils/virtfs.h index dc6e1e4e0..5888d52eb 100644 --- a/src/utils/virtfs.h +++ b/src/utils/virtfs.h @@ -28,6 +28,7 @@ #include struct VirtFile; +struct VirtList; namespace VirtFs { @@ -37,9 +38,9 @@ namespace VirtFs const char *getBaseDir(); const char *getUserDir(); bool exists(const char *restrict const fname); - char **enumerateFiles(const char *restrict const dir); + VirtList *enumerateFiles(const std::string &restrict dir) RETURNS_NONNULL; bool isDirectory(const char *restrict const fname); - void freeList(void *restrict const listVar); + void freeList(VirtList *restrict const handle); VirtFile *openRead(const char *restrict const filename); VirtFile *openWrite(const char *restrict const filename); VirtFile *openAppend(const char *restrict const filename); diff --git a/src/utils/virtfstools.cpp b/src/utils/virtfstools.cpp index f7f1f793f..d804dfd41 100644 --- a/src/utils/virtfstools.cpp +++ b/src/utils/virtfstools.cpp @@ -23,6 +23,7 @@ #include "logger.h" #include "utils/virtfs.h" +#include "utils/virtlist.h" #include "debug.h" @@ -59,16 +60,16 @@ namespace VirtFs const std::string &restrict ext, const Append append) { - char **list = VirtFs::enumerateFiles(path.c_str()); - - for (char **i = list; *i; i++) + VirtList *const list = VirtFs::enumerateFiles(path); + FOR_EACH (StringVectCIter, i, list->names) { - const size_t len = strlen(*i); + const std::string str = *i; + const size_t len = str.size(); if (len > ext.length() && - !ext.compare((*i) + (len - ext.length()))) + !ext.compare(str.substr(len - ext.length()))) { - const std::string file = path + (*i); + const std::string file = path + str; const std::string realPath = std::string( VirtFs::getRealDir(file.c_str())); VirtFs::addZipToSearchPath(std::string(realPath).append( @@ -81,15 +82,15 @@ namespace VirtFs void searchAndRemoveArchives(const std::string &restrict path, const std::string &restrict ext) { - char **list = VirtFs::enumerateFiles(path.c_str()); - - for (char **i = list; *i; i++) + VirtList *const list = VirtFs::enumerateFiles(path); + FOR_EACH (StringVectCIter, i, list->names) { - const size_t len = strlen(*i); + const std::string str = *i; + const size_t len = str.size(); if (len > ext.length() && - !ext.compare((*i) + (len - ext.length()))) + !ext.compare(str.substr(len - ext.length()))) { - const std::string file = path + (*i); + const std::string file = path + str; const std::string realPath = std::string( VirtFs::getRealDir(file.c_str())); VirtFs::removeZipFromSearchPath(std::string( diff --git a/src/utils/virtlist.cpp b/src/utils/virtlist.cpp new file mode 100644 index 000000000..350de98db --- /dev/null +++ b/src/utils/virtlist.cpp @@ -0,0 +1,34 @@ +/* + * The ManaPlus Client + * Copyright (C) 2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program 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 + * any later version. + * + * This program 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 this program. If not, see . + */ + +#include "utils/virtlist.h" + +#include "utils/delete2.h" + +#include "debug.h" + +VirtList::VirtList() : + names() +{ +} + +VirtList::~VirtList() +{ +} diff --git a/src/utils/virtlist.h b/src/utils/virtlist.h new file mode 100644 index 000000000..5b1d10924 --- /dev/null +++ b/src/utils/virtlist.h @@ -0,0 +1,39 @@ +/* + * The ManaPlus Client + * Copyright (C) 2017 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program 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 + * any later version. + * + * This program 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 this program. If not, see . + */ + +#ifndef UTILS_VIRTLIST_H +#define UTILS_VIRTLIST_H + +#include "utils/stringvector.h" + +#include "localconsts.h" + +struct VirtList final +{ + VirtList(); + + A_DELETE_COPY(VirtList) + + ~VirtList(); + + StringVect names; +}; + +#endif // UTILS_VIRTLIST_H -- cgit v1.2.3-70-g09d2