Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
QtDirectory.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlQt6/QtDirectory.hpp>
33 #include <vlQt6/QtFile.hpp>
34 #include <algorithm>
35 #include <QDir>
36 
37 using namespace vl;
38 
39 //-----------------------------------------------------------------------------
40 // QtDirectory
41 //-----------------------------------------------------------------------------
43 {
44  setPath(name);
45 }
46 //-----------------------------------------------------------------------------
48 {
49 }
50 //-----------------------------------------------------------------------------
51 void QtDirectory::listFilesRecursive(std::vector<String> &file_list) const
52 {
53  file_list.clear();
54  listFilesRecursive_internal(file_list);
55 }
56 //-----------------------------------------------------------------------------
57 ref<QtDirectory> QtDirectory::qtSubDir(const String &subdir_name) const
58 {
59  if (path().empty())
60  {
61  Log::error("QtDirectory::path() must not be empty!\n");
62  return NULL;
63  }
64 
65  std::vector<String> dir_list;
66  String p = translatePath(subdir_name).right(-path().length() - 1);
67  this->listSubDirs(dir_list);
68  String cur_p = path();
69  for (int i = 0; i < (int)dir_list.size(); ++i)
70  {
71  dir_list[i] = dir_list[i].right(-cur_p.length() - 1);
72  if (p.startsWith(dir_list[i] + '/') || p == dir_list[i])
73  {
74  ref<QtDirectory> dir = new QtDirectory(cur_p + '/' + dir_list[i]);
75  if (!dir)
76  return NULL;
77  cur_p = cur_p + '/' + dir_list[i];
78  p = p.right(-dir_list[i].length() - 1);
79  dir->listSubDirs(dir_list);
80  i = -1;
81  if (p.empty())
82  return dir;
83  }
84  }
85  return NULL;
86 }
87 //-----------------------------------------------------------------------------
88 bool QtDirectory::exists() const
89 {
90  if (path().empty())
91  {
92  Log::error("QtDirectory::path() must not be empty!\n");
93  return false;
94  }
95  return QDir(QString(path().toStdString().c_str())).exists();
96 }
97 //-----------------------------------------------------------------------------
98 void QtDirectory::listSubDirs(std::vector<String> &dirs_out, bool append) const
99 {
100  if (!append)
101  {
102  dirs_out.clear();
103  }
104 
105  if (path().empty())
106  {
107  Log::error("QtDirectory::path() must not be empty!\n");
108  return;
109  }
110 
111  const char *p = path().toStdString().c_str();
112  QStringList subdirs = QDir(QString(p)).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
113 
114  for (int i = 0; i < subdirs.size(); ++i)
115  {
116  vl::String name = vl::String::fromStdString(subdirs.at(i).toStdString());
117  dirs_out.push_back(path() + name + '/');
118  }
119 }
120 //-----------------------------------------------------------------------------
121 void QtDirectory::listFiles(std::vector<ref<QtFile>> &file_list, bool append) const
122 {
123  if (!append)
124  file_list.clear();
125  std::vector<String> file_names;
126  listFiles(file_names, false);
127  for (unsigned i = 0; i < file_names.size(); ++i)
128  {
129  ref<QtFile> file = new QtFile;
130  // file->setPath( path() + file_names[i] );
131  file->setPath(file_names[i]);
132  file_list.push_back(file);
133  }
134 }
135 //-----------------------------------------------------------------------------
136 void QtDirectory::listFiles(std::vector<String> &files_out, bool append) const
137 {
138  if (!append)
139  {
140  files_out.clear();
141  }
142 
143  if (path().empty())
144  {
145  Log::error("QtDirectory::path() must not be empty!\n");
146  return;
147  }
148 
149  const char *p = path().toStdString().c_str();
150  QStringList files = QDir(QString(p)).entryList(QDir::Files);
151 
152  for (int i = 0; i < files.size(); ++i)
153  {
154  vl::String name = vl::String::fromStdString(files.at(i).toStdString());
155  files_out.push_back(path() + name + '/');
156  }
157 }
158 //-----------------------------------------------------------------------------
159 ref<VirtualFile> QtDirectory::file(const String &name) const
160 {
161  return qtFile(name);
162 }
163 //-----------------------------------------------------------------------------
164 ref<QtFile> QtDirectory::qtFile(const String &name) const
165 {
166  String p = translatePath(name);
167  ref<QtFile> file = new QtFile(p);
168  if (file->exists())
169  return file;
170  else
171  return NULL;
172 }
173 //-----------------------------------------------------------------------------
174 void QtDirectory::listFilesRecursive_internal(std::vector<String> &file_list) const
175 {
176  // add local child
177  listFiles(file_list, true);
178  // descend recursively
179  std::vector<String> dir_list;
180  listSubDirs(dir_list);
181  for (unsigned i = 0; i < dir_list.size(); ++i)
182  {
183  VL_CHECK(dir_list[i] != ".")
184  VL_CHECK(dir_list[i] != "..")
185 
186  QtDirectory sub_dir(dir_list[i]);
187  sub_dir.listFilesRecursive_internal(file_list);
188  }
189 }
190 //-----------------------------------------------------------------------------
virtual bool setPath(const String &path)
Changes the path name of a VirtualDirectory. Must not be an empty string.
void listFilesRecursive(std::vector< String > &file_list) const
Use carefully this function, since this search the whole given file system tree.
Definition: QtDirectory.cpp:51
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
virtual bool exists() const
Returns true if the file exists.
Definition: QtFile.cpp:96
ref< QtDirectory > qtSubDir(const String &subdir_name) const
Definition: QtDirectory.cpp:57
Visualization Library main namespace.
static String fromStdString(const std::string &str, bool utf8=true)
Initializes the string from a std::string using fromUTF() if utf8 == true (default) otherwise uses fr...
Definition: String.cpp:881
virtual const String & path() const
int length() const
Returns the length of the string.
Definition: String.hpp:133
void listFilesRecursive_internal(std::vector< String > &file_list) const
virtual ref< QtFile > qtFile(const String &name) const
A VirtualDirectory that uses Qt&#39;s QDir.
Definition: QtDirectory.hpp:58
void listSubDirs(std::vector< String > &dirs, bool append=false) const
Definition: QtDirectory.cpp:98
bool empty() const
Returns true if length() == 0.
Definition: String.hpp:136
bool startsWith(const String &str) const
Returns true if a String starts with the specified String str.
Definition: String.cpp:720
A VirtualFile that uses Qt&#39;s QFile.
Definition: QtFile.hpp:61
void listFiles(std::vector< String > &file_list, bool append=false) const
#define NULL
Definition: OpenGLDefs.hpp:81
bool exists() const
Definition: QtDirectory.cpp:88
void setPath(const String &name)
Changes the path bound to a VirtualFile. Use carefully this function, you shouldn&#39;t rename a VirtualF...
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
T length(T v)
Definition: glsl_math.hpp:1084
String right(int count) const
Returns the count rightmost caracters of a String. If count is negative the function returns all but ...
Definition: String.cpp:822
std::string toStdString() const
Returns a UTF8 encoded std::string.
Definition: String.cpp:1156
virtual ref< VirtualFile > file(const String &name) const
Returns the VirtualFile with the given name if any, NULL otherwise.
#define VL_CHECK(expr)
Definition: checks.hpp:73
String translatePath(const String &p) const