Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
PathCommon.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef _MMAP_COMMON_H
7#define _MMAP_COMMON_H
8
9#include <chrono>
10#include <string>
11#include <vector>
12
13#include "Define.h"
14
15#ifdef _WIN32
16 #ifndef WIN32_LEAN_AND_MEAN
17 #define WIN32_LEAN_AND_MEAN
18 #endif
19 #include <windows.h>
20#else
21 #include <stddef.h>
22 #include <dirent.h>
23#endif
24
25#ifdef __linux__
26 #include <errno.h>
27#endif
28
30{
31 NAV_EMPTY = 0x00,
32 NAV_GROUND = 0x01,
33 NAV_MAGMA = 0x02,
34 NAV_SLIME = 0x04,
35 NAV_WATER = 0x08,
40 // we only have 8 bits
41};
42
43namespace MMAP
44{
45 inline bool matchWildcardFilter(const char* filter, const char* str)
46 {
47 if (!filter || !str)
48 return false;
49
50 // end on null character
51 while (*filter && *str)
52 {
53 if (*filter == '*')
54 {
55 if (*++filter == '\0') // wildcard at end of filter means all remaing chars match
56 return true;
57
58 while (true)
59 {
60 if (*filter == *str)
61 break;
62 if (*str == '\0')
63 return false; // reached end of string without matching next filter character
64 str++;
65 }
66 }
67 else if (*filter != *str)
68 return false; // mismatch
69
70 filter++;
71 str++;
72 }
73
74 return ((*filter == '\0' || (*filter == '*' && *++filter == '\0')) && *str == '\0');
75 }
76
82
83 inline ListFilesResult getDirContents(std::vector<std::string> &fileList, std::string dirpath = ".", std::string filter = "*")
84 {
85 #ifdef WIN32
86 HANDLE hFind;
87 WIN32_FIND_DATA findFileInfo;
88 std::string directory;
89
90 directory = dirpath + "/" + filter;
91
92 hFind = FindFirstFile(directory.c_str(), &findFileInfo);
93
94 if (hFind == INVALID_HANDLE_VALUE)
96 do
97 {
98 if ((findFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
99 fileList.push_back(std::string(findFileInfo.cFileName));
100 }
101 while (FindNextFile(hFind, &findFileInfo));
102
103 FindClose(hFind);
104
105 #else
106 const char *p = dirpath.c_str();
107 DIR * dirp = opendir(p);
108 struct dirent * dp;
109 dirp = opendir(p);
110
111 while (dirp)
112 {
113 errno = 0;
114 if ((dp = readdir(dirp)) != NULL)
115 {
116 if (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
133 {
134 using Clock = std::chrono::steady_clock;
135 static const Clock::time_point ApplicationStartTime = Clock::now();
136 return uint32(std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - ApplicationStartTime).count());
137 }
138
139 inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
140 {
141 // getMSTime() have limited data range and this is case when it overflow in this tick
142 if (oldMSTime > newMSTime)
143 return (0xFFFFFFFF - oldMSTime) + newMSTime;
144 else
145 return newMSTime - oldMSTime;
146 }
147
149 {
150 return getMSTimeDiff(oldMSTime, getMSTime());
151 }
152}
153
154#endif
std::uint32_t uint32
Definition Define.h:77
NavTerrain
@ NAV_UNUSED3
@ NAV_UNUSED2
@ NAV_UNUSED1
@ NAV_EMPTY
@ NAV_UNUSED4
@ NAV_MAGMA
@ NAV_GROUND
@ NAV_SLIME
@ NAV_WATER
ListFilesResult
Definition PathCommon.h:78
@ LISTFILE_DIRECTORY_NOT_FOUND
Definition PathCommon.h:79
@ LISTFILE_OK
Definition PathCommon.h:80
bool matchWildcardFilter(const char *filter, const char *str)
Definition PathCommon.h:45
ListFilesResult getDirContents(std::vector< std::string > &fileList, std::string dirpath=".", std::string filter="*")
Definition PathCommon.h:83
uint32 getMSTime()
Definition PathCommon.h:132
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition PathCommon.h:148
uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
Definition PathCommon.h:139