/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. #ifndef _MMAP_COMMON_H
  19. #define _MMAP_COMMON_H
  20. #include <string>
  21. #include <vector>
  22. #include "Common.h"
  23. #ifndef _WIN32
  24. #include <stddef.h>
  25. #include <dirent.h>
  26. #endif
  27. #ifdef __linux__
  28. #include <errno.h>
  29. #endif
  30. enum NavTerrain
  31. {
  32. NAV_EMPTY = 0x00,
  33. NAV_GROUND = 0x01,
  34. NAV_MAGMA = 0x02,
  35. NAV_SLIME = 0x04,
  36. NAV_WATER = 0x08,
  37. NAV_UNUSED1 = 0x10,
  38. NAV_UNUSED2 = 0x20,
  39. NAV_UNUSED3 = 0x40,
  40. NAV_UNUSED4 = 0x80
  41. // we only have 8 bits
  42. };
  43. namespace MMAP
  44. {
  45. inline bool matchWildcardFilter(const char* filter, const char* str)
  46. {
  47. if (!filter || !str)
  48. return false;
  49. // end on null character
  50. while (*filter && *str)
  51. {
  52. if (*filter == '*')
  53. {
  54. if (*++filter == '\0') // wildcard at end of filter means all remaing chars match
  55. return true;
  56. for (;;)
  57. {
  58. if (*filter == *str)
  59. break;
  60. if (*str == '\0')
  61. return false; // reached end of string without matching next filter character
  62. str++;
  63. }
  64. }
  65. else if (*filter != *str)
  66. return false; // mismatch
  67. filter++;
  68. str++;
  69. }
  70. return ((*filter == '\0' || (*filter == '*' && *++filter == '\0')) && *str == '\0');
  71. }
  72. enum ListFilesResult
  73. {
  74. LISTFILE_DIRECTORY_NOT_FOUND = 0,
  75. LISTFILE_OK = 1
  76. };
  77. inline ListFilesResult getDirContents(std::vector<std::string> &fileList, std::string dirpath = ".", std::string filter = "*")
  78. {
  79. #ifdef WIN32
  80. HANDLE hFind;
  81. WIN32_FIND_DATA findFileInfo;
  82. std::string directory;
  83. directory = dirpath + "/" + filter;
  84. hFind = FindFirstFile(directory.c_str(), &findFileInfo);
  85. if (hFind == INVALID_HANDLE_VALUE)
  86. return LISTFILE_DIRECTORY_NOT_FOUND;
  87. do
  88. {
  89. if ((findFileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
  90. fileList.push_back(std::string(findFileInfo.cFileName));
  91. }
  92. while (FindNextFile(hFind, &findFileInfo));
  93. FindClose(hFind);
  94. #else
  95. const char *p = dirpath.c_str();
  96. DIR * dirp = opendir(p);
  97. struct dirent * dp;
  98. while (dirp)
  99. {
  100. errno = 0;
  101. if ((dp = readdir(dirp)) != NULL)
  102. {
  103. if (matchWildcardFilter(filter.c_str(), dp->d_name))
  104. fileList.push_back(std::string(dp->d_name));
  105. }
  106. else
  107. break;
  108. }
  109. if (dirp)
  110. closedir(dirp);
  111. else
  112. return LISTFILE_DIRECTORY_NOT_FOUND;
  113. #endif
  114. return LISTFILE_OK;
  115. }
  116. }
  117. #endif