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]
MemoryDirectory.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 
33 
34 using namespace vl;
35 
36 //---------------------------------------------------------------------------
38 {
39  String root = name;
40  root.trim();
41  root.normalizeSlashes();
42  if (root.empty())
43  {
44  Log::error("MemoryDirectory::setPath() given an empty path.\n");
45  return false;
46  }
47  if (!root.endsWith('/'))
48  {
49  // Log::warning( Say("MemoryDirectory::setPath() : path (%s) must end with a '/'.\n") << root );
50  root += '/';
51  }
52 
53  std::map< String, ref<MemoryFile> > file_map;
54  for( std::map< String, ref<MemoryFile> >::iterator it = mFiles.begin(); it != mFiles.end(); ++it )
55  {
56  String p = it->first;
57  if ( !p.startsWith(path()) )
58  {
59  Log::warning( Say("MemoryDirectory::setPath() : invalid path file '%s'.\n") << p );
60  continue;
61  }
62  p = p.right(-path().length());
63  it->second->setPath(root + p);
64  file_map[it->second->path()] = it->second;
65  }
66  mFiles = file_map;
67  mPath = root;
68  return true;
69 }
70 //---------------------------------------------------------------------------
72 {
73  if (path().empty())
74  {
75  Log::error( "VirtualDirectory::path() must not be empty!\n" );
76  return false;
77  }
78 
79  if ( !file->path().startsWith(path()) )
80  {
81  Log::error( Say("File '%s' does not belong to MemoryDirectory '%s'\n") << file->path() << path() );
82  return false;
83  }
84 
85  String p = file->path();
86  p.normalizeSlashes();
87  file->setPath( p );
88 
89  mFiles[file->path()] = file;
90  return true;
91 }
92 //---------------------------------------------------------------------------
94 {
95  return removeFile( file->path() );
96 }
97 //---------------------------------------------------------------------------
99 {
100  bool ok = mFiles.find( name ) != mFiles.end();
101  mFiles.erase( name );
102  return ok;
103 }
104 //---------------------------------------------------------------------------
106 {
107  String p = translatePath(name);
108  std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.find(p);
109  if (it == mFiles.end())
110  return NULL;
111  else
112  {
113  ref<MemoryFile> mem_file = new MemoryFile;
114  mem_file->operator=(*it->second);
115  return mem_file.get();
116  }
117 }
118 //---------------------------------------------------------------------------
119 void MemoryDirectory::listFilesRecursive( std::vector<String>& file_list ) const
120 {
121  file_list.clear();
122  if (path().empty())
123  {
124  Log::error( "VirtualDirectory::path() must not be empty!\n" );
125  return;
126  }
127  for( std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.begin(); it != mFiles.end(); ++it)
128  {
129  if (!it->first.startsWith(path()))
130  vl::Log::warning( Say("MemoryFile '%s' does not belong to MemoryDirectory '%s'.\n") << it->first << path() );
131  file_list.push_back( it->first );
132  }
133 }
134 //---------------------------------------------------------------------------
135 void MemoryDirectory::listSubDirs(std::vector<String>& dirs, bool append) const
136 {
137  if (!append)
138  dirs.clear();
139  if (path().empty())
140  {
141  Log::error( "VirtualDirectory::path() must not be empty!\n" );
142  return;
143  }
144  std::set<String> sub_dirs;
145  for( std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.begin(); it != mFiles.end(); ++it )
146  {
147  VL_CHECK(it->first.startsWith(path()))
148  String p = it->first.extractPath();
149  if (path().length())
150  p = p.right(-path().length());
151  while(p.startsWith('/'))
152  p = p.right(-1);
153  String drive_letter;
154  if (p.length()>3 && p[1] == ':' && p[2] == '/')
155  {
156  drive_letter = p.left(3);
157  p = p.right(-3);
158  }
159  if (p.empty()) // is a file
160  continue;
161  std::vector<String> tokens;
162  p.split('/',tokens,true);
163  if (tokens.size())
164  sub_dirs.insert(path() + tokens[0]);
165  }
166  for(std::set<String>::const_iterator it = sub_dirs.begin(); it != sub_dirs.end(); ++it)
167  dirs.push_back(*it);
168 }
169 //---------------------------------------------------------------------------
171 {
172  String p = translatePath(subdir_name);
173  if (path().empty())
174  {
175  Log::error( "VirtualDirectory::path() must not be empty!\n" );
176  return NULL;
177  }
179  for( std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.begin(); it != mFiles.end(); ++it )
180  {
181  if (it->first.startsWith(p+'/'))
182  {
183  ref<MemoryFile> mfile = static_cast<MemoryFile*>(it->second->clone().get());
184  VL_CHECK(mfile)
185  dir->mFiles[mfile->path()] = mfile;
186  }
187  }
188 
189  if (dir->mFiles.empty())
190  return NULL;
191  else
192  return dir;
193 }
194 //---------------------------------------------------------------------------
195 void MemoryDirectory::listFiles(std::vector<String>& file_list, bool append) const
196 {
197  if (!append)
198  file_list.clear();
199  if (path().empty())
200  {
201  Log::error( "VirtualDirectory::path() must not be empty!\n" );
202  return;
203  }
204  for( std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.begin(); it != mFiles.end(); ++it )
205  {
206  if (it->first.extractPath().left(-1) == path())
207  file_list.push_back( it->first );
208  }
209 }
210 //---------------------------------------------------------------------------
211 void MemoryDirectory::clone(VirtualDirectory* directory, const String& match)
212 {
213  setPath(directory->path());
214  eraseAllFiles();
215  std::vector<String> file_list;
216  directory->listFilesRecursive(file_list, match);
217  for(unsigned i=0; i<file_list.size(); ++i)
218  {
219  ref<VirtualFile> file = directory->file( file_list[i] );
220  if (file)
221  {
222  ref<MemoryFile> mem_file = new MemoryFile;
223  mem_file->copy(file.get());
224  mem_file->setPath( file->path() );
225  addFile(mem_file.get());
226  }
227  }
228 }
229 //---------------------------------------------------------------------------
bool addFile(MemoryFile *file)
The string file->path() must contain the full path including the MemoryDirectory&#39;s path() ...
const T * get() const
Definition: Object.hpp:128
A simple String formatting class.
Definition: Say.hpp:124
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
Definition: Log.cpp:155
void listSubDirs(std::vector< String > &dirs, bool append=false) const
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
void listFiles(std::vector< String > &file_list, bool append=false) const
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
ref< MemoryDirectory > memorySubDir(const String &subdir_name) const
String extractPath() const
If the String contains a file path the function returns the path with trailing slash, without the file name.
Definition: String.cpp:830
MemoryDirectory(const String &path=".")
String left(int count) const
Returns the count leftmost caracters of a String. If count is negative the function returns all but t...
Definition: String.cpp:814
String & normalizeSlashes()
Transform \ slashes in / slashes and removes duplicates.
Definition: String.cpp:534
String & trim(wchar_t ch)
Removes the specified character ch from the beginning and the end of the String.
Definition: String.cpp:322
const String & path() const
Returns the path of the file.
Definition: VirtualFile.hpp:98
Visualization Library main namespace.
virtual const String & path() const
virtual ref< VirtualFile > file(const String &name) const
Returns the VirtualFile with the given name if any, NULL otherwise.
A VirtualFile to manipulate files stored in memory.
Definition: MemoryFile.hpp:56
void clone(VirtualDirectory *directory, const String &match="*")
Clones the content of another directory (empty directories are never cloned).
virtual void listFilesRecursive(std::vector< String > &file_list) const =0
Returns the list of files contained in the VirtualDirectory.
Abstact class representing a directory of files.
bool empty() const
Returns true if length() == 0.
Definition: String.hpp:136
void split(wchar_t separator, std::vector< String > &fields, bool remove_empty_fields=false) const
Splits a String into a set of fields. The fields are separated by the specified separator and are ret...
Definition: String.cpp:386
bool startsWith(const String &str) const
Returns true if a String starts with the specified String str.
Definition: String.cpp:720
#define NULL
Definition: OpenGLDefs.hpp:81
void setPath(const String &name)
Changes the path bound to a VirtualFile. Use carefully this function, you shouldn&#39;t rename a VirtualF...
bool removeFile(MemoryFile *file)
The string file->path() must contain the full path including the MemoryDirectory&#39;s path() ...
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
bool endsWith(const String &str) const
Returns true if a String ends with the specified String str.
Definition: String.cpp:705
ref< MemoryFile > memoryFile(const String &name) const
#define VL_CHECK(expr)
Definition: checks.hpp:73
std::map< String, ref< MemoryFile > > mFiles
void copy(VirtualFile *file)
Copies the data of any kind of VirtualFile.
Definition: MemoryFile.cpp:60
String translatePath(const String &p) const
void listFilesRecursive(std::vector< String > &file_list) const
Returns the list of files contained in the VirtualDirectory.
virtual ref< VirtualFile > file(const String &name) const =0
Returns the VirtualFile with the given name if any, NULL otherwise.
virtual bool setPath(const String &name)
Changes the path name of a VirtualDirectory. Must not be an empty string.