Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/LoadWriterManager.cpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #include <vlCore/LoadWriterManager.hpp>
00033 #include <vlCore/Log.hpp>
00034 #include <vlCore/Say.hpp>
00035 #include <vlCore/Time.hpp>
00036 #include <vlCore/GZipCodec.hpp>
00037 
00038 using namespace vl;
00039 
00040 //-----------------------------------------------------------------------------
00041 const ResourceLoadWriter* LoadWriterManager::findLoader(VirtualFile* file) const 
00042 {
00043   if (file->path().empty())
00044     Log::warning("findLoader(VirtualFile* file): cannot check the file extension, file->path() is empty!\n");
00045   return findLoader(file->path());
00046 }
00047 //-----------------------------------------------------------------------------
00048 const ResourceLoadWriter* LoadWriterManager::findWriter(VirtualFile* file) const 
00049 {
00050   if (file->path().empty())
00051     Log::warning("findWriter(VirtualFile* file): cannot check the file extension, file->path() is empty!\n");
00052   return findWriter(file->path());
00053 }
00054 //-----------------------------------------------------------------------------
00055 ref<ResourceDatabase> LoadWriterManager::loadResource(const String& path, bool quick) const 
00056 {
00057   struct TimerClass
00058   {
00059     TimerClass(const String* path): mPath(path)
00060     {
00061       mTimer.start();
00062     }
00063     ~TimerClass()
00064     {
00065       Log::debug( Say("loadResource('%s'): %ns\n") << *mPath << mTimer.elapsed() );
00066     }
00067     Time mTimer;
00068     const String* mPath;
00069   } timer(&path);
00070 
00071   ref<VirtualFile> file = locateFile(path);
00072 
00073   if (file)
00074   {
00075     if (path.endsWith(".gz") || path.endsWith(".GZ"))
00076     {
00077       ref<GZipCodec> gz = new GZipCodec;
00078       gz->setStream(file.get());
00079       // remove .gz suffix so that correct loader can be picked up
00080       gz->setPath( file->path().left(-3) );
00081       file = gz;
00082     }
00083     return loadResource(file.get(), quick);
00084   }
00085   else
00086     return NULL;
00087 }
00088 //-----------------------------------------------------------------------------
00089 ref<ResourceDatabase> LoadWriterManager::loadResource(VirtualFile* file, bool quick) const 
00090 {
00091   const ResourceLoadWriter* loadwriter = findLoader(file);
00092   if (loadwriter)
00093   {
00094     ref<ResourceDatabase> db;
00095     if (quick)
00096     {
00097       // caching the data in the memory provides a huge performance boost
00098       ref<MemoryFile> memfile = new MemoryFile;
00099       memfile->allocateBuffer(file->size());
00100       file->open(OM_ReadOnly);
00101       file->read(memfile->ptr(),file->size());
00102       file->close();
00103       memfile->setPath(file->path());
00104       db = loadwriter->loadResource(memfile.get());
00105     }
00106     else
00107       db = loadwriter->loadResource(file);
00108     // load callbacks
00109     for(size_t i=0; db && i<loadCallbacks().size(); ++i)
00110       loadCallbacks()[i].get_writable()->operator()(db.get());
00111     return db;
00112   }
00113   else
00114   {
00115     Log::error( Say("no ResourceLoadWriter registered to load '%s'.\n") << file->path() );
00116     return NULL; 
00117   }
00118 }
00119 //-----------------------------------------------------------------------------
00120 bool LoadWriterManager::writeResource(const String& path, ResourceDatabase* resource) const
00121 {
00122   const ResourceLoadWriter* loadwriter = findWriter(path);
00123   if (loadwriter)
00124   {
00125     // write callbacks
00126     for(size_t i=0; i<writeCallbacks().size(); ++i)
00127       writeCallbacks()[i].get_writable()->operator()(resource);
00128     // write resource
00129     return loadwriter->writeResource(path,resource);
00130   }
00131   else
00132   {
00133     Log::error( Say("no ResourceLoadWriter registered to write '%s'.\n") << path );
00134     return false; 
00135   }
00136 }
00137 //-----------------------------------------------------------------------------
00138 bool LoadWriterManager::writeResource(VirtualFile* file, ResourceDatabase* resource) const
00139 { 
00140   const ResourceLoadWriter* loadwriter = findWriter(file);
00141   if (loadwriter)
00142   {
00143     // write callbacks
00144     for(size_t i=0; i<writeCallbacks().size(); ++i)
00145       writeCallbacks()[i].get_writable()->operator()(resource);
00146     // write resource
00147     return loadwriter->writeResource(file,resource);
00148   }
00149   else
00150   {
00151     Log::error( Say("no ResourceLoadWriter registered to write '%s'.\n") << file->path() );
00152     return false; 
00153   }
00154 }
00155 //-----------------------------------------------------------------------------
00156 const ResourceLoadWriter* LoadWriterManager::findLoader(const String& path) const 
00157 {
00158   String ext = path.extractFileExtension(false).toLowerCase();
00159   for(size_t i=0; i<loadWriters().size(); ++i)
00160     if (loadWriters()[i]->canLoad(ext))
00161       return loadWriters()[i].get();
00162 
00163   return NULL;
00164 }
00165 //-----------------------------------------------------------------------------
00166 const ResourceLoadWriter* LoadWriterManager::findWriter(const String& path) const 
00167 {
00168   String ext = path.extractFileExtension(false).toLowerCase();
00169   for(size_t i=0; i<loadWriters().size(); ++i)
00170     if (loadWriters()[i]->canWrite(ext))
00171       return loadWriters()[i].get();
00172 
00173   return NULL;
00174 }
00175 //-----------------------------------------------------------------------------
00176 void LoadWriterManager::registerLoadWriter(ResourceLoadWriter* load_writer)
00177 {
00178   ref<ResourceLoadWriter> lowr = load_writer;
00179   // remove load writer
00180   std::vector< ref<ResourceLoadWriter> >::iterator it = std::find( loadWriters().begin(), loadWriters().end(), load_writer );
00181   if( it != loadWriters().end() )
00182     loadWriters().erase( it );
00183   // insert load writer
00184   loadWriters().push_back( load_writer );
00185 }
00186 //-----------------------------------------------------------------------------
00187 bool vl::canLoad(const String& path)  { return defLoadWriterManager()->canLoad(path);  }
00188 //-----------------------------------------------------------------------------
00189 bool vl::canWrite(const String& path) { return defLoadWriterManager()->canWrite(path); }
00190 //-----------------------------------------------------------------------------
00191 bool vl::canLoad(VirtualFile* file)   { return defLoadWriterManager()->canLoad(file);  }
00192 //-----------------------------------------------------------------------------
00193 bool vl::canWrite(VirtualFile* file)  { return defLoadWriterManager()->canWrite(file); }
00194 //-----------------------------------------------------------------------------
00195 ref<ResourceDatabase> vl::loadResource(const String& path, bool quick) { return defLoadWriterManager()->loadResource(path,quick); }
00196 //-----------------------------------------------------------------------------
00197 ref<ResourceDatabase> vl::loadResource(VirtualFile* file, bool quick)  { return defLoadWriterManager()->loadResource(file,quick); }
00198 //-----------------------------------------------------------------------------
00199 bool vl::writeResource(const String& path, ResourceDatabase* resource) { return defLoadWriterManager()->writeResource(path, resource); }
00200 //-----------------------------------------------------------------------------
00201 bool vl::writeResource(VirtualFile* file, ResourceDatabase* resource) { return defLoadWriterManager()->writeResource(file, resource); }
00202 //-----------------------------------------------------------------------------
00203 
00204 

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:29.
Permission is granted to use this page to write and publish articles regarding Visualization Library.