TrinityCore
Loading...
Searching...
No Matches
PathCommon.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_MMAP_COMMON_H
19#define TRINITYCORE_MMAP_COMMON_H
20
21#include "Common.h"
22#include <boost/filesystem/directory.hpp>
23#include <string_view>
24#include <unordered_map>
25
26namespace MMAP
27{
28 inline bool matchWildcardFilter(std::string_view filter, std::string_view str)
29 {
30 if (filter.empty() || str.empty())
31 return false;
32
33 auto filterItr = filter.begin();
34 auto filterEnd = filter.end();
35 auto strItr = str.begin();
36 auto strEnd = str.end();
37
38 // end on null character
39 while (filterItr != filterEnd && strItr != strEnd)
40 {
41 if (*filterItr == '*')
42 {
43 if (++filterItr == filterEnd) // wildcard at end of filter means all remaing chars match
44 return true;
45
46 while (*filterItr != *strItr)
47 {
48 if (strItr == strEnd)
49 return false; // reached end of string without matching next filter character
50 ++strItr;
51 }
52 }
53 else if (*filterItr != *strItr)
54 return false; // mismatch
55
56 ++filterItr;
57 ++strItr;
58 }
59
60 return (filterItr == filterEnd || (*filterItr == '*' && filterItr + 1 == filterEnd)) && strItr == strEnd;
61 }
62
68
69 inline ListFilesResult getDirContents(std::vector<std::string>& fileList, boost::filesystem::path const& dirpath,
70 boost::filesystem::file_type type = boost::filesystem::regular_file, std::string_view filter = "*"sv)
71 {
72 boost::system::error_code ec;
73 boost::filesystem::directory_iterator dirItr(dirpath, ec);
74 if (ec)
76
77 for (boost::filesystem::directory_entry const& dirEntry : dirItr)
78 {
79 if (dirEntry.status(ec).type() != type || ec)
80 continue;
81
82 std::string fileName = dirEntry.path().filename().string();
83 if (!matchWildcardFilter(filter, fileName))
84 continue;
85
86 fileList.push_back(std::move(fileName));
87 }
88
89 return LISTFILE_OK;
90 }
91
99
100 extern std::unordered_map<uint32, MapEntry> sMapStore;
101}
102
103#endif
uint8_t uint8
Definition Define.h:156
int16_t int16
Definition Define.h:151
int8_t int8
Definition Define.h:152
int32_t int32
Definition Define.h:150
ListFilesResult
Definition PathCommon.h:64
@ LISTFILE_DIRECTORY_NOT_FOUND
Definition PathCommon.h:65
@ LISTFILE_OK
Definition PathCommon.h:66
ListFilesResult getDirContents(std::vector< std::string > &fileList, boost::filesystem::path const &dirpath, boost::filesystem::file_type type=boost::filesystem::regular_file, std::string_view filter="*"sv)
Definition PathCommon.h:69
bool matchWildcardFilter(std::string_view filter, std::string_view str)
Definition PathCommon.h:28
std::unordered_map< uint32, MapEntry > sMapStore
int16 ParentMapID
Definition PathCommon.h:96