# Visualization Library

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

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

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/MemoryDirectory.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/MemoryDirectory.hpp>
    00033
    00034 using namespace vl;
    00035
    00036 //---------------------------------------------------------------------------
    00037 bool MemoryDirectory::setPath(const String& name)
    00038 {
    00039   String root = name;
    00040   root.trim();
    00041   root.normalizeSlashes();
    00042   if (root.empty())
    00043   {
    00044     Log::error("MemoryDirectory::setPath() given an empty path.\n");
    00045     return false;
    00046   }
    00047   if (!root.endsWith('/'))
    00048   {
    00049     // Log::warning( Say("MemoryDirectory::setPath() : path (%s) must end with a '/'.\n") << root );
    00050     root += '/';
    00051   }
    00052
    00053   std::map< String, ref<MemoryFile> > file_map;
    00054   for( std::map< String, ref<MemoryFile> >::iterator it = mFiles.begin(); it != mFiles.end(); ++it )
    00055   {
    00056     String p = it->first;
    00057     if ( !p.startsWith(path()) )
    00058     {
    00059       Log::warning( Say("MemoryDirectory::setPath() : invalid path file '%s'.\n") << p );
    00060       continue;
    00061     }
    00062     p = p.right(-path().length());
    00063     it->second->setPath(root + p);
    00064     file_map[it->second->path()] = it->second;
    00065   }
    00066   mFiles = file_map;
    00067   mPath = root;
    00068   return true;
    00069 }
    00070 //---------------------------------------------------------------------------
    00071 bool MemoryDirectory::addFile(MemoryFile* file)
    00072 {
    00073   if (path().empty())
    00074   {
    00075     Log::error( "VirtualDirectory::path() must not be empty!\n" );
    00076     return false;
    00077   }
    00078
    00079   if ( !file->path().startsWith(path()) )
    00080   {
    00081     Log::error( Say("File '%s' does not belong to MemoryDirectory '%s'\n") << file->path() << path() );
    00082     return false;
    00083   }
    00084
    00085   String p = file->path();
    00086   p.normalizeSlashes();
    00087   file->setPath( p );
    00088
    00089   mFiles[file->path()] = file;
    00090   return true;
    00091 }
    00092 //---------------------------------------------------------------------------
    00093 bool MemoryDirectory::removeFile(MemoryFile* file)
    00094 {
    00095   return removeFile( file->path() );
    00096 }
    00097 //---------------------------------------------------------------------------
    00098 bool MemoryDirectory::removeFile(const String& name)
    00099 {
    00100   bool ok = mFiles.find( name ) != mFiles.end();
    00101   mFiles.erase( name );
    00102   return ok;
    00103 }
    00104 //---------------------------------------------------------------------------
    00105 ref<MemoryFile> MemoryDirectory::memoryFile(const String& name) const
    00106 {
    00107   String p = translatePath(name);
    00108   std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.find(p);
    00109   if (it == mFiles.end())
    00110     return NULL;
    00111   else
    00112   {
    00113     ref<MemoryFile> mem_file = new MemoryFile;
    00114     mem_file->operator=(*it->second);
    00115     return mem_file.get();
    00116   }
    00117 }
    00118 //---------------------------------------------------------------------------
    00119 void MemoryDirectory::listFilesRecursive( std::vector<String>& file_list ) const
    00120 {
    00121   file_list.clear();
    00122   if (path().empty())
    00123   {
    00124     Log::error( "VirtualDirectory::path() must not be empty!\n" );
    00125     return;
    00126   }
    00127   for( std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.begin(); it != mFiles.end();  ++it)
    00128   {
    00129     if (!it->first.startsWith(path()))
    00130       vl::Log::warning( Say("MemoryFile '%s' does not belong to MemoryDirectory '%s'.\n") << it->first << path() );
    00131     file_list.push_back( it->first );
    00132   }
    00133 }
    00134 //---------------------------------------------------------------------------
    00135 void MemoryDirectory::listSubDirs(std::vector<String>& dirs, bool append) const
    00136 {
    00137   if (!append)
    00138     dirs.clear();
    00139   if (path().empty())
    00140   {
    00141     Log::error( "VirtualDirectory::path() must not be empty!\n" );
    00142     return;
    00143   }
    00144   std::set<String> sub_dirs;
    00145   for( std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.begin(); it != mFiles.end(); ++it )
    00146   {
    00147     VL_CHECK(it->first.startsWith(path()))
    00148     String p = it->first.extractPath();
    00149     if (path().length())
    00150       p = p.right(-path().length());
    00151     while(p.startsWith('/'))
    00152       p = p.right(-1);
    00153     String drive_letter;
    00154     if (p.length()>3 && p[1] == ':' && p[2] == '/')
    00155     {
    00156       drive_letter = p.left(3);
    00157       p = p.right(-3);
    00158     }
    00159     if (p.empty()) // is a file
    00160       continue;
    00161     std::vector<String> tokens;
    00162     p.split('/',tokens,true);
    00163     if (tokens.size())
    00164       sub_dirs.insert(path() + tokens[0]);
    00165   }
    00166   for(std::set<String>::const_iterator it = sub_dirs.begin(); it != sub_dirs.end(); ++it)
    00167     dirs.push_back(*it);
    00168 }
    00169 //---------------------------------------------------------------------------
    00170 ref<MemoryDirectory> MemoryDirectory::memorySubDir(const String& subdir_name) const
    00171 {
    00172   String p = translatePath(subdir_name);
    00173   if (path().empty())
    00174   {
    00175     Log::error( "VirtualDirectory::path() must not be empty!\n" );
    00176     return NULL;
    00177   }
    00178   ref<MemoryDirectory> dir = new MemoryDirectory(p);
    00179   for( std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.begin(); it != mFiles.end(); ++it )
    00180   {
    00181     if (it->first.startsWith(p+'/'))
    00182     {
    00183       ref<MemoryFile> mfile = static_cast<MemoryFile*>(it->second->clone().get());
    00184       VL_CHECK(mfile)
    00185       dir->mFiles[mfile->path()] = mfile;
    00186     }
    00187   }
    00188
    00189   if (dir->mFiles.empty())
    00190     return NULL;
    00191   else
    00192     return dir;
    00193 }
    00194 //---------------------------------------------------------------------------
    00195 void MemoryDirectory::listFiles(std::vector<String>& file_list, bool append) const
    00196 {
    00197   if (!append)
    00198     file_list.clear();
    00199   if (path().empty())
    00200   {
    00201     Log::error( "VirtualDirectory::path() must not be empty!\n" );
    00202     return;
    00203   }
    00204   for( std::map< String, ref<MemoryFile> >::const_iterator it = mFiles.begin(); it != mFiles.end(); ++it )
    00205   {
    00206     if (it->first.extractPath().left(-1) == path())
    00207       file_list.push_back( it->first );
    00208   }
    00209 }
    00210 //---------------------------------------------------------------------------
    00211 void MemoryDirectory::clone(VirtualDirectory* directory, const String& match)
    00212 {
    00213   setPath(directory->path());
    00214   eraseAllFiles();
    00215   std::vector<String> file_list;
    00216   directory->listFilesRecursive(file_list, match);
    00217   for(unsigned i=0; i<file_list.size(); ++i)
    00218   {
    00219     ref<VirtualFile> file = directory->file( file_list[i] );
    00220     if (file)
    00221     {
    00222       ref<MemoryFile> mem_file = new MemoryFile;
    00223       mem_file->copy(file.get());
    00224       mem_file->setPath( file->path() );
    00225       addFile(mem_file.get());
    00226     }
    00227   }
    00228 }
    00229 //---------------------------------------------------------------------------

Visualization Library v2011.09.1162 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu Jan 26 2012 00:43:29.
Permission is granted to use this page to write and publish articles regarding Visualization Library.