FileSystem.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "FileSystem.h"
  29. #include <RmlUi/Core/StringUtilities.h>
  30. #include <cstdlib>
  31. #include <cstdio>
  32. #include <string.h>
  33. #ifdef RMLUI_PLATFORM_WIN32
  34. #include <io.h>
  35. #else
  36. #include <dirent.h>
  37. #endif
  38. struct FileSystemNode;
  39. typedef Rml::UnorderedMap< Rml::String, FileSystemNode* > NodeMap;
  40. FileSystemNode* file_system_root = nullptr;
  41. NodeMap node_map;
  42. /**
  43. Stores a single node (file or directory) in the file system. This is only used to generate
  44. interesting data for the data source.
  45. */
  46. struct FileSystemNode
  47. {
  48. FileSystemNode(const Rml::String _name, bool _directory, int _depth = -1) : name(_name)
  49. {
  50. id = Rml::CreateString(16, "%x", this);
  51. directory = _directory;
  52. depth = _depth;
  53. node_map[id] = this;
  54. }
  55. ~FileSystemNode()
  56. {
  57. for (size_t i = 0; i < child_nodes.size(); ++i)
  58. delete child_nodes[i];
  59. }
  60. // Build the list of files and directories within this directory.
  61. void BuildTree(const Rml::String& root = "")
  62. {
  63. #ifdef RMLUI_PLATFORM_WIN32
  64. _finddata_t find_data;
  65. intptr_t find_handle = _findfirst((root + name + "/*.*").c_str(), &find_data);
  66. if (find_handle != -1)
  67. {
  68. do
  69. {
  70. if (strcmp(find_data.name, ".") == 0 ||
  71. strcmp(find_data.name, "..") == 0)
  72. continue;
  73. child_nodes.push_back(new FileSystemNode(find_data.name, (find_data.attrib & _A_SUBDIR) == _A_SUBDIR, depth + 1));
  74. } while (_findnext(find_handle, &find_data) == 0);
  75. _findclose(find_handle);
  76. }
  77. #else
  78. struct dirent** file_list = nullptr;
  79. int file_count = -1;
  80. file_count = scandir((root + name).c_str(), &file_list, 0, alphasort);
  81. if (file_count == -1)
  82. return;
  83. while (file_count--)
  84. {
  85. if (strcmp(file_list[file_count]->d_name, ".") == 0 ||
  86. strcmp(file_list[file_count]->d_name, "..") == 0)
  87. continue;
  88. child_nodes.push_back(new FileSystemNode(file_list[file_count]->d_name, (file_list[file_count]->d_type & DT_DIR) == DT_DIR, depth + 1));
  89. free(file_list[file_count]);
  90. }
  91. free(file_list);
  92. #endif
  93. // Generate the trees of all of our subdirectories.
  94. for (size_t i = 0; i < child_nodes.size(); ++i)
  95. {
  96. if (child_nodes[i]->directory)
  97. child_nodes[i]->BuildTree(root + name + "/");
  98. }
  99. }
  100. typedef Rml::Vector< FileSystemNode* > NodeList;
  101. Rml::String id;
  102. Rml::String name;
  103. bool directory;
  104. int depth;
  105. NodeList child_nodes;
  106. };
  107. FileSystem::FileSystem(const Rml::String& root) : Rml::DataSource("file")
  108. {
  109. // Generate the file system nodes starting at the RmlUi's root directory.
  110. file_system_root = new FileSystemNode(".", true);
  111. file_system_root->BuildTree(root);
  112. }
  113. FileSystem::~FileSystem()
  114. {
  115. delete file_system_root;
  116. file_system_root = nullptr;
  117. }
  118. void FileSystem::GetRow(Rml::StringList& row, const Rml::String& table, int row_index, const Rml::StringList& columns)
  119. {
  120. // Get the node that data is being queried from; one of its children (as indexed by row_index)
  121. // is the actual node the data will be read from.
  122. FileSystemNode* node = GetNode(table);
  123. if (node == nullptr)
  124. return;
  125. for (size_t i = 0; i < columns.size(); i++)
  126. {
  127. if (columns[i] == "name")
  128. {
  129. // Returns the node's name.
  130. row.push_back(node->child_nodes[row_index]->name);
  131. }
  132. else if (columns[i] == "depth")
  133. {
  134. // Returns the depth of the node (ie, how far down the directory structure it is).
  135. row.push_back(Rml::CreateString(8, "%d", node->child_nodes[row_index]->depth));
  136. }
  137. else if (columns[i] == Rml::DataSource::CHILD_SOURCE)
  138. {
  139. // Returns the name of the data source that this node's children can be queried from.
  140. row.push_back("file." + node->child_nodes[row_index]->id);
  141. }
  142. }
  143. }
  144. int FileSystem::GetNumRows(const Rml::String& table)
  145. {
  146. FileSystemNode* node = GetNode(table);
  147. if (node != nullptr)
  148. return (int) node->child_nodes.size();
  149. return 0;
  150. }
  151. FileSystemNode* FileSystem::GetNode(const Rml::String& table)
  152. {
  153. // Determine which node the row is being requested from.
  154. if (table == "root")
  155. return file_system_root;
  156. else
  157. {
  158. NodeMap::iterator i = node_map.find(table);
  159. if (i != node_map.end())
  160. return i->second;
  161. }
  162. return nullptr;
  163. }