PageRenderTime 19ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/src/tools/mmaps_generator/PathCommon.h

https://gitlab.com/tkrokli/TrinityCore_434
C Header | 139 lines | 98 code | 22 blank | 19 comment | 33 complexity | 9834b0f537a3d5818cfd9518d937aef8 MD5 | raw file
  1/*
  2 * Copyright (C) 2008-2015 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
 36enum NavTerrain
 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
 50namespace 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
 84    enum ListFilesResult
 85    {
 86        LISTFILE_DIRECTORY_NOT_FOUND = 0,
 87        LISTFILE_OK = 1
 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)
102            return LISTFILE_DIRECTORY_NOT_FOUND;
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
132            return LISTFILE_DIRECTORY_NOT_FOUND;
133    #endif
134
135        return LISTFILE_OK;
136    }
137}
138
139#endif