# Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/LoadWriterManager.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    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
    00036 using namespace vl;
    00037
    00038 //-----------------------------------------------------------------------------
    00039 const ResourceLoadWriter* LoadWriterManager::findLoader(VirtualFile* file) const
    00040 {
    00041   if (file->path().empty())
    00042     Log::warning("findLoader(VirtualFile* file): cannot check the file extension, file->path() is empty!\n");
    00043   return findLoader(file->path());
    00044 }
    00045 //-----------------------------------------------------------------------------
    00046 const ResourceLoadWriter* LoadWriterManager::findWriter(VirtualFile* file) const
    00047 {
    00048   if (file->path().empty())
    00049     Log::warning("findWriter(VirtualFile* file): cannot check the file extension, file->path() is empty!\n");
    00050   return findWriter(file->path());
    00051 }
    00052 //-----------------------------------------------------------------------------
    00053 ref<ResourceDatabase> LoadWriterManager::loadResource(const String& path, bool quick) const
    00054 {
    00055   ref<VirtualFile> file = locateFile(path);
    00056   if (file)
    00057     return loadResource(file.get(), quick);
    00058   else
    00059     return NULL;
    00060 }
    00061 //-----------------------------------------------------------------------------
    00062 ref<ResourceDatabase> LoadWriterManager::loadResource(VirtualFile* file, bool quick) const
    00063 {
    00064   const ResourceLoadWriter* loadwriter = findLoader(file);
    00065   if (loadwriter)
    00066   {
    00067     ref<ResourceDatabase> db;
    00068     if (quick)
    00069     {
    00070       // caching the data in the memory provides a huge performance boost
    00071       ref<MemoryFile> memfile = new MemoryFile;
    00072       memfile->allocateBuffer(file->size());
    00073       file->open(OM_ReadOnly);
    00074       file->read(memfile->ptr(),file->size());
    00075       file->close();
    00076       memfile->setPath(file->path());
    00077       db = loadwriter->loadResource(memfile.get());
    00078     }
    00079     else
    00080       db = loadwriter->loadResource(file);
    00081     // load callbacks
    00082     for(int i=0; db && i<loadCallbacks()->size(); ++i)
    00083       const_cast<LoadCallback*>(loadCallbacks()->at(i))->operator()(db.get());
    00084     return db;
    00085   }
    00086   else
    00087   {
    00088     Log::error( Say("no ResourceLoadWriter registered to load '%s'.\n") << file->path() );
    00089     return NULL;
    00090   }
    00091 }
    00092 //-----------------------------------------------------------------------------
    00093 bool LoadWriterManager::writeResource(const String& path, ResourceDatabase* resource) const
    00094 {
    00095   const ResourceLoadWriter* loadwriter = findWriter(path);
    00096   if (loadwriter)
    00097   {
    00098     // write callbacks
    00099     for(int i=0; i<writeCallbacks()->size(); ++i)
    00100       const_cast<WriteCallback*>(writeCallbacks()->at(i))->operator()(resource);
    00101     // write resource
    00102     return loadwriter->writeResource(path,resource);
    00103   }
    00104   else
    00105   {
    00106     Log::error( Say("no ResourceLoadWriter registered to write '%s'.\n") << path );
    00107     return false;
    00108   }
    00109 }
    00110 //-----------------------------------------------------------------------------
    00111 bool LoadWriterManager::writeResource(VirtualFile* file, ResourceDatabase* resource) const
    00112 {
    00113   const ResourceLoadWriter* loadwriter = findWriter(file);
    00114   if (loadwriter)
    00115   {
    00116     // write callbacks
    00117     for(int i=0; i<writeCallbacks()->size(); ++i)
    00118       const_cast<WriteCallback*>(writeCallbacks()->at(i))->operator()(resource);
    00119     // write resource
    00120     return loadwriter->writeResource(file,resource);
    00121   }
    00122   else
    00123   {
    00124     Log::error( Say("no ResourceLoadWriter registered to write '%s'.\n") << file->path() );
    00125     return false;
    00126   }
    00127 }
    00128 //-----------------------------------------------------------------------------
    00129 const ResourceLoadWriter* LoadWriterManager::findLoader(const String& path) const
    00130 {
    00131   String ext = path.extractFileExtension(false).toLowerCase();
    00132   for(int i=0; i<loadWriters()->size(); ++i)
    00133     if (loadWriters()->at(i)->canLoad(ext))
    00134       return loadWriters()->at(i);
    00135
    00136   return NULL;
    00137 }
    00138 //-----------------------------------------------------------------------------
    00139 const ResourceLoadWriter* LoadWriterManager::findWriter(const String& path) const
    00140 {
    00141   String ext = path.extractFileExtension(false).toLowerCase();
    00142   for(int i=0; i<loadWriters()->size(); ++i)
    00143     if (loadWriters()->at(i)->canWrite(ext))
    00144       return loadWriters()->at(i);
    00145
    00146   return NULL;
    00147 }
    00148 //-----------------------------------------------------------------------------
    00149 void LoadWriterManager::registerLoadWriter(ResourceLoadWriter* load_writer)
    00150 {
    00151   ref<ResourceLoadWriter> lowr = load_writer;
    00152   LoadWriterManager::loadWriters()->erase( load_writer );
    00153   LoadWriterManager::loadWriters()->push_back( load_writer );
    00154 }
    00155 //-----------------------------------------------------------------------------
    00156 bool vl::canLoad(const String& path)  { return defLoadWriterManager()->canLoad(path);  }
    00157 //-----------------------------------------------------------------------------
    00158 bool vl::canWrite(const String& path) { return defLoadWriterManager()->canWrite(path); }
    00159 //-----------------------------------------------------------------------------
    00160 bool vl::canLoad(VirtualFile* file)   { return defLoadWriterManager()->canLoad(file);  }
    00161 //-----------------------------------------------------------------------------
    00162 bool vl::canWrite(VirtualFile* file)  { return defLoadWriterManager()->canWrite(file); }
    00163 //-----------------------------------------------------------------------------
    00164 ref<ResourceDatabase> vl::loadResource(const String& path, bool quick) { return defLoadWriterManager()->loadResource(path,quick); }
    00165 //-----------------------------------------------------------------------------
    00166 ref<ResourceDatabase> vl::loadResource(VirtualFile* file, bool quick)  { return defLoadWriterManager()->loadResource(file,quick); }
    00167 //-----------------------------------------------------------------------------
    00168
    00169

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:21.
Permission is granted to use this page to write and publish articles regarding Visualization Library.