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]
LoadWriterManager.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 #include <vlCore/Log.hpp>
34 #include <vlCore/Say.hpp>
35 #include <vlCore/Time.hpp>
36 #include <vlCore/GZipCodec.hpp>
37 
38 using namespace vl;
39 
40 //-----------------------------------------------------------------------------
42 {
43  if (file->path().empty())
44  Log::warning("findLoader(VirtualFile* file): cannot check the file extension, file->path() is empty!\n");
45  return findLoader(file->path());
46 }
47 //-----------------------------------------------------------------------------
49 {
50  if (file->path().empty())
51  Log::warning("findWriter(VirtualFile* file): cannot check the file extension, file->path() is empty!\n");
52  return findWriter(file->path());
53 }
54 //-----------------------------------------------------------------------------
56 {
57  struct TimerClass
58  {
59  TimerClass(const String* path): mPath(path)
60  {
61  mTimer.start();
62  }
63  ~TimerClass()
64  {
65  Log::debug( Say("loadResource('%s'): %ns\n") << *mPath << mTimer.elapsed() );
66  }
67  Time mTimer;
68  const String* mPath;
69  } timer(&path);
70 
71  ref<VirtualFile> file = locateFile(path);
72 
73  if (file)
74  {
75  if (path.endsWith(".gz") || path.endsWith(".GZ"))
76  {
77  ref<GZipCodec> gz = new GZipCodec;
78  gz->setStream(file.get());
79  // remove .gz suffix so that correct loader can be picked up
80  gz->setPath( file->path().left(-3) );
81  file = gz;
82  }
83  return loadResource(file.get(), quick);
84  }
85  else
86  return NULL;
87 }
88 //-----------------------------------------------------------------------------
90 {
91  const ResourceLoadWriter* loadwriter = findLoader(file);
92  if (loadwriter)
93  {
95  if (quick)
96  {
97  // caching the data in the memory provides a huge performance boost
98  ref<MemoryFile> memfile = new MemoryFile;
99  memfile->allocateBuffer(file->size());
100  file->open(OM_ReadOnly);
101  file->read(memfile->ptr(),file->size());
102  file->close();
103  memfile->setPath(file->path());
104  db = loadwriter->loadResource(memfile.get());
105  }
106  else
107  db = loadwriter->loadResource(file);
108  // load callbacks
109  for(size_t i=0; db && i<loadCallbacks().size(); ++i)
110  loadCallbacks()[i].get_writable()->operator()(db.get());
111  return db;
112  }
113  else
114  {
115  Log::error( Say("no ResourceLoadWriter registered to load '%s'.\n") << file->path() );
116  return NULL;
117  }
118 }
119 //-----------------------------------------------------------------------------
120 bool LoadWriterManager::writeResource(const String& path, ResourceDatabase* resource) const
121 {
122  const ResourceLoadWriter* loadwriter = findWriter(path);
123  if (loadwriter)
124  {
125  // write callbacks
126  for(size_t i=0; i<writeCallbacks().size(); ++i)
127  writeCallbacks()[i].get_writable()->operator()(resource);
128  // write resource
129  return loadwriter->writeResource(path,resource);
130  }
131  else
132  {
133  Log::error( Say("no ResourceLoadWriter registered to write '%s'.\n") << path );
134  return false;
135  }
136 }
137 //-----------------------------------------------------------------------------
139 {
140  const ResourceLoadWriter* loadwriter = findWriter(file);
141  if (loadwriter)
142  {
143  // write callbacks
144  for(size_t i=0; i<writeCallbacks().size(); ++i)
145  writeCallbacks()[i].get_writable()->operator()(resource);
146  // write resource
147  return loadwriter->writeResource(file,resource);
148  }
149  else
150  {
151  Log::error( Say("no ResourceLoadWriter registered to write '%s'.\n") << file->path() );
152  return false;
153  }
154 }
155 //-----------------------------------------------------------------------------
157 {
158  String ext = path.extractFileExtension(false).toLowerCase();
159  for(size_t i=0; i<loadWriters().size(); ++i) {
160  const ResourceLoadWriter* lw = loadWriters()[i].get();
161  if ( lw->canLoad(ext) )
162  return lw;
163  }
164 
165  return NULL;
166 }
167 //-----------------------------------------------------------------------------
169 {
170  String ext = path.extractFileExtension(false).toLowerCase();
171  for(size_t i=0; i<loadWriters().size(); ++i)
172  if (loadWriters()[i]->canWrite(ext))
173  return loadWriters()[i].get();
174 
175  return NULL;
176 }
177 //-----------------------------------------------------------------------------
179 {
180  ref<ResourceLoadWriter> lowr = load_writer;
181  // remove load writer
182  std::vector< ref<ResourceLoadWriter> >::iterator it = std::find( loadWriters().begin(), loadWriters().end(), load_writer );
183  if( it != loadWriters().end() )
184  loadWriters().erase( it );
185  // insert load writer
186  loadWriters().push_back( load_writer );
187 }
188 //-----------------------------------------------------------------------------
189 bool vl::canLoad(const String& path) { return defLoadWriterManager()->canLoad(path); }
190 //-----------------------------------------------------------------------------
191 bool vl::canWrite(const String& path) { return defLoadWriterManager()->canWrite(path); }
192 //-----------------------------------------------------------------------------
193 bool vl::canLoad(VirtualFile* file) { return defLoadWriterManager()->canLoad(file); }
194 //-----------------------------------------------------------------------------
195 bool vl::canWrite(VirtualFile* file) { return defLoadWriterManager()->canWrite(file); }
196 //-----------------------------------------------------------------------------
197 ref<ResourceDatabase> vl::loadResource(const String& path, bool quick) { return defLoadWriterManager()->loadResource(path,quick); }
198 //-----------------------------------------------------------------------------
200 //-----------------------------------------------------------------------------
201 bool vl::writeResource(const String& path, ResourceDatabase* resource) { return defLoadWriterManager()->writeResource(path, resource); }
202 //-----------------------------------------------------------------------------
203 bool vl::writeResource(VirtualFile* file, ResourceDatabase* resource) { return defLoadWriterManager()->writeResource(file, resource); }
204 //-----------------------------------------------------------------------------
205 
206 
static void debug(const String &message)
Use this function to provide extra information useful to investigate and solve problems.
Definition: Log.cpp:145
virtual ref< ResourceDatabase > loadResource(const String &path) const =0
String toLowerCase() const
Returns the lower-case version of a String.
Definition: String.cpp:755
VLCORE_EXPORT bool canLoad(const String &path)
Short version of defLoadWriterManager()->canLoad(path).
long long read(void *buffer, long long byte_count)
Reads byte_count bytes from a file. Returns the number of bytes actually read.
Definition: VirtualFile.cpp:82
String extractFileExtension(bool require_dot=true) const
If the String contains a file name or file path the function returns the extension of the file...
Definition: String.cpp:857
const T * get() const
Definition: Object.hpp:128
An abstract class representing a file.
Definition: VirtualFile.hpp:60
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
const std::vector< ref< WriteCallback > > & writeCallbacks() const
unsigned char * ptr()
Definition: MemoryFile.hpp:70
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
bool canLoad(const String &extension) const
Returns true if the given file type can be loaded.
void setStream(VirtualFile *stream)
Installs the VirtualFile representing the GZip file to be read or to be written.
Definition: GZipCodec.cpp:375
const std::vector< ref< LoadCallback > > & loadCallbacks() const
ref< ResourceDatabase > loadResource(const String &path, bool quick=true) const
Loads the resource specified by the given path using the appropriate ResourceLoadWriter.
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
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
virtual bool writeResource(const String &path, ResourceDatabase *resource) const =0
virtual void close()=0
Closes the file.
VLCORE_EXPORT ref< ResourceDatabase > loadResource(const String &path, bool quick=true)
Short version of defLoadWriterManager()->loadResource(path, quick).
const String & path() const
Returns the path of the file.
Definition: VirtualFile.hpp:98
Visualization Library main namespace.
Simple class to be used as a timer and to retrieve the current time and date.
Definition: Time.hpp:49
A VirtualFile to manipulate files stored in memory.
Definition: MemoryFile.hpp:56
VLCORE_EXPORT bool canWrite(const String &path)
Short version of defLoadWriterManager()->canWrite(path).
The GZipCodec class is a VirtualFile that transparently encodes and decodes a stream of data using th...
Definition: GZipCodec.hpp:43
bool empty() const
Returns true if length() == 0.
Definition: String.hpp:136
virtual bool open(EOpenMode mode)=0
Opens the file in the specified mode.
const ResourceLoadWriter * findWriter(const String &path) const
Returns the ResourceLoadWriter that has been registered to write the resource type specified by the g...
std::vector< ref< ResourceLoadWriter > > & loadWriters()
Returns the set of registered ResourceLoadWriter objects.
#define NULL
Definition: OpenGLDefs.hpp:81
virtual long long size() const =0
Returns the size of the file in bytes.
void allocateBuffer(long long byte_count)
Definition: MemoryFile.hpp:81
bool canWrite(const String &path) const
Returns true if there is a ResourceLoadWriter registered to write the specified path or extension...
bool canLoad(const String &path) const
Returns true if there is a ResourceLoadWriter registered to load the specified path or extension...
VLCORE_EXPORT ref< VirtualFile > locateFile(const String &path)
Utility function, equivalent to vl::defFileSystem()->locateFile(path)
Definition: VirtualFile.cpp:41
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
bool endsWith(const String &str) const
Returns true if a String ends with the specified String str.
Definition: String.cpp:705
The ResourceLoadWriter class is an abstract class used to implement read/write support for one or mor...
VLCORE_EXPORT bool writeResource(const String &path, ResourceDatabase *resource)
Short version of defLoadWriterManager()->writeResource(path, resource).
VLCORE_EXPORT LoadWriterManager * defLoadWriterManager()
Returs the default LoadWriterManager used by Visualization Library.
Definition: pimpl.cpp:82
The ResourceDatabase class contains and manipulates a set of resources.
const ResourceLoadWriter * findLoader(const String &path) const
Returns the ResourceLoadWriter that has been registered to load the resource type specified by the gi...
bool writeResource(const String &path, ResourceDatabase *resource) const
Writes the resource specified by the given file using the appropriate ResourceLoadWriter.
void registerLoadWriter(ResourceLoadWriter *)