mirror of https://github.com/vitalif/phantomjs
A bit more compliancy with the CommonJS Filesystem/A draft specs.
* Renamed some methods to be more compliant with the specs * Added some Directory/Tree specific ops * Support for the open mode '+' (that is equivalent to 'a') * See draft at: http://wiki.commonjs.org/wiki/Filesystem/A1.3
parent
6b04ee23c1
commit
f4aa689ec7
|
@ -126,7 +126,7 @@ bool FileSystem::exists(const QString &path) const
|
|||
return QFile::exists(path);
|
||||
}
|
||||
|
||||
bool FileSystem::isDir(const QString &path) const
|
||||
bool FileSystem::isDirectory(const QString &path) const
|
||||
{
|
||||
return QFileInfo(path).isDir();
|
||||
}
|
||||
|
@ -136,21 +136,64 @@ bool FileSystem::isFile(const QString &path) const
|
|||
return QFileInfo(path).isFile();
|
||||
}
|
||||
|
||||
bool FileSystem::mkDir(const QString &path) const
|
||||
bool FileSystem::makeDirectory(const QString &path) const
|
||||
{
|
||||
return QDir().mkdir(path);
|
||||
}
|
||||
|
||||
bool FileSystem::makeTree(const QString &path) const
|
||||
{
|
||||
return QDir().mkpath(path);
|
||||
}
|
||||
|
||||
bool FileSystem::remove(const QString &path) const
|
||||
{
|
||||
return QFile::remove(path);
|
||||
}
|
||||
|
||||
bool FileSystem::removeDirectory(const QString &path) const
|
||||
{
|
||||
return QDir().rmdir(path);
|
||||
}
|
||||
|
||||
bool FileSystem::removeTree(const QString &path) const
|
||||
{
|
||||
QDir dir(path);
|
||||
bool res = false;
|
||||
|
||||
if (dir.exists()) {
|
||||
foreach(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
|
||||
if (info.isDir()) {
|
||||
res = removeTree(info.absoluteFilePath());
|
||||
} else {
|
||||
res = remove(info.absoluteFilePath());
|
||||
}
|
||||
|
||||
if (!res) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
res = removeDirectory(path);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
QStringList FileSystem::list(const QString &path) const
|
||||
{
|
||||
return QDir(path).entryList();
|
||||
}
|
||||
|
||||
QString FileSystem::workDir() const
|
||||
QString FileSystem::workingDirectory() const
|
||||
{
|
||||
return QDir::currentPath();
|
||||
}
|
||||
|
||||
bool FileSystem::changeWorkingDirectory(const QString &path) const
|
||||
{
|
||||
return QDir::setCurrent(path);
|
||||
}
|
||||
|
||||
QString FileSystem::separator() const
|
||||
{
|
||||
return QDir::separator();
|
||||
|
@ -161,44 +204,41 @@ QObject *FileSystem::open(const QString &path, const QString &mode) const
|
|||
File *f = NULL;
|
||||
QFile *_f = new QFile(path);
|
||||
QFile::OpenMode modeCode = QFile::NotOpen;
|
||||
QChar modeAsChar = 0;
|
||||
|
||||
// Ensure only one "mode character" has been selected
|
||||
if ( mode.length() != 1) {
|
||||
qDebug() << "FileSystem::open - " << "Wrong Mode string length:" << mode;
|
||||
return false;
|
||||
}
|
||||
// Read out the mode
|
||||
modeAsChar = mode[0];
|
||||
|
||||
// Determine the OpenMode
|
||||
switch(modeAsChar.toAscii()) {
|
||||
case 'r': case 'R': {
|
||||
modeCode |= QFile::ReadOnly;
|
||||
// Make sure there is something to read, otherwise return "false"
|
||||
if ( !_f->exists() ) {
|
||||
qDebug() << "FileSystem::open - " << "Trying to read a file that doesn't exist:" << path;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'a': case 'A': {
|
||||
modeCode |= QFile::Append;
|
||||
// NOTE: no "break" here! This case will also execute the code for case 'w'.
|
||||
}
|
||||
case 'w': case 'W': {
|
||||
modeCode |= QFile::WriteOnly;
|
||||
// If the file doesn't exist and the desired path can't be created, return "false"
|
||||
if ( !_f->exists() && !mkDir(QFileInfo(path).dir().absolutePath()) ) {
|
||||
qDebug() << "FileSystem::open - " << "Full path coulnd't be created:" << path;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
qDebug() << "FileSystem::open - " << "Wrong Mode:" << mode;
|
||||
switch(mode[0].toAscii()) {
|
||||
case 'r': case 'R': {
|
||||
modeCode |= QFile::ReadOnly;
|
||||
// Make sure there is something to read, otherwise return "false"
|
||||
if ( !_f->exists() ) {
|
||||
qDebug() << "FileSystem::open - " << "Trying to read a file that doesn't exist:" << path;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'a': case 'A': case '+': {
|
||||
modeCode |= QFile::Append;
|
||||
// NOTE: no "break" here! This case will also execute the code for case 'w'.
|
||||
}
|
||||
case 'w': case 'W': {
|
||||
modeCode |= QFile::WriteOnly;
|
||||
// If the file doesn't exist and the desired path can't be created, return "false"
|
||||
if ( !_f->exists() && !makeTree(QFileInfo(path).dir().absolutePath()) ) {
|
||||
qDebug() << "FileSystem::open - " << "Full path coulnd't be created:" << path;
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
qDebug() << "FileSystem::open - " << "Wrong Mode:" << mode;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to Open
|
||||
|
@ -211,8 +251,3 @@ QObject *FileSystem::open(const QString &path, const QString &mode) const
|
|||
qDebug() << "FileSystem::open - " << "Couldn't be opened:" << path;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileSystem::remove(const QString &path) const
|
||||
{
|
||||
return QFile::remove(path);
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ private:
|
|||
class FileSystem : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString workDir READ workDir)
|
||||
Q_PROPERTY(QString workingDirectory READ workingDirectory)
|
||||
Q_PROPERTY(QString separator READ separator)
|
||||
|
||||
public:
|
||||
|
@ -71,14 +71,24 @@ public:
|
|||
|
||||
public slots:
|
||||
bool exists(const QString &path) const;
|
||||
bool isDir(const QString &path) const;
|
||||
bool isDirectory(const QString &path) const;
|
||||
bool isFile(const QString &path) const;
|
||||
bool mkDir(const QString &path) const;
|
||||
QStringList list(const QString &path) const;
|
||||
QString workDir() const;
|
||||
QString separator() const;
|
||||
QObject *open(const QString &path, const QString &mode) const;
|
||||
|
||||
bool makeDirectory(const QString &path) const;
|
||||
bool makeTree(const QString &path) const;
|
||||
|
||||
bool remove(const QString &path) const;
|
||||
bool removeDirectory(const QString &path) const;
|
||||
bool removeTree(const QString &path) const;
|
||||
|
||||
QStringList list(const QString &path) const;
|
||||
|
||||
QString workingDirectory() const;
|
||||
bool changeWorkingDirectory(const QString &path) const;
|
||||
|
||||
QString separator() const;
|
||||
|
||||
QObject *open(const QString &path, const QString &mode) const;
|
||||
};
|
||||
|
||||
#endif // FILESYSTEM_H
|
||||
|
|
Loading…
Reference in New Issue