2011-06-14 14:28:51 +04:00
|
|
|
/*
|
|
|
|
This file is part of the PhantomJS project from Ofi Labs.
|
|
|
|
|
2011-08-31 19:29:40 +04:00
|
|
|
Copyright (C) 2011 Ivan De Marino <ivan.de.marino@gmail.com>
|
2011-06-14 14:28:51 +04:00
|
|
|
|
|
|
|
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.
|
|
|
|
* Neither the name of the <organization> nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
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 <COPYRIGHT HOLDER> 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 FILESYSTEM_H
|
|
|
|
#define FILESYSTEM_H
|
|
|
|
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QFile>
|
2012-02-02 19:29:00 +04:00
|
|
|
#include <QTextCodec>
|
2011-06-14 14:28:51 +04:00
|
|
|
#include <QTextStream>
|
2011-07-22 03:29:25 +04:00
|
|
|
#include <QVariant>
|
2011-06-14 14:28:51 +04:00
|
|
|
|
2012-12-29 20:23:10 +04:00
|
|
|
class File : public QObject
|
2011-06-14 14:28:51 +04:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
Filesystem module should allow reading and writing binary files.
CommonJS proposal: http://wiki.commonjs.org/wiki/Filesystem/A.
It's called "raw".
http://code.google.com/p/phantomjs/issues/detail?id=400
Squashed commit of the following:
commit dd5fab4778bb7b67f1eca26a07d430aadd458c6e
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Thu Feb 23 16:19:21 2012 +0100
the "mode" string is now properly parsed, and not only the first
char evaluated. This allows us to do fancy things like
fs.open(file, "rw+"); // read/write/append
Furthermore .read() is adapted such that it will always return the
full file contents, no matter where we have seeked to before (i.e.
by passing + we seek to the end, hence read() would always return
an empty string).
To open a binary file, pass "b" in the mode string to fs.open, e.g.:
fs.open(file, "rb"); // read binary
fs.open(file, "wb"); // write binary
fs.open(file, "rwb+"); // read/write binary, append
alternatively, one can use these shortcuts:
fs.write(file, contents, "b"); // write binary
fs.read(file, "b"); // read binary
Unit tests are extended and the echoToFile.js example fixed (it did not
close the file, which lead to the contents never getting written
on-disk since flush() is never called).
Also note that the FileSystem::open method was cleaned up and at least
one memory leak (if QFile* could not open) was fixed. The code should
now also be more C++-like.
commit 41139951138491459accefab22d48eba7b0b9900
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Wed Feb 15 16:39:23 2012 +0100
use QString instead of QByteArray for raw binary data
QByteArray is simply unusable in JavaScript, since functions like
e.g. window.btoa expect a string. Also there is no sane way to
create a byte array in javascript, as ArrayBuffer e.g. is not
supported by QScript (at least there is no conversion in place).
If we use QString and some custom read/write code this all works
as expected though, we can use window.btoa to base64 encode binary
data and we can create random binary data using String.fromCharCode
also adds a unit test
commit e45673486ef27daf916902153217f9e5001b68c9
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Wed Feb 15 14:39:15 2012 +0100
make it possible to read/write raw/binary files
this adds File::readRaw and File::writeRaw functions,
as well as 'shimmed' versions FS::readRaw and FS::writeRaw
these functions directly use QFile and QByteArray instead of
QTextStream and QString, making it possible to read and write
binary data, e.g. images and such.
2012-02-29 19:53:12 +04:00
|
|
|
// handle a textfile with given codec
|
|
|
|
// if @p codec is null, the file is considered to be binary
|
2012-02-02 19:29:00 +04:00
|
|
|
File(QFile *openfile, QTextCodec *codec, QObject *parent = 0);
|
2011-06-14 14:28:51 +04:00
|
|
|
virtual ~File();
|
|
|
|
|
|
|
|
public slots:
|
2012-12-18 10:23:26 +04:00
|
|
|
/**
|
|
|
|
* @param n Number of bytes to read (a negative value means read up to EOF)
|
|
|
|
* NOTE: The use of QVariant here is necessary to catch JavaScript `null`.
|
|
|
|
* @see <a href="http://wiki.commonjs.org/wiki/IO/A#Instance_Methods">IO/A spec</a>
|
|
|
|
*/
|
|
|
|
QString read(const QVariant &n = -1);
|
2011-06-14 14:28:51 +04:00
|
|
|
bool write(const QString &data);
|
|
|
|
|
2012-12-18 04:48:09 +04:00
|
|
|
bool seek(const qint64 pos);
|
|
|
|
|
2011-06-14 14:28:51 +04:00
|
|
|
QString readLine();
|
|
|
|
bool writeLine(const QString &data);
|
|
|
|
|
|
|
|
bool atEnd() const;
|
|
|
|
void flush();
|
|
|
|
void close();
|
|
|
|
|
2013-03-26 16:22:53 +04:00
|
|
|
QString getEncoding() const;
|
|
|
|
bool setEncoding(const QString &encoding);
|
|
|
|
|
2011-06-14 14:28:51 +04:00
|
|
|
private:
|
2013-03-26 08:05:25 +04:00
|
|
|
bool _isUnbuffered() const;
|
|
|
|
|
2011-06-14 14:28:51 +04:00
|
|
|
QFile *m_file;
|
Filesystem module should allow reading and writing binary files.
CommonJS proposal: http://wiki.commonjs.org/wiki/Filesystem/A.
It's called "raw".
http://code.google.com/p/phantomjs/issues/detail?id=400
Squashed commit of the following:
commit dd5fab4778bb7b67f1eca26a07d430aadd458c6e
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Thu Feb 23 16:19:21 2012 +0100
the "mode" string is now properly parsed, and not only the first
char evaluated. This allows us to do fancy things like
fs.open(file, "rw+"); // read/write/append
Furthermore .read() is adapted such that it will always return the
full file contents, no matter where we have seeked to before (i.e.
by passing + we seek to the end, hence read() would always return
an empty string).
To open a binary file, pass "b" in the mode string to fs.open, e.g.:
fs.open(file, "rb"); // read binary
fs.open(file, "wb"); // write binary
fs.open(file, "rwb+"); // read/write binary, append
alternatively, one can use these shortcuts:
fs.write(file, contents, "b"); // write binary
fs.read(file, "b"); // read binary
Unit tests are extended and the echoToFile.js example fixed (it did not
close the file, which lead to the contents never getting written
on-disk since flush() is never called).
Also note that the FileSystem::open method was cleaned up and at least
one memory leak (if QFile* could not open) was fixed. The code should
now also be more C++-like.
commit 41139951138491459accefab22d48eba7b0b9900
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Wed Feb 15 16:39:23 2012 +0100
use QString instead of QByteArray for raw binary data
QByteArray is simply unusable in JavaScript, since functions like
e.g. window.btoa expect a string. Also there is no sane way to
create a byte array in javascript, as ArrayBuffer e.g. is not
supported by QScript (at least there is no conversion in place).
If we use QString and some custom read/write code this all works
as expected though, we can use window.btoa to base64 encode binary
data and we can create random binary data using String.fromCharCode
also adds a unit test
commit e45673486ef27daf916902153217f9e5001b68c9
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Wed Feb 15 14:39:15 2012 +0100
make it possible to read/write raw/binary files
this adds File::readRaw and File::writeRaw functions,
as well as 'shimmed' versions FS::readRaw and FS::writeRaw
these functions directly use QFile and QByteArray instead of
QTextStream and QString, making it possible to read and write
binary data, e.g. images and such.
2012-02-29 19:53:12 +04:00
|
|
|
QTextStream *m_fileStream;
|
2011-06-14 14:28:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-12-29 20:23:10 +04:00
|
|
|
class FileSystem : public QObject
|
2011-06-14 14:28:51 +04:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2011-06-28 02:12:46 +04:00
|
|
|
Q_PROPERTY(QString workingDirectory READ workingDirectory)
|
2011-06-14 14:28:51 +04:00
|
|
|
Q_PROPERTY(QString separator READ separator)
|
|
|
|
|
|
|
|
public:
|
|
|
|
FileSystem(QObject *parent = 0);
|
|
|
|
|
|
|
|
public slots:
|
2011-07-05 02:35:54 +04:00
|
|
|
// Attributes
|
2011-09-05 00:26:11 +04:00
|
|
|
// 'size(path)' implemented in "filesystem-shim.js" using '_size(path)'
|
2011-07-22 03:29:25 +04:00
|
|
|
int _size(const QString &path) const;
|
|
|
|
QVariant lastModified(const QString &path) const;
|
2011-07-18 02:13:29 +04:00
|
|
|
|
|
|
|
// Directory
|
2011-09-05 02:42:54 +04:00
|
|
|
// 'copyTree(source, destination)' implemented in "filesystem-shim.js" using '_copyTree(source, destination)'
|
|
|
|
bool _copyTree(const QString &source, const QString &destination) const;
|
2011-06-28 02:12:46 +04:00
|
|
|
bool makeDirectory(const QString &path) const;
|
|
|
|
bool makeTree(const QString &path) const;
|
2011-09-05 00:26:11 +04:00
|
|
|
// 'removeDirectory(path)' implemented in "filesystem-shim.js" using '_removeDirectory(path)'
|
2011-09-01 02:32:56 +04:00
|
|
|
bool _removeDirectory(const QString &path) const;
|
2011-09-05 00:26:11 +04:00
|
|
|
// 'removeTree(path)' implemented in "filesystem-shim.js" using '_removeTree(path)'
|
2011-09-01 02:32:56 +04:00
|
|
|
bool _removeTree(const QString &path) const;
|
2011-07-18 02:13:29 +04:00
|
|
|
|
|
|
|
// Files
|
2012-02-02 18:48:35 +04:00
|
|
|
// 'open(path, mode|options)' implemented in "filesystem-shim.js" using '_open(path, opts)'
|
|
|
|
QObject *_open(const QString &path, const QVariantMap &opts) const;
|
|
|
|
// 'read(path, options)' implemented in "filesystem-shim.js"
|
Filesystem module should allow reading and writing binary files.
CommonJS proposal: http://wiki.commonjs.org/wiki/Filesystem/A.
It's called "raw".
http://code.google.com/p/phantomjs/issues/detail?id=400
Squashed commit of the following:
commit dd5fab4778bb7b67f1eca26a07d430aadd458c6e
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Thu Feb 23 16:19:21 2012 +0100
the "mode" string is now properly parsed, and not only the first
char evaluated. This allows us to do fancy things like
fs.open(file, "rw+"); // read/write/append
Furthermore .read() is adapted such that it will always return the
full file contents, no matter where we have seeked to before (i.e.
by passing + we seek to the end, hence read() would always return
an empty string).
To open a binary file, pass "b" in the mode string to fs.open, e.g.:
fs.open(file, "rb"); // read binary
fs.open(file, "wb"); // write binary
fs.open(file, "rwb+"); // read/write binary, append
alternatively, one can use these shortcuts:
fs.write(file, contents, "b"); // write binary
fs.read(file, "b"); // read binary
Unit tests are extended and the echoToFile.js example fixed (it did not
close the file, which lead to the contents never getting written
on-disk since flush() is never called).
Also note that the FileSystem::open method was cleaned up and at least
one memory leak (if QFile* could not open) was fixed. The code should
now also be more C++-like.
commit 41139951138491459accefab22d48eba7b0b9900
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Wed Feb 15 16:39:23 2012 +0100
use QString instead of QByteArray for raw binary data
QByteArray is simply unusable in JavaScript, since functions like
e.g. window.btoa expect a string. Also there is no sane way to
create a byte array in javascript, as ArrayBuffer e.g. is not
supported by QScript (at least there is no conversion in place).
If we use QString and some custom read/write code this all works
as expected though, we can use window.btoa to base64 encode binary
data and we can create random binary data using String.fromCharCode
also adds a unit test
commit e45673486ef27daf916902153217f9e5001b68c9
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Wed Feb 15 14:39:15 2012 +0100
make it possible to read/write raw/binary files
this adds File::readRaw and File::writeRaw functions,
as well as 'shimmed' versions FS::readRaw and FS::writeRaw
these functions directly use QFile and QByteArray instead of
QTextStream and QString, making it possible to read and write
binary data, e.g. images and such.
2012-02-29 19:53:12 +04:00
|
|
|
// 'readRaw(path, options)' implemented in "filesystem-shim.js"
|
2012-02-02 18:48:35 +04:00
|
|
|
// 'write(path, mode|options)' implemented in the "filesystem-shim.js"
|
Filesystem module should allow reading and writing binary files.
CommonJS proposal: http://wiki.commonjs.org/wiki/Filesystem/A.
It's called "raw".
http://code.google.com/p/phantomjs/issues/detail?id=400
Squashed commit of the following:
commit dd5fab4778bb7b67f1eca26a07d430aadd458c6e
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Thu Feb 23 16:19:21 2012 +0100
the "mode" string is now properly parsed, and not only the first
char evaluated. This allows us to do fancy things like
fs.open(file, "rw+"); // read/write/append
Furthermore .read() is adapted such that it will always return the
full file contents, no matter where we have seeked to before (i.e.
by passing + we seek to the end, hence read() would always return
an empty string).
To open a binary file, pass "b" in the mode string to fs.open, e.g.:
fs.open(file, "rb"); // read binary
fs.open(file, "wb"); // write binary
fs.open(file, "rwb+"); // read/write binary, append
alternatively, one can use these shortcuts:
fs.write(file, contents, "b"); // write binary
fs.read(file, "b"); // read binary
Unit tests are extended and the echoToFile.js example fixed (it did not
close the file, which lead to the contents never getting written
on-disk since flush() is never called).
Also note that the FileSystem::open method was cleaned up and at least
one memory leak (if QFile* could not open) was fixed. The code should
now also be more C++-like.
commit 41139951138491459accefab22d48eba7b0b9900
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Wed Feb 15 16:39:23 2012 +0100
use QString instead of QByteArray for raw binary data
QByteArray is simply unusable in JavaScript, since functions like
e.g. window.btoa expect a string. Also there is no sane way to
create a byte array in javascript, as ArrayBuffer e.g. is not
supported by QScript (at least there is no conversion in place).
If we use QString and some custom read/write code this all works
as expected though, we can use window.btoa to base64 encode binary
data and we can create random binary data using String.fromCharCode
also adds a unit test
commit e45673486ef27daf916902153217f9e5001b68c9
Author: Milian Wolff <milian.wolff@kdab.com>
Date: Wed Feb 15 14:39:15 2012 +0100
make it possible to read/write raw/binary files
this adds File::readRaw and File::writeRaw functions,
as well as 'shimmed' versions FS::readRaw and FS::writeRaw
these functions directly use QFile and QByteArray instead of
QTextStream and QString, making it possible to read and write
binary data, e.g. images and such.
2012-02-29 19:53:12 +04:00
|
|
|
// 'writeRaw(path, mode|options)' implemented in the "filesystem-shim.js"
|
2011-09-05 00:26:11 +04:00
|
|
|
// 'remove(path)' implemented in "filesystem-shim.js" using '_remove(path)'
|
2011-09-01 02:32:56 +04:00
|
|
|
bool _remove(const QString &path) const;
|
2011-09-05 00:26:11 +04:00
|
|
|
// 'copy(source, destination)' implemented in "filesystem-shim.js" using '_copy(source, destination)'
|
2011-09-01 03:01:40 +04:00
|
|
|
bool _copy(const QString &source, const QString &destination) const;
|
2011-09-05 00:26:11 +04:00
|
|
|
// 'move(source, destination)' implemented in "filesystem-shim.js"
|
|
|
|
// 'touch(path)' implemented in "filesystem-shim.js"
|
2011-06-28 02:12:46 +04:00
|
|
|
|
2011-07-05 02:35:54 +04:00
|
|
|
// Listing
|
2011-06-14 14:28:51 +04:00
|
|
|
QStringList list(const QString &path) const;
|
2011-06-28 02:12:46 +04:00
|
|
|
|
2011-07-05 02:35:54 +04:00
|
|
|
// Paths
|
2011-07-18 02:13:29 +04:00
|
|
|
QString separator() const;
|
2011-06-28 02:12:46 +04:00
|
|
|
QString workingDirectory() const;
|
|
|
|
bool changeWorkingDirectory(const QString &path) const;
|
2011-08-02 03:47:43 +04:00
|
|
|
QString absolute(const QString &relativePath) const;
|
2012-12-21 16:10:30 +04:00
|
|
|
// 'join(...)' implemented in "fs.js"
|
|
|
|
// 'split(path)' implemented in "fs.js"
|
|
|
|
QString fromNativeSeparators(const QString &path) const;
|
|
|
|
QString toNativeSeparators(const QString &path) const;
|
2011-06-28 02:12:46 +04:00
|
|
|
|
2012-03-17 07:43:26 +04:00
|
|
|
// Links
|
|
|
|
QString readLink(const QString &path) const;
|
|
|
|
|
2011-07-18 02:13:29 +04:00
|
|
|
// Tests
|
|
|
|
bool exists(const QString &path) const;
|
|
|
|
bool isDirectory(const QString &path) const;
|
|
|
|
bool isFile(const QString &path) const;
|
2011-08-09 02:42:11 +04:00
|
|
|
bool isAbsolute(const QString &path) const;
|
|
|
|
bool isExecutable(const QString &path) const;
|
|
|
|
bool isReadable(const QString &path) const;
|
|
|
|
bool isWritable(const QString &path) const;
|
|
|
|
bool isLink(const QString &path) const;
|
2011-06-14 14:28:51 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // FILESYSTEM_H
|