TrinityCore
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 _MMAP_COMMON_H
19#define _MMAP_COMMON_H
20
21#include "Define.h"
22#include <memory>
23#include <string>
24#include <unordered_map>
25#include <vector>
26
27#ifndef _WIN32
28 #include <cstddef>
29 #include <cstring>
30 #include <dirent.h>
31#else
32 #include <Windows.h>
33#endif
34
35#ifndef _WIN32
36 #include <cerrno>
37#endif
38
39namespace VMAP
40{
41 class VMapManager2;
42}
43
44namespace MMAP
45{
46 inline bool matchWildcardFilter(char const* filter, char const* str)
47 {
48 if (!filter || !str)
49 return false;
50
51 // end on null character
52 while (*filter && *str)
53 {
54 if (*filter == '*')
55 {
56 if (*++filter == '\0') // wildcard at end of filter means all remaing chars match
57 return true;
58
59 for (;;)
60 {
61 if (*filter == *str)
62 break;
63 if (*str == '\0')
64 return false; // reached end of string without matching next filter character
65 str++;
66 }
67 }
68 else if (*filter != *str)
69 return false; // mismatch
70
71 filter++;
72 str++;
73 }
74
75 return ((*filter == '\0' || (*filter == '*' && *++filter == '\0')) && *str == '\0');
76 }
77
79 {
81 LISTFILE_OK = 1
82 };
83
84 inline ListFilesResult getDirContents(std::vector<std::string> &fileList, std::string dirpath = ".", std::string filter = "*")
85 {
86 #ifdef WIN32
87 HANDLE hFind;
88 WIN32_FIND_DATA findFileInfo;
89 std::string directory;
90
91 directory = dirpath + "/" + filter;
92
93 hFind = FindFirstFile(directory.c_str(), &findFileInfo);
94
95 if (hFind == INVALID_HANDLE_VALUE)
97 do
98 {
99 if (strcmp(findFileInfo.cFileName, ".") != 0 && strcmp(findFileInfo.cFileName, "..") != 0)
100 fileList.push_back(std::string(findFileInfo.cFileName));
101 }
102 while (FindNextFile(hFind, &findFileInfo));
103
104 FindClose(hFind);
105
106 #else
107 const char *p = dirpath.c_str();
108 DIR * dirp = opendir(p);
109 struct dirent * dp;
110
111 while (dirp)
112 {
113 errno = 0;
114 if ((dp = readdir(dirp)) != nullptr)
115 {
116 if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0 && matchWildcardFilter(filter.c_str(), dp->d_name))
117 fileList.push_back(std::string(dp->d_name));
118 }
119 else
120 break;
121 }
122
123 if (dirp)
124 closedir(dirp);
125 else
127 #endif
128
129 return LISTFILE_OK;
130 }
131
132 struct MapEntry
133 {
138 };
139
140 extern std::unordered_map<uint32, MapEntry> sMapStore;
141
142 namespace VMapFactory
143 {
144 std::unique_ptr<VMAP::VMapManager2> CreateVMapManager();
145}
146}
147
148#endif
uint8_t uint8
Definition: Define.h:144
int16_t int16
Definition: Define.h:139
int8_t int8
Definition: Define.h:140
int32_t int32
Definition: Define.h:138
std::unique_ptr< VMAP::VMapManager2 > CreateVMapManager()
ListFilesResult
Definition: PathCommon.h:79
@ LISTFILE_DIRECTORY_NOT_FOUND
Definition: PathCommon.h:80
@ LISTFILE_OK
Definition: PathCommon.h:81
ListFilesResult getDirContents(std::vector< std::string > &fileList, std::string dirpath=".", std::string filter="*")
Definition: PathCommon.h:84
bool matchWildcardFilter(char const *filter, char const *str)
Definition: PathCommon.h:46
std::unordered_map< uint32, MapEntry > sMapStore
int16 ParentMapID
Definition: PathCommon.h:136