/
MMapCommon.h
155 lines (133 loc) · 4.01 KB
/
MMapCommon.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* MaNGOS is a full featured server for World of Warcraft, supporting
* the following clients: 1.12.x, 2.4.3, 3.3.5a, 4.3.4a and 5.4.8
*
* Copyright (C) 2005-2019 MaNGOS project <http://getmangos.eu>
*
* 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
* (at your option) 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* World of Warcraft, and all World of Warcraft or Warcraft art, images,
* and lore are copyrighted by Blizzard Entertainment, Inc.
*/
#ifndef MANGOS_H_MMAP_COMMON
#define MANGOS_H_MMAP_COMMON
// stop warning spam from ACE includes
#ifdef WIN32
# pragma warning( disable : 4996 )
#endif
#include <string>
#include <vector>
#include <errno.h>
#include "Platform/Define.h"
#ifndef WIN32
#include <stddef.h>
#include <dirent.h>
#endif
using namespace std;
namespace MMAP
{
/**
* @brief
*
* @param filter
* @param str
* @return bool
*/
inline bool matchWildcardFilter(const char* filter, const char* str)
{
if (!filter || !str)
{ return false; }
// end on null character
while (*filter && *str)
{
if (*filter == '*')
{
if (*++filter == '\0') // wildcard at end of filter means all remaing chars match
{ return true; }
while (true)
{
if (*filter == *str)
{ break; }
if (*str == '\0')
{ return false; } // reached end of string without matching next filter character
str++;
}
}
else if (*filter != *str)
{ return false; } // mismatch
filter++;
str++;
}
return ((*filter == '\0' || (*filter == '*' && *++filter == '\0')) && *str == '\0');
}
/**
* @brief
*
*/
enum ListFilesResult
{
LISTFILE_DIRECTORY_NOT_FOUND = 0,
LISTFILE_OK = 1
};
/**
* @brief
*
* @param fileList
* @param dirpath
* @param filter
* @param includeSubDirs
* @return ListFilesResult
*/
inline ListFilesResult getDirContents(vector<string>& fileList, string dirpath = ".", string filter = "*", bool includeSubDirs = false)
{
#ifdef WIN32
HANDLE hFind;
WIN32_FIND_DATA findFileInfo;
string directory;
directory = dirpath + "/" + filter;
hFind = FindFirstFile(directory.c_str(), &findFileInfo);
if (hFind == INVALID_HANDLE_VALUE)
{ return LISTFILE_DIRECTORY_NOT_FOUND; }
do
{
if (includeSubDirs || (findFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
{ fileList.push_back(string(findFileInfo.cFileName)); }
}
while (FindNextFile(hFind, &findFileInfo));
FindClose(hFind);
#else
const char* p = dirpath.c_str();
DIR* dirp = opendir(p);
struct dirent* dp;
while (dirp)
{
errno = 0;
if ((dp = readdir(dirp)) != NULL)
{
if (matchWildcardFilter(filter.c_str(), dp->d_name))
{ fileList.push_back(string(dp->d_name)); }
}
else
{ break; }
}
if (dirp)
{ closedir(dirp); }
else
{ return LISTFILE_DIRECTORY_NOT_FOUND; }
#endif
return LISTFILE_OK;
}
}
#endif