Commit 1040ab74 authored by Michele Bosi's avatar Michele Bosi

Added support for Qt4 virtual filesystem

parent b86ffb0b
/**************************************************************************************/
/* */
/* Visualization Library */
/* http://visualizationlibrary.org */
/* */
/* Copyright (c) 2005-2016, Michele Bosi */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without modification, */
/* are permitted provided that the following conditions are met: */
/* */
/* - Redistributions of source code must retain the above copyright notice, this */
/* list of conditions and the following disclaimer. */
/* */
/* - Redistributions in binary form must reproduce the above copyright notice, this */
/* list of conditions and the following disclaimer in the documentation and/or */
/* other materials provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
/* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
/* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
/* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/**************************************************************************************/
#include <vlQt4/QtDirectory.hpp>
#include <vlQt4/QtFile.hpp>
#include <algorithm>
#include <QDir>
using namespace vl;
//-----------------------------------------------------------------------------
// QtDirectory
//-----------------------------------------------------------------------------
QtDirectory::QtDirectory( const String& name )
{
setPath(name);
}
//-----------------------------------------------------------------------------
QtDirectory::QtDirectory()
{
}
//-----------------------------------------------------------------------------
void QtDirectory::listFilesRecursive(std::vector<String>& file_list) const
{
file_list.clear();
listFilesRecursive_internal(file_list);
}
//-----------------------------------------------------------------------------
ref<QtDirectory> QtDirectory::qtSubDir(const String& subdir_name) const
{
if (path().empty())
{
Log::error( "QtDirectory::path() must not be empty!\n" );
return NULL;
}
std::vector<String> dir_list;
String p = translatePath(subdir_name).right(-path().length()-1);
this->listSubDirs(dir_list);
String cur_p = path();
for(int i=0; i<(int)dir_list.size(); ++i)
{
dir_list[i] = dir_list[i].right(-cur_p.length()-1);
if (p.startsWith(dir_list[i]+'/') || p == dir_list[i])
{
ref<QtDirectory> dir = new QtDirectory(cur_p + '/' + dir_list[i]);
if (!dir)
return NULL;
cur_p = cur_p + '/' + dir_list[i];
p = p.right(-dir_list[i].length()-1);
dir->listSubDirs(dir_list);
i=-1;
if (p.empty())
return dir;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
bool QtDirectory::exists() const
{
if ( path().empty() )
{
Log::error( "QtDirectory::path() must not be empty!\n" );
return false;
}
return QDir( QString( path().toStdString().c_str() ) ).exists();
}
//-----------------------------------------------------------------------------
void QtDirectory::listSubDirs(std::vector<String>& dirs_out, bool append) const
{
if ( ! append ) {
dirs_out.clear();
}
if ( path().empty() )
{
Log::error( "QtDirectory::path() must not be empty!\n" );
return;
}
const char* p = path().toStdString().c_str();
QStringList subdirs = QDir(QString(p)).entryList( QDir::Filter::Dirs | QDir::NoDotAndDotDot );
for (int i = 0; i < subdirs.size(); ++i) {
vl::String name = vl::String::fromStdString( subdirs.at(i).toStdString() );
dirs_out.push_back( path() + name + '/' );
}
}
//-----------------------------------------------------------------------------
void QtDirectory::listFiles(std::vector< ref<QtFile> >& file_list, bool append) const
{
if (!append)
file_list.clear();
std::vector<String> file_names;
listFiles(file_names,false);
for(unsigned i=0; i<file_names.size(); ++i)
{
ref<QtFile> file = new QtFile;
// file->setPath( path() + file_names[i] );
file->setPath( file_names[i] );
file_list.push_back(file);
}
}
//-----------------------------------------------------------------------------
void QtDirectory::listFiles(std::vector<String>& files_out, bool append) const
{
if ( ! append ) {
files_out.clear();
}
if ( path().empty() )
{
Log::error( "QtDirectory::path() must not be empty!\n" );
return;
}
const char* p = path().toStdString().c_str();
QStringList files = QDir(QString(p)).entryList( QDir::Filter::Files );
for (int i = 0; i < files.size(); ++i) {
vl::String name = vl::String::fromStdString( files.at(i).toStdString() );
files_out.push_back( path() + name + '/' );
}
}
//-----------------------------------------------------------------------------
ref<VirtualFile> QtDirectory::file(const String& name) const
{
return qtFile(name);
}
//-----------------------------------------------------------------------------
ref<QtFile> QtDirectory::qtFile(const String& name) const
{
String p = translatePath(name);
ref<QtFile> file = new QtFile(p);
if (file->exists())
return file;
else
return NULL;
}
//-----------------------------------------------------------------------------
void QtDirectory::listFilesRecursive_internal(std::vector<String>& file_list) const
{
// add local child
listFiles(file_list, true);
// descend recursively
std::vector<String> dir_list;
listSubDirs(dir_list);
for(unsigned i=0; i<dir_list.size(); ++i)
{
VL_CHECK(dir_list[i] != ".")
VL_CHECK(dir_list[i] != "..")
QtDirectory sub_dir( dir_list[i] );
sub_dir.listFilesRecursive_internal(file_list);
}
}
//-----------------------------------------------------------------------------
/**************************************************************************************/
/* */
/* Visualization Library */
/* http://visualizationlibrary.org */
/* */
/* Copyright (c) 2005-2016, Michele Bosi */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without modification, */
/* are permitted provided that the following conditions are met: */
/* */
/* - Redistributions of source code must retain the above copyright notice, this */
/* list of conditions and the following disclaimer. */
/* */
/* - Redistributions in binary form must reproduce the above copyright notice, this */
/* list of conditions and the following disclaimer in the documentation and/or */
/* other materials provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
/* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
/* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
/* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/**************************************************************************************/
#ifndef QtDirectory_INCLUDE_ONCE
#define QtDirectory_INCLUDE_ONCE
#include <vlQt4/link_config.hpp>
#include <vlCore/VirtualDirectory.hpp>
namespace vl
{
class QtFile;
//---------------------------------------------------------------------------
// QtDirectory
//---------------------------------------------------------------------------
/**
* A VirtualDirectory that uses Qt's QDir.
*
* \sa
* - MemoryDirectory
* - DiskDirectory
* - ZippedDirectory
* - FileSystem
* - VirtualFile
* - DiskFile
* - QtFile
* - MemoryFile
* - ZippedFile
*/
class VLQT4_EXPORT QtDirectory: public VirtualDirectory
{
VL_INSTRUMENT_CLASS(vl::QtDirectory, VirtualDirectory)
public:
QtDirectory();
QtDirectory( const String& path );
//! Use carefully this function, since this search the whole given file system tree.
void listFilesRecursive(std::vector<String>& file_list) const;
void listFiles(std::vector<String>& file_list, bool append=false) const;
void listFiles(std::vector< ref<QtFile> >& file_list, bool append=false) const;
void listSubDirs(std::vector<String>& dirs, bool append=false) const;
ref<QtDirectory> qtSubDir(const String& subdir_name) const;
ref<VirtualDirectory> subDir(const String& subdir_name) const { return qtSubDir(subdir_name); }
virtual ref<VirtualFile> file(const String& name) const;
virtual ref<QtFile> qtFile(const String& name) const;
bool exists() const;
protected:
void listFilesRecursive_internal(std::vector<String>& file_list) const;
};
}
#endif
/**************************************************************************************/
/* */
/* Visualization Library */
/* http://visualizationlibrary.org */
/* */
/* Copyright (c) 2005-2016, Michele Bosi */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without modification, */
/* are permitted provided that the following conditions are met: */
/* */
/* - Redistributions of source code must retain the above copyright notice, this */
/* list of conditions and the following disclaimer. */
/* */
/* - Redistributions in binary form must reproduce the above copyright notice, this */
/* list of conditions and the following disclaimer in the documentation and/or */
/* other materials provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
/* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
/* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
/* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/**************************************************************************************/
#include <vlQt4/QtFile.hpp>
using namespace vl;
//-----------------------------------------------------------------------------
// QtFile
//-----------------------------------------------------------------------------
QtFile::QtFile(const String& path)
{
setPath(path);
}
//-----------------------------------------------------------------------------
QtFile::~QtFile()
{
close();
}
//-----------------------------------------------------------------------------
bool QtFile::open(const String& path, EOpenMode mode)
{
setPath(path);
return open(mode);
}
//-----------------------------------------------------------------------------
bool QtFile::open(EOpenMode mode)
{
if ( isOpen() )
{
Log::error("QtFile::open(): file already open.\n");
return false;
}
QIODevice::OpenMode qmode;
if (mode == OM_ReadOnly) {
qmode = QIODevice::ReadOnly;
} else {
qmode = QIODevice::WriteOnly;
}
mQFile.setFileName( path().toStdString().c_str() );
if ( ! mQFile.open( qmode ) )
{
Log::error( Say("QtFile::open(): error opening file '%s'\n") << path() );
return false;
}
return true;
}
//-----------------------------------------------------------------------------
bool QtFile::isOpen() const
{
return mQFile.isOpen();
}
//-----------------------------------------------------------------------------
void QtFile::close()
{
mQFile.close();
}
//-----------------------------------------------------------------------------
long long QtFile::size() const
{
return QFile( path().toStdString().c_str() ).size();
}
//-----------------------------------------------------------------------------
bool QtFile::exists() const
{
return !path().empty() && QFile::exists( path().toStdString().c_str() );
}
//-----------------------------------------------------------------------------
long long QtFile::read_Implementation(void* buffer, long long byte_count)
{
if ( ! mQFile.isOpen() )
{
Log::error("QtFile::read_Implementation() called on closed file!\n");
return 0;
}
return mQFile.read( (char*)buffer, byte_count );
}
//-----------------------------------------------------------------------------
long long QtFile::write_Implementation(const void* buffer, long long byte_count)
{
if ( ! mQFile.isOpen() )
{
Log::error("QtFile::write_Implementation() called on closed file!\n");
return 0;
}
return mQFile.write( (char*)buffer, byte_count );
}
//-----------------------------------------------------------------------------
long long QtFile::position_Implementation() const
{
if ( ! mQFile.isOpen() )
{
Log::error("QtFile::position_Implementation() called on closed file!\n");
return -1;
}
return mQFile.pos();
}
//-----------------------------------------------------------------------------
bool QtFile::seekSet_Implementation(long long offset)
{
if ( ! mQFile.isOpen() )
{
Log::error("QtFile::seekSet_Implementation() called on closed file!\n");
return false;
}
mQFile.seek(offset);
return true;
}
//-----------------------------------------------------------------------------
ref<VirtualFile> QtFile::clone() const
{
ref<QtFile> file = new QtFile;
file->operator=(*this);
return file;
}
//-----------------------------------------------------------------------------
/**************************************************************************************/
/* */
/* Visualization Library */
/* http://visualizationlibrary.org */
/* */
/* Copyright (c) 2005-2016, Michele Bosi */
/* All rights reserved. */
/* */
/* Redistribution and use in source and binary forms, with or without modification, */
/* are permitted provided that the following conditions are met: */
/* */
/* - Redistributions of source code must retain the above copyright notice, this */
/* list of conditions and the following disclaimer. */
/* */
/* - Redistributions in binary form must reproduce the above copyright notice, this */
/* list of conditions and the following disclaimer in the documentation and/or */
/* other materials provided with the distribution. */
/* */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
/* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
/* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
/* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
/* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
/* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* */
/**************************************************************************************/
#ifndef QtFile_INCLUDE_ONCE
#define QtFile_INCLUDE_ONCE
#include <vlQt4/link_config.hpp>
#include <vlCore/VirtualFile.hpp>
#include <vlQt4/QtDirectory.hpp>
#include <QFile>
namespace vl
{
class QtDirectory;
//---------------------------------------------------------------------------
// QtFile
//---------------------------------------------------------------------------
/**
* A VirtualFile that uses Qt's QFile.
*
* \sa
* - VirtualDirectory
* - DiskDirectory
* - QtDirectory
* - MemoryDirectory
* - ZippedDirectory
* - FileSystem
* - VirtualFile
* - MemoryFile
* - DiskFile
* - ZippedFile
*/
class VLQT4_EXPORT QtFile: public VirtualFile
{
VL_INSTRUMENT_CLASS(vl::QtFile, VirtualFile)
friend class QtDirectory;
protected:
QtFile(const QtFile& other): VirtualFile(other) {}
public:
QtFile(const String& path = String());
~QtFile();
//! The specified path is relative to the parent directory. See setPhysicalPath().
bool open(const String& path, EOpenMode mode);
virtual bool open(EOpenMode mode);
virtual bool isOpen() const;
virtual void close();
//! Returns the file size in bytes or -1 on error.
virtual long long size() const;
virtual bool exists() const;
QtFile& operator=(const QtFile& other) { close(); super::operator=(other); return *this; }
virtual ref<VirtualFile> clone() const;
protected:
virtual long long read_Implementation(void* buffer, long long byte_count);
virtual long long write_Implementation(const void* buffer, long long byte_count);
virtual long long position_Implementation() const;
virtual bool seekSet_Implementation(long long offset);
protected:
QFile mQFile;
protected:
};
}
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment