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/CMakeLists.txt | 4 ++++ src/Makefile.am | 2 ++ src/gui/theme.cpp | 7 ++++--- src/resources/wallpaper.cpp | 17 +++++++++++------ 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 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 136 insertions(+), 37 deletions(-) create mode 100644 src/utils/virtlist.cpp create mode 100644 src/utils/virtlist.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e2c2103f3..910817468 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -891,6 +891,8 @@ SET(SRCS utils/virtfsrwops.h utils/virtfstools.cpp utils/virtfstools.h + utils/virtlist.cpp + utils/virtlist.h utils/virtfs.cpp utils/virtfs.h utils/process.cpp @@ -1775,6 +1777,8 @@ SET(DYE_CMD_SRCS utils/virtfsrwops.h utils/virtfstools.cpp utils/virtfstools.h + utils/virtlist.cpp + utils/virtlist.h utils/virtfs.cpp utils/virtfs.h utils/sdl2helper.cpp diff --git a/src/Makefile.am b/src/Makefile.am index e8c713f6b..320809175 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -551,6 +551,8 @@ BASE_SRC += events/actionevent.h \ utils/virtfsrwops.h \ utils/virtfstools.cpp \ utils/virtfstools.h \ + utils/virtlist.cpp \ + utils/virtlist.h \ utils/virtfs.cpp \ utils/virtfs.h \ utils/process.cpp \ diff --git a/src/gui/theme.cpp b/src/gui/theme.cpp index a60b01e99..96f09d324 100644 --- a/src/gui/theme.cpp +++ b/src/gui/theme.cpp @@ -47,6 +47,7 @@ #include "utils/dtor.h" #include "utils/files.h" #include "utils/virtfs.h" +#include "utils/virtlist.h" #include "debug.h" @@ -577,10 +578,10 @@ void Theme::fillFontsList(StringVect &list) void Theme::fillSoundsList(StringVect &list) { - char **skins = VirtFs::enumerateFiles( - branding.getStringValue("systemsounds").c_str()); + VirtList *const skins = VirtFs::enumerateFiles( + branding.getStringValue("systemsounds")); - for (char **i = skins; *i; i++) + FOR_EACH (StringVectCIter, i, skins->names) { if (!VirtFs::isDirectory(( branding.getStringValue("systemsounds") + *i).c_str())) diff --git a/src/resources/wallpaper.cpp b/src/resources/wallpaper.cpp index b98b9eb67..63a8d755c 100644 --- a/src/resources/wallpaper.cpp +++ b/src/resources/wallpaper.cpp @@ -27,6 +27,7 @@ #include "resources/wallpaperdata.h" #include "utils/virtfs.h" +#include "utils/virtlist.h" #include @@ -84,19 +85,19 @@ void Wallpaper::loadWallpapers() { wallpaperData.clear(); initDefaultWallpaperPaths(); - char **const imgs = VirtFs::enumerateFiles(wallpaperPath.c_str()); + VirtList *const imgs = VirtFs::enumerateFiles(wallpaperPath); - for (char *const *i = imgs; *i; i++) + FOR_EACH (StringVectCIter, i, imgs->names) { + // First, get the base filename of the image: + std::string filename = *i; // If the backup file is found, we tell it. - if (strncmp (*i, wallpaperFile.c_str(), strlen(*i)) == 0) + if (filename.find(wallpaperFile) != std::string::npos) haveBackup = true; // If the image format is terminated by: "_x.png" // It is taken as a potential wallpaper. - // First, get the base filename of the image: - std::string filename = *i; size_t separator = filename.rfind('_'); filename = filename.substr(0, separator); @@ -104,12 +105,16 @@ void Wallpaper::loadWallpapers() separator = filename.find('%'); if (separator == std::string::npos) { + std::string name = filename; // Then, append the width and height search mask. filename.append("_%10dx%10d.png"); int width; int height; - if (sscanf(*i, filename.c_str(), &width, &height) == 2) + if (sscanf(name.c_str(), + filename.c_str(), + &width, + &height) == 2) { WallpaperData wp; wp.filename = wallpaperPath; 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