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