TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
PathCommon.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef _MMAP_COMMON_H
20 #define _MMAP_COMMON_H
21 
22 #include <string>
23 #include <vector>
24 
25 #include "Common.h"
26 
27 #ifndef _WIN32
28  #include <stddef.h>
29  #include <dirent.h>
30 #endif
31 
32 #ifdef __linux__
33  #include <errno.h>
34 #endif
35 
37 {
38  NAV_EMPTY = 0x00,
39  NAV_GROUND = 0x01,
40  NAV_MAGMA = 0x02,
41  NAV_SLIME = 0x04,
42  NAV_WATER = 0x08,
43  NAV_UNUSED1 = 0x10,
44  NAV_UNUSED2 = 0x20,
45  NAV_UNUSED3 = 0x40,
46  NAV_UNUSED4 = 0x80
47  // we only have 8 bits
48 };
49 
50 namespace MMAP
51 {
52  inline bool matchWildcardFilter(const char* filter, const char* str)
53  {
54  if (!filter || !str)
55  return false;
56 
57  // end on null character
58  while (*filter && *str)
59  {
60  if (*filter == '*')
61  {
62  if (*++filter == '\0') // wildcard at end of filter means all remaing chars match
63  return true;
64 
65  for (;;)
66  {
67  if (*filter == *str)
68  break;
69  if (*str == '\0')
70  return false; // reached end of string without matching next filter character
71  str++;
72  }
73  }
74  else if (*filter != *str)
75  return false; // mismatch
76 
77  filter++;
78  str++;
79  }
80 
81  return ((*filter == '\0' || (*filter == '*' && *++filter == '\0')) && *str == '\0');
82  }
83 
85  {
88  };
89 
90  inline ListFilesResult getDirContents(std::vector<std::string> &fileList, std::string dirpath = ".", std::string filter = "*")
91  {
92  #ifdef WIN32
93  HANDLE hFind;
94  WIN32_FIND_DATA findFileInfo;
95  std::string directory;
96 
97  directory = dirpath + "/" + filter;
98 
99  hFind = FindFirstFile(directory.c_str(), &findFileInfo);
100 
101  if (hFind == INVALID_HANDLE_VALUE)
103  do
104  {
105  if ((findFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
106  fileList.push_back(std::string(findFileInfo.cFileName));
107  }
108  while (FindNextFile(hFind, &findFileInfo));
109 
110  FindClose(hFind);
111 
112  #else
113  const char *p = dirpath.c_str();
114  DIR * dirp = opendir(p);
115  struct dirent * dp;
116 
117  while (dirp)
118  {
119  errno = 0;
120  if ((dp = readdir(dirp)) != NULL)
121  {
122  if (matchWildcardFilter(filter.c_str(), dp->d_name))
123  fileList.push_back(std::string(dp->d_name));
124  }
125  else
126  break;
127  }
128 
129  if (dirp)
130  closedir(dirp);
131  else
133  #endif
134 
135  return LISTFILE_OK;
136  }
137 }
138 
139 #endif
Definition: PathCommon.h:41
void * HANDLE
Definition: CascPort.h:146
Definition: PathCommon.h:39
Definition: PathCommon.h:40
Definition: PathCommon.h:45
arena_t NULL
Definition: jemalloc_internal.h:624
ListFilesResult
Definition: PathCommon.h:84
Definition: MMapFactory.cpp:22
Definition: PathCommon.h:43
NavTerrain
Definition: MapDefines.h:22
Definition: PathCommon.h:87
Definition: PathCommon.h:86
Definition: PathCommon.h:46
Definition: PathCommon.h:42
ListFilesResult getDirContents(std::vector< std::string > &fileList, std::string dirpath=".", std::string filter="*")
Definition: PathCommon.h:90
Definition: PathCommon.h:44
#define INVALID_HANDLE_VALUE
Definition: CascPort.h:169
Definition: PathCommon.h:38
bool matchWildcardFilter(const char *filter, const char *str)
Definition: PathCommon.h:52