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]
FileSystem.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 <vlCore/FileSystem.hpp>
33 #include <vlCore/DiskDirectory.hpp>
35 
36 using namespace vl;
37 
38 //-----------------------------------------------------------------------------
61 ref<VirtualFile> FileSystem::locateFile(const String& full_path, const String& alternate_path) const
62 {
63  std::vector<String> paths;
64  paths.push_back(full_path);
65  if (!alternate_path.empty())
66  paths.push_back(alternate_path + '/' + full_path);
67  if( full_path.extractFileName() != full_path )
68  {
69  paths.push_back(full_path.extractFileName());
70  if (!alternate_path.empty())
71  paths.push_back(alternate_path + '/' + full_path.extractFileName());
72  }
73 
74  for(unsigned ipath=0; ipath<paths.size(); ++ipath)
75  {
76  paths[ipath].normalizeSlashes();
77 
78  // first look in the "." directory
79  ref<DiskFile> disk_file = new DiskFile( paths[ipath] );
80  if ( disk_file->exists() )
81  return disk_file;
82 
83  // iterate backwards
84  for( int idir=(int)directories().size(); idir--; )
85  {
86  // returns the first one found
87  ref<VirtualFile> file = directories()[idir]->file( paths[ipath] );
88  if (file)
89  return file;
90  }
91  }
92 
93  return NULL;
94 }
95 //-----------------------------------------------------------------------------
97 {
98  // first look in the "." directory
99  ref<DiskDirectory> disk_directory = new DiskDirectory( name );
100  if ( disk_directory->exists() )
101  return disk_directory;
102 
103  // iterate backwards
104  for( int idir=(int)directories().size(); idir--; )
105  {
106  // returns the first one found
107  ref<VirtualDirectory> dir = directories()[idir]->subDir(name);
108  if (dir)
109  return dir;
110  }
111 
112  return NULL;
113 }
114 //-----------------------------------------------------------------------------
115 void FileSystem::listFilesRecursive(std::vector<String>& file_list ) const
116 {
117  file_list.clear();
118  std::vector<String> file_list_part;
119  // iterate backwards
120  for( int idir=(int)directories().size(); idir--; )
121  {
122  directories()[idir]->listFilesRecursive(file_list_part);
123  file_list.reserve( file_list.size() + file_list_part.size() );
124  for(unsigned j=0; j<file_list_part.size(); ++j)
125  file_list.push_back( file_list_part[j] );
126  }
127 }
128 //-----------------------------------------------------------------------------
129 void FileSystem::listFilesRecursive(std::vector<String>& file_list, const String& match) const
130 {
131  file_list.clear();
132  std::vector<String> file_list_part;
133  // iterate backwards
134  for( int idir=(int)directories().size(); idir--; )
135  {
136  directories()[idir]->listFilesRecursive(file_list_part, match);
137  file_list.reserve( file_list.size() + file_list_part.size() );
138  for(unsigned j=0; j<file_list_part.size(); ++j)
139  file_list.push_back( file_list_part[j] );
140  }
141 }
142 //-----------------------------------------------------------------------------
std::vector< ref< VirtualDirectory > > & directories()
Returns the list of VirtualDirectory objects added to a FileSystem.
Definition: FileSystem.hpp:84
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
bool exists() const
A VirtualDirectory that operates on reguar disk directories.
virtual ref< VirtualFile > locateFile(const String &full_path, const String &alternate_path=String()) const
Looks for a VirtualFile on the disk and in the currently active FileSystem.
Definition: FileSystem.cpp:61
Visualization Library main namespace.
virtual void listFilesRecursive(std::vector< String > &file_list) const
Returns the names of all the files contained in the previously added Directories. ...
Definition: FileSystem.cpp:115
virtual ref< VirtualDirectory > locateDirectory(const String &name) const
Definition: FileSystem.cpp:96
bool empty() const
Returns true if length() == 0.
Definition: String.hpp:136
String extractFileName() const
If the String contains a file path the function returns the file name without the path...
Definition: String.cpp:845
#define NULL
Definition: OpenGLDefs.hpp:81
A VirtualFile that operates on regular disk files.
Definition: DiskFile.hpp:64
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
virtual bool exists() const
Returns true if the file exists.
Definition: DiskFile.cpp:172