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 _MMAP_COMMON_H
19#define _MMAP_COMMON_H
20
21#include "Define.h"
22#include <memory>
23#include <string>
24#include <vector>
25
26#ifndef _WIN32
27 #include <cstddef>
28 #include <cstring>
29 #include <dirent.h>
30#else
31 #include <Windows.h>
32#endif
33
34#ifndef _WIN32
35 #include <cerrno>
36#endif
37
38namespace MMAP
39{
40 inline bool matchWildcardFilter(char const* filter, char const* str)
41 {
42 if (!filter || !str)
43 return false;
44
45 // end on null character
46 while (*filter && *str)
47 {
48 if (*filter == '*')
49 {
50 if (*++filter == '\0') // wildcard at end of filter means all remaing chars match
51 return true;
52
53 for (;;)
54 {
55 if (*filter == *str)
56 break;
57 if (*str == '\0')
58 return false; // reached end of string without matching next filter character
59 str++;
60 }
61 }
62 else if (*filter != *str)
63 return false; // mismatch
64
65 filter++;
66 str++;
67 }
68
69 return ((*filter == '\0' || (*filter == '*' && *++filter == '\0')) && *str == '\0');
70 }
71
77
78 inline ListFilesResult getDirContents(std::vector<std::string> &fileList, std::string dirpath = ".", std::string filter = "*")
79 {
80 #ifdef WIN32
81 HANDLE hFind;
82 WIN32_FIND_DATAA findFileInfo;
83 std::string directory;
84
85 directory = dirpath + "/" + filter;
86
87 hFind = FindFirstFileA(directory.c_str(), &findFileInfo);
88
89 if (hFind == INVALID_HANDLE_VALUE)
91 do
92 {
93 if ((findFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
94 fileList.push_back(std::string(findFileInfo.cFileName));
95 }
96 while (FindNextFileA(hFind, &findFileInfo));
97
98 FindClose(hFind);
99
100 #else
101 const char *p = dirpath.c_str();
102 DIR * dirp = opendir(p);
103 struct dirent * dp;
104
105 while (dirp)
106 {
107 errno = 0;
108 if ((dp = readdir(dirp)) != nullptr)
109 {
110 if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0 && matchWildcardFilter(filter.c_str(), dp->d_name))
111 fileList.push_back(std::string(dp->d_name));
112 }
113 else
114 break;
115 }
116
117 if (dirp)
118 closedir(dirp);
119 else
121 #endif
122
123 return LISTFILE_OK;
124 }
125}
126
127#endif
ListFilesResult
Definition PathCommon.h:73
@ LISTFILE_DIRECTORY_NOT_FOUND
Definition PathCommon.h:74
@ LISTFILE_OK
Definition PathCommon.h:75
ListFilesResult getDirContents(std::vector< std::string > &fileList, std::string dirpath=".", std::string filter="*")
Definition PathCommon.h:78
bool matchWildcardFilter(char const *filter, char const *str)
Definition PathCommon.h:40