Visualization Library 2.0.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 <vlQt5/QtDirectory.hpp>
33 #include <vlQt5/QtFile.hpp>
34 #include <algorithm>
35 #include <QDir>
36 
37 using namespace vl;
38 
39 //-----------------------------------------------------------------------------
40 // QtDirectory
41 //-----------------------------------------------------------------------------
42 QtDirectory::QtDirectory( const String& name )
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  dirs_out.clear();
102  }
103 
104  if ( path().empty() )
105  {
106  Log::error( "QtDirectory::path() must not be empty!\n" );
107  return;
108  }
109 
110  const char* p = path().toStdString().c_str();
111  QStringList subdirs = QDir(QString(p)).entryList( QDir::Dirs | QDir::NoDotAndDotDot );
112 
113  for (int i = 0; i < subdirs.size(); ++i) {
114  vl::String name = vl::String::fromStdString( subdirs.at(i).toStdString() );
115  dirs_out.push_back( path() + name + '/' );
116  }
117 }
118 //-----------------------------------------------------------------------------
119 void QtDirectory::listFiles(std::vector< ref<QtFile> >& file_list, bool append) const
120 {
121  if (!append)
122  file_list.clear();
123  std::vector<String> file_names;
124  listFiles(file_names,false);
125  for(unsigned i=0; i<file_names.size(); ++i)
126  {
127  ref<QtFile> file = new QtFile;
128  // file->setPath( path() + file_names[i] );
129  file->setPath( file_names[i] );
130  file_list.push_back(file);
131  }
132 }
133 //-----------------------------------------------------------------------------
134 void QtDirectory::listFiles(std::vector<String>& files_out, bool append) const
135 {
136  if ( ! append ) {
137  files_out.clear();
138  }
139 
140  if ( path().empty() )
141  {
142  Log::error( "QtDirectory::path() must not be empty!\n" );
143  return;
144  }
145 
146  const char* p = path().toStdString().c_str();
147  QStringList files = QDir(QString(p)).entryList( QDir::Files );
148 
149  for (int i = 0; i < files.size(); ++i) {
150  vl::String name = vl::String::fromStdString( files.at(i).toStdString() );
151  files_out.push_back( path() + name + '/' );
152  }
153 }
154 //-----------------------------------------------------------------------------
155 ref<VirtualFile> QtDirectory::file(const String& name) const
156 {
157  return qtFile(name);
158 }
159 //-----------------------------------------------------------------------------
160 ref<QtFile> QtDirectory::qtFile(const String& name) const
161 {
162  String p = translatePath(name);
163  ref<QtFile> file = new QtFile(p);
164  if (file->exists())
165  return file;
166  else
167  return NULL;
168 }
169 //-----------------------------------------------------------------------------
170 void QtDirectory::listFilesRecursive_internal(std::vector<String>& file_list) const
171 {
172  // add local child
173  listFiles(file_list, true);
174  // descend recursively
175  std::vector<String> dir_list;
176  listSubDirs(dir_list);
177  for(unsigned i=0; i<dir_list.size(); ++i)
178  {
179  VL_CHECK(dir_list[i] != ".")
180  VL_CHECK(dir_list[i] != "..")
181 
182  QtDirectory sub_dir( dir_list[i] );
183  sub_dir.listFilesRecursive_internal(file_list);
184  }
185 }
186 //-----------------------------------------------------------------------------
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