Tileson  1.3.0
A helpful json parser for Tiled maps
ProjectFolder.hpp
Go to the documentation of this file.
1 //
2 // Created by robin on 01.08.2020.
3 //
4 
5 #ifndef TILESON_PROJECTFOLDER_HPP
6 #define TILESON_PROJECTFOLDER_HPP
7 
8 namespace tson
9 {
11  {
12  public:
13  inline ProjectFolder(const fs::path &path);
14 
15  inline const fs::path &getPath() const;
16  inline bool hasWorldFile() const;
17  inline const std::vector<ProjectFolder> &getSubFolders() const;
18  inline const std::vector<fs::path> &getFiles() const;
19  inline const World &getWorld() const;
20 
21  private:
22  inline void loadData();
23  fs::path m_path;
24  bool m_hasWorldFile;
25  tson::World m_world;
26  std::vector<ProjectFolder> m_subFolders;
27  std::vector<fs::path> m_files;
28 
29  };
30 
31  ProjectFolder::ProjectFolder(const fs::path &path) : m_path {path}
32  {
33  loadData();
34  }
35 
36  void ProjectFolder::loadData()
37  {
38  m_hasWorldFile = false;
39  m_subFolders.clear();
40  m_files.clear();
41  //Search and see if there is a World file .world file
42  fs::path worldPath;
43  for (const auto & entry : fs::directory_iterator(m_path))
44  {
45  if(fs::is_regular_file(entry.path()))
46  {
47  if(entry.path().extension() == ".world")
48  {
49  m_hasWorldFile = true;
50  worldPath = entry.path();
51  }
52  }
53  }
54 
55  if(m_hasWorldFile)
56  m_world.parse(worldPath);
57 
58  for (const auto & entry : fs::directory_iterator(m_path))
59  {
60  if (fs::is_directory(entry.path()))
61  m_subFolders.emplace_back(entry.path());//.loadData(); - loadData() is called in the constructor, so don't call again.
62  else if (fs::is_regular_file(entry.path()))
63  {
64  if(m_hasWorldFile && m_world.contains(entry.path().filename().u8string()))
65  m_files.emplace_back(entry.path());
66  else if(!m_hasWorldFile)
67  m_files.emplace_back(entry.path());
68  }
69  }
70 
71  }
72 
73  const fs::path &ProjectFolder::getPath() const
74  {
75  return m_path;
76  }
77 
79  {
80  return m_hasWorldFile;
81  }
82 
83  const std::vector<ProjectFolder> &ProjectFolder::getSubFolders() const
84  {
85  return m_subFolders;
86  }
87 
88  const std::vector<fs::path> &ProjectFolder::getFiles() const
89  {
90  return m_files;
91  }
92 
98  {
99  return m_world;
100  }
101 }
102 
103 #endif //TILESON_PROJECTFOLDER_HPP
Definition: ProjectFolder.hpp:11
const fs::path & getPath() const
Definition: ProjectFolder.hpp:73
const World & getWorld() const
Definition: ProjectFolder.hpp:97
const std::vector< ProjectFolder > & getSubFolders() const
Definition: ProjectFolder.hpp:83
ProjectFolder(const fs::path &path)
Definition: ProjectFolder.hpp:31
bool hasWorldFile() const
Definition: ProjectFolder.hpp:78
const std::vector< fs::path > & getFiles() const
Definition: ProjectFolder.hpp:88
Definition: World.hpp:14
bool parse(const fs::path &path)
Definition: World.hpp:58
bool contains(std::string_view filename)
Definition: World.hpp:113
Definition: Base64.hpp:12