Shell.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "Shell.h"
  29. #include <RmlUi/Core/Core.h>
  30. #ifdef RMLUI_PLATFORM_WIN32
  31. #include <io.h>
  32. #else
  33. #include <dirent.h>
  34. #endif
  35. /// Loads the default fonts from the given path.
  36. void Shell::LoadFonts(const char* directory)
  37. {
  38. Rml::String font_names[5];
  39. font_names[0] = "Delicious-Roman.otf";
  40. font_names[1] = "Delicious-Italic.otf";
  41. font_names[2] = "Delicious-Bold.otf";
  42. font_names[3] = "Delicious-BoldItalic.otf";
  43. font_names[4] = "NotoEmoji-Regular.ttf";
  44. const int fallback_face = 4;
  45. for (size_t i = 0; i < sizeof(font_names) / sizeof(Rml::String); i++)
  46. {
  47. Rml::LoadFontFace(Rml::String(directory) + font_names[i], i == fallback_face);
  48. }
  49. }
  50. enum class ListType { Files, Directories };
  51. static Rml::StringList ListFilesOrDirectories(ListType type, const Rml::String& directory, const Rml::String& extension)
  52. {
  53. if (directory.empty())
  54. return Rml::StringList();
  55. Rml::StringList result;
  56. #ifdef RMLUI_PLATFORM_WIN32
  57. const Rml::String find_path = directory + "/*." + (extension.empty() ? Rml::String("*") : extension);
  58. _finddata_t find_data;
  59. intptr_t find_handle = _findfirst(find_path.c_str(), &find_data);
  60. if (find_handle != -1)
  61. {
  62. do
  63. {
  64. if (strcmp(find_data.name, ".") == 0 ||
  65. strcmp(find_data.name, "..") == 0)
  66. continue;
  67. bool is_directory = ((find_data.attrib & _A_SUBDIR) == _A_SUBDIR);
  68. bool is_file = (!is_directory && ((find_data.attrib & _A_NORMAL) == _A_NORMAL));
  69. if (((type == ListType::Files) && is_file) ||
  70. ((type == ListType::Directories) && is_directory))
  71. {
  72. result.push_back(find_data.name);
  73. }
  74. } while (_findnext(find_handle, &find_data) == 0);
  75. _findclose(find_handle);
  76. }
  77. #else
  78. struct dirent** file_list = nullptr;
  79. const int file_count = scandir(directory.c_str(), &file_list, 0, alphasort);
  80. if (file_count == -1)
  81. return Rml::StringList();
  82. for (int i = 0; i < file_count; i++)
  83. {
  84. if (strcmp(file_list[i]->d_name, ".") == 0 ||
  85. strcmp(file_list[i]->d_name, "..") == 0)
  86. continue;
  87. bool is_directory = ((file_list[i]->d_type & DT_DIR) == DT_DIR);
  88. bool is_file = ((file_list[i]->d_type & DT_REG) == DT_REG);
  89. if (!extension.empty())
  90. {
  91. const char* last_dot = strrchr(file_list[i]->d_name, '.');
  92. if (!last_dot || strcmp(last_dot + 1, extension.c_str()) != 0)
  93. continue;
  94. }
  95. if ((type == ListType::Files && is_file) ||
  96. (type == ListType::Directories && is_directory))
  97. {
  98. result.push_back(file_list[i]->d_name);
  99. }
  100. }
  101. free(file_list);
  102. #endif
  103. return result;
  104. }
  105. Rml::StringList Shell::ListDirectories(const Rml::String& in_directory)
  106. {
  107. return ListFilesOrDirectories(ListType::Directories, in_directory, Rml::String());
  108. }
  109. Rml::StringList Shell::ListFiles(const Rml::String& in_directory, const Rml::String& extension)
  110. {
  111. return ListFilesOrDirectories(ListType::Files, in_directory, extension);
  112. }