phantomjs/src/filesystem.h

145 lines
5.4 KiB
C
Raw Normal View History

/*
This file is part of the PhantomJS project from Ofi Labs.
Copyright (C) 2011 Ivan De Marino <ivan.de.marino@gmail.com>
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>
#include <QTextCodec>
#include <QTextStream>
#include <QVariant>
class File : public QObject
{
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
File(QFile *openfile, QTextCodec *codec, QObject *parent = 0);
virtual ~File();
public slots:
/**
* @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);
bool write(const QString &data);
bool seek(const qint64 pos);
QString readLine();
bool writeLine(const QString &data);
bool atEnd() const;
void flush();
void close();
QString getEncoding() const;
bool setEncoding(const QString &encoding);
private:
bool _isUnbuffered() const;
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;
};
class FileSystem : public QObject
{
Q_OBJECT
Q_PROPERTY(QString workingDirectory READ workingDirectory)
Q_PROPERTY(QString separator READ separator)
public:
FileSystem(QObject *parent = 0);
public slots:
// Attributes
// 'size(path)' implemented in "filesystem-shim.js" using '_size(path)'
int _size(const QString &path) const;
QVariant lastModified(const QString &path) const;
// Directory
// 'copyTree(source, destination)' implemented in "filesystem-shim.js" using '_copyTree(source, destination)'
bool _copyTree(const QString &source, const QString &destination) const;
bool makeDirectory(const QString &path) const;
bool makeTree(const QString &path) const;
// 'removeDirectory(path)' implemented in "filesystem-shim.js" using '_removeDirectory(path)'
bool _removeDirectory(const QString &path) const;
// 'removeTree(path)' implemented in "filesystem-shim.js" using '_removeTree(path)'
bool _removeTree(const QString &path) const;
// Files
// '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"
// '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"
// 'remove(path)' implemented in "filesystem-shim.js" using '_remove(path)'
bool _remove(const QString &path) const;
// 'copy(source, destination)' implemented in "filesystem-shim.js" using '_copy(source, destination)'
bool _copy(const QString &source, const QString &destination) const;
// 'move(source, destination)' implemented in "filesystem-shim.js"
// 'touch(path)' implemented in "filesystem-shim.js"
// Listing
QStringList list(const QString &path) const;
// Paths
QString separator() const;
QString workingDirectory() const;
bool changeWorkingDirectory(const QString &path) const;
QString absolute(const QString &relativePath) const;
// 'join(...)' implemented in "fs.js"
// 'split(path)' implemented in "fs.js"
QString fromNativeSeparators(const QString &path) const;
QString toNativeSeparators(const QString &path) const;
// Links
QString readLink(const QString &path) const;
// Tests
bool exists(const QString &path) const;
bool isDirectory(const QString &path) const;
bool isFile(const QString &path) const;
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;
};
#endif // FILESYSTEM_H