-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMMapCommon.h
112 lines (93 loc) · 2.71 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
#ifndef _MMAP_COMMON_H
#define _MMAP_COMMON_H
// stop warning spam from ACE includes
#pragma warning(disable : 4996)
#define MMAP_GENERATOR
#include <string>
#include <vector>
#include <dirent.h>
#ifdef WIN32
#include <windows.h>
#endif
#include "DebugAlloc.h"
typedef unsigned int uint32;
typedef unsigned short int uint16;
typedef unsigned char uint8;
using namespace std;
namespace MMAP
{
#ifndef WIN32
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');
}
#endif
enum ListFilesResult
{
LISTFILE_DIRECTORY_NOT_FOUND = -1,
LISTFILE_OK = 1
};
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;
struct dirent * dp;
dirp = opendir(p);
while (dirp)
{
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