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);
|
return QFile::exists(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileSystem::isDir(const QString &path) const
|
bool FileSystem::isDirectory(const QString &path) const
|
||||||
{
|
{
|
||||||
return QFileInfo(path).isDir();
|
return QFileInfo(path).isDir();
|
||||||
}
|
}
|
||||||
|
@ -136,21 +136,64 @@ bool FileSystem::isFile(const QString &path) const
|
||||||
return QFileInfo(path).isFile();
|
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);
|
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
|
QStringList FileSystem::list(const QString &path) const
|
||||||
{
|
{
|
||||||
return QDir(path).entryList();
|
return QDir(path).entryList();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString FileSystem::workDir() const
|
QString FileSystem::workingDirectory() const
|
||||||
{
|
{
|
||||||
return QDir::currentPath();
|
return QDir::currentPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FileSystem::changeWorkingDirectory(const QString &path) const
|
||||||
|
{
|
||||||
|
return QDir::setCurrent(path);
|
||||||
|
}
|
||||||
|
|
||||||
QString FileSystem::separator() const
|
QString FileSystem::separator() const
|
||||||
{
|
{
|
||||||
return QDir::separator();
|
return QDir::separator();
|
||||||
|
@ -161,44 +204,41 @@ QObject *FileSystem::open(const QString &path, const QString &mode) const
|
||||||
File *f = NULL;
|
File *f = NULL;
|
||||||
QFile *_f = new QFile(path);
|
QFile *_f = new QFile(path);
|
||||||
QFile::OpenMode modeCode = QFile::NotOpen;
|
QFile::OpenMode modeCode = QFile::NotOpen;
|
||||||
QChar modeAsChar = 0;
|
|
||||||
|
|
||||||
// Ensure only one "mode character" has been selected
|
// Ensure only one "mode character" has been selected
|
||||||
if ( mode.length() != 1) {
|
if ( mode.length() != 1) {
|
||||||
qDebug() << "FileSystem::open - " << "Wrong Mode string length:" << mode;
|
qDebug() << "FileSystem::open - " << "Wrong Mode string length:" << mode;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Read out the mode
|
|
||||||
modeAsChar = mode[0];
|
|
||||||
|
|
||||||
// Determine the OpenMode
|
// Determine the OpenMode
|
||||||
switch(modeAsChar.toAscii()) {
|
switch(mode[0].toAscii()) {
|
||||||
case 'r': case 'R': {
|
case 'r': case 'R': {
|
||||||
modeCode |= QFile::ReadOnly;
|
modeCode |= QFile::ReadOnly;
|
||||||
// Make sure there is something to read, otherwise return "false"
|
// Make sure there is something to read, otherwise return "false"
|
||||||
if ( !_f->exists() ) {
|
if ( !_f->exists() ) {
|
||||||
qDebug() << "FileSystem::open - " << "Trying to read a file that doesn't exist:" << path;
|
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;
|
|
||||||
return false;
|
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
|
// 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;
|
qDebug() << "FileSystem::open - " << "Couldn't be opened:" << path;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FileSystem::remove(const QString &path) const
|
|
||||||
{
|
|
||||||
return QFile::remove(path);
|
|
||||||
}
|
|
||||||
|
|
|
@ -63,7 +63,7 @@ private:
|
||||||
class FileSystem : public QObject
|
class FileSystem : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(QString workDir READ workDir)
|
Q_PROPERTY(QString workingDirectory READ workingDirectory)
|
||||||
Q_PROPERTY(QString separator READ separator)
|
Q_PROPERTY(QString separator READ separator)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -71,14 +71,24 @@ public:
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
bool exists(const QString &path) const;
|
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 isFile(const QString &path) const;
|
||||||
bool mkDir(const QString &path) const;
|
|
||||||
QStringList list(const QString &path) const;
|
bool makeDirectory(const QString &path) const;
|
||||||
QString workDir() const;
|
bool makeTree(const QString &path) const;
|
||||||
QString separator() const;
|
|
||||||
QObject *open(const QString &path, const QString &mode) const;
|
|
||||||
bool remove(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
|
#endif // FILESYSTEM_H
|
||||||
|
|
Loading…
Reference in New Issue