phantomjs/src/filesystem.cpp

531 lines
14 KiB
C++
Raw Permalink 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.
*/
#include "filesystem.h"
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <QDateTime>
// File
// public:
File::File(QFile *openfile, QTextCodec *codec, QObject *parent) :
QObject(parent),
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
m_file(openfile),
m_fileStream(0)
{
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
if ( codec ) {
m_fileStream = new QTextStream(m_file);
m_fileStream->setCodec(codec);
}
}
File::~File()
{
this->close();
}
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
//NOTE: for binary files we want to use QString instead of QByteArray as the
// latter is not really useable in javascript and e.g. window.btoa expects a string
// and we need special code required since fromAsci() would stop as soon as it
// encounters \0 or similar
// public slots:
QString File::read(const QVariant &n)
{
// Default to 1024 (used when n is "null")
qint64 bytesToRead = 1024;
// If parameter can be converted to a qint64, do so and use that value instead
if (n.canConvert(QVariant::LongLong)) {
bytesToRead = n.toLongLong();
}
const bool isReadAll = 0 > bytesToRead;
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
if ( !m_file->isReadable() ) {
qDebug() << "File::read - " << "Couldn't read:" << m_file->fileName();
return QString();
}
if ( m_file->isWritable() ) {
// make sure we write everything to disk before reading
flush();
}
if ( m_fileStream ) {
// text file
QString ret;
if (isReadAll) {
// This code, for some reason, reads the whole file from 0 to EOF,
// and then resets to the position the file was at prior to reading
const qint64 pos = m_fileStream->pos();
m_fileStream->seek(0);
ret = m_fileStream->readAll();
m_fileStream->seek(pos);
} else {
ret = m_fileStream->read(bytesToRead);
}
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
return ret;
} else {
// binary file
QByteArray data;
if (isReadAll) {
// This code, for some reason, reads the whole file from 0 to EOF,
// and then resets to the position the file was at prior to reading
const qint64 pos = m_file->pos();
m_file->seek(0);
data = m_file->readAll();
m_file->seek(pos);
} else {
data = m_file->read(bytesToRead);
}
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
QString ret(data.size());
for(int i = 0; i < data.size(); ++i) {
ret[i] = data.at(i);
}
return ret;
}
}
bool File::write(const QString &data)
{
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
if ( !m_file->isWritable() ) {
qDebug() << "File::write - " << "Couldn't write:" << m_file->fileName();
return true;
}
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
if ( m_fileStream ) {
// text file
(*m_fileStream) << data;
if (_isUnbuffered()) {
m_fileStream->flush();
}
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
return true;
} else {
// binary file
QByteArray bytes(data.size(), Qt::Uninitialized);
for(int i = 0; i < data.size(); ++i) {
bytes[i] = data.at(i).toLatin1();
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
}
return m_file->write(bytes);
}
}
bool File::seek(const qint64 pos)
{
if (m_fileStream) {
return m_fileStream->seek(pos);
} else {
return m_file->seek(pos);
}
}
QString File::readLine()
{
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
if ( !m_file->isReadable() ) {
qDebug() << "File::readLine - " << "Couldn't read:" << m_file->fileName();
return QString();
}
if ( m_file->isWritable() ) {
// make sure we write everything to disk before reading
flush();
}
if ( m_fileStream ) {
// text file
return m_fileStream->readLine();
} else {
// binary file - doesn't make much sense but well...
return QString::fromLatin1(m_file->readLine());
}
}
bool File::writeLine(const QString &data)
{
if ( write(data) && write("\n") ) {
return true;
}
qDebug() << "File::writeLine - " << "Couldn't write:" << m_file->fileName();
return false;
}
bool File::atEnd() const
{
if ( m_file->isReadable() ) {
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
if (m_fileStream) {
// text file
return m_fileStream->atEnd();
} else {
// binary file
return m_file->atEnd();
}
}
qDebug() << "File::atEnd - " << "Couldn't read:" << m_file->fileName();
return false;
}
void File::flush()
{
if ( 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
if ( m_fileStream ) {
// text file
m_fileStream->flush();
}
// binary or text file
m_file->flush();
}
}
void File::close()
{
flush();
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
if ( m_fileStream ) {
delete m_fileStream;
m_fileStream = 0;
}
if ( m_file ) {
m_file->close();
delete m_file;
m_file = NULL;
}
deleteLater();
}
bool File::setEncoding(const QString &encoding) {
if (encoding.isEmpty() || encoding.isNull()) {
return false;
}
// "Binary" mode doesn't use/need text codecs
if ((QTextStream *)NULL == m_fileStream) {
// TODO: Should we switch to "text" mode?
return false;
}
// Since there can be multiple names for the same codec (i.e., "utf8" and
// "utf-8"), we need to get the codec in the system first and use its
// canonical name
QTextCodec *codec = QTextCodec::codecForName(encoding.toLatin1());
if ((QTextCodec *)NULL == codec) {
return false;
}
// Check whether encoding actually needs to be changed
const QString encodingBeforeUpdate(m_fileStream->codec()->name());
if (0 == encodingBeforeUpdate.compare(QString(codec->name()), Qt::CaseInsensitive)) {
return false;
}
m_fileStream->setCodec(codec);
// Return whether update was successful
const QString encodingAfterUpdate(m_fileStream->codec()->name());
return 0 != encodingBeforeUpdate.compare(encodingAfterUpdate, Qt::CaseInsensitive);
}
QString File::getEncoding() const
{
QString encoding;
if ((QTextStream *)NULL != m_fileStream) {
encoding = QString(m_fileStream->codec()->name());
}
return encoding;
}
// private:
bool File::_isUnbuffered() const
{
return m_file->openMode() & QIODevice::Unbuffered;
}
// FileSystem
// public:
A REPL for PhantomJS This covers [Issue 252](http://code.google.com/p/phantomjs/issues/detail?id=252) The commit is composed of 12 squashed commits: commit efdc6ba4f143c30a690fd97d92d80fa412e79999 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Mon Feb 27 00:19:36 2012 +0000 Pretty-pringing and Completion Caching done! * This completes pretty-printing for the result of evaluated * expressions in the REPL. * Also, now we cache the "possible completions", to speed things up * a bit (nothing fancy though). * Minor tweaks to the internal doc and the way we "mock" * pretty-printing for QObjects/REPLCompletanle * All tests passing :) commit 1f9ef690e112a535b431fca409b77bb9c09d1c70 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sun Feb 26 22:35:00 2012 +0000 Moving most of REPL shim JavaScritp code in a separate file. Way easier to work on. commit 02d460a16fee14e7096ae7d899c03902c5b8a9c6 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sat Feb 25 20:25:18 2012 +0000 Initialisation of the Completions is now done in a pure virtual. This means that every REPLCompletable object will ACTUALLY register completion strings, ONLY if we are running a REPL and that object is ACTUALLY created. Otherwise, why bother? Adding completions for all exposed REPLCompletable objects Also, fixed an issue with _getCompletions() commit 412c3778fb04aa1c7379f8e760afce702b0428dd Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Tue Feb 21 00:49:17 2012 +0000 Few more tweaks to the REPL: - Now 'phantom' is the first QObject with proper completion - No repetition in QObject completions - LVAL of any user expression is now correctly prettified and printed Major things left to do: - Cache completions (using QCache?) - Add completions for the other QObject - When the LVAL of a user expression is a QObject, print what's expected, not the QObject "real" structure commit 46f04713c8165d898055e15478bb31403f8c93f1 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Tue Feb 7 10:13:23 2012 -0800 Pretty-print expressions result Still not done though: there are issues with the NON-Native JS objects. commit 98b2fe67651dc750b62c6fa9cf1d80317fd9ae06 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Fri Feb 3 00:22:52 2012 -0800 Introducing REPLCompletable. This class should be inherited by any JavaScript-exposed QObject, to ensure correct Auto-Completion. Correct auto-completion for QObjects. - Now even QObjects can correctly provide auto-completion, and avoid showing "not for users" methods - The strings used for the auto-completion are stored in a single Index: minimum memory footprint - Still, there is optimization that should be done (when "searching" for the right completion by prefix) - Completion for the objects not set up yet, but now it's just a trivial sequence of "addCompletion('bla')" in their constructors commit 9bd48618154b1530a37b41f4060440184e23253d Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Thu Feb 2 00:20:25 2012 -0800 Changing the way we import Linenoise. Will just import a specific commit, and update manually when needed. commit cfc9bae9fbdab13b01019b34b7cbd565e3153780 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sun Jan 29 23:22:26 2012 -0800 Made the REPL into a Singleton. With Auto-completion!. Reasons: 1) Needed a pointer to function (i.e. a static method) to be used with Linenoise to provide auto-completions 2) It makes more sense, as it's not like we are going to have 2 REPL running at the same time, are we? There are problems to address: - the enumeration in JS seems to return only the native interface of our objects - the function completions contain argument types of those functions - "private" methods are exposed commit c78bd32e17f8e0e4cc4a0066858de8cc81d33b97 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sun Jan 29 22:10:20 2012 -0800 Migrating from the original, now [unmantained Linenoise](https://github.com/antirez/linenoise) to the fairly active [tadmarshall fork](https://github.com/tadmarshall/linenoise). Also now the project is imported as a Git Submodule. Having migrated to the latest Linenoise (see prev. commit), now this _SHOULD_ work on Windows too. But, of course, this needs testing. :) commit 43713c5723d7c5ed446ba41ae8d6f8c9feba7f9b Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Tue Jan 24 23:17:06 2012 -0800 Now that the basics work, I'm adding support for REPL history. This is something almost everyone today is accustomed to. Also, now REPL history works! And I found some useful resources to solve pending TODOs. commit 31e5f88b044a5b4a823c67527ef8c245d2ac7863 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sun Jan 22 20:56:36 2012 -0800 Adding Linenoise Project (https://github.com/antirez/linenoise). For now is included as a drop-in set of files. Later on, if the Linenoise project has frequent updates, we might prefer to do it as a git-submodule. commit 4be9c15c65db4767e482fba0be13f8aab286d5f3 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Thu Jan 5 15:31:13 2012 +0000 First simple REPL implementation. - Not complete - Still doesn't handle arrow keys (needed for history)
2012-01-05 19:31:13 +04:00
FileSystem::FileSystem(QObject *parent)
: QObject(parent)
{ }
// public slots:
// Attributes
int FileSystem::_size(const QString &path) const
{
QFileInfo fi(path);
if (fi.exists()) {
return fi.size();
}
return -1;
}
QVariant FileSystem::lastModified(const QString &path) const
{
QFileInfo fi(path);
if (fi.exists()) {
return QVariant(fi.lastModified());
}
return QVariant(QDateTime());
}
// Links
QString FileSystem::readLink(const QString &path) const
{
return QFileInfo(path).symLinkTarget();
}
// Tests
bool FileSystem::exists(const QString &path) const
{
return QFile::exists(path);
}
bool FileSystem::isDirectory(const QString &path) const
{
return QFileInfo(path).isDir();
}
bool FileSystem::isFile(const QString &path) const
{
return QFileInfo(path).isFile();
}
bool FileSystem::isAbsolute(const QString &path) const {
return QFileInfo(path).isAbsolute();
}
bool FileSystem::isExecutable(const QString &path) const {
return QFileInfo(path).isExecutable();
}
bool FileSystem::isLink(const QString &path) const {
return QFileInfo(path).isSymLink();
}
bool FileSystem::isReadable(const QString &path) const {
return QFileInfo(path).isReadable();
}
bool FileSystem::isWritable(const QString &path) const {
return QFileInfo(path).isWritable();
}
// Directory
bool FileSystem::_copyTree(const QString &source, const QString &destination) const {
QDir sourceDir(source);
QDir::Filters sourceDirFilter = QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files | QDir::NoSymLinks | QDir::Drives;
if (sourceDir.exists()) {
// Make the destination directory if it doesn't exist already
if (!FileSystem::exists(destination) && !FileSystem::makeDirectory(destination)) {
return false;
}
foreach(QFileInfo entry, sourceDir.entryInfoList(sourceDirFilter, QDir::DirsFirst)) {
if (entry.isDir()) {
if (!FileSystem::_copyTree(entry.absoluteFilePath(),
destination + "/" + entry.fileName())) { //< directory: recursive call
return false;
}
} else {
if (!FileSystem::_copy(entry.absoluteFilePath(),
destination + "/" + entry.fileName())) { //< file: copy
return false;
}
}
}
}
return true;
}
bool FileSystem::makeDirectory(const QString &path) const
{
return QDir().mkdir(path);
}
bool FileSystem::makeTree(const QString &path) const
{
return QDir().mkpath(path);
}
bool FileSystem::_removeDirectory(const QString &path) const
{
return QDir().rmdir(path);
}
bool FileSystem::_removeTree(const QString &path) const
{
QDir dir(path);
QDir::Filters dirFilter = QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files;
if (dir.exists()) {
foreach(QFileInfo info, dir.entryInfoList(dirFilter, QDir::DirsFirst)) {
if (info.isDir()) {
if (!FileSystem::_removeTree(info.absoluteFilePath())) { //< directory: recursive call
return false;
}
} else {
if (!FileSystem::_remove(info.absoluteFilePath())) { //< file: remove
return false;
}
}
}
if (!FileSystem::_removeDirectory(path)) { //< delete the top tree directory
return false;
}
}
return true;
}
QStringList FileSystem::list(const QString &path) const
{
return QDir(path).entryList();
}
// Paths
QString FileSystem::separator() const
{
return QDir::separator();
}
QString FileSystem::workingDirectory() const
{
return QDir::currentPath();
}
bool FileSystem::changeWorkingDirectory(const QString &path) const
{
return QDir::setCurrent(path);
}
QString FileSystem::absolute(const QString &relativePath) const
{
return QFileInfo(relativePath).absoluteFilePath();
}
QString FileSystem::fromNativeSeparators(const QString &path) const
{
return QDir::fromNativeSeparators(path);
}
QString FileSystem::toNativeSeparators(const QString &path) const
{
return QDir::toNativeSeparators(path);
}
// Files
QObject *FileSystem::_open(const QString &path, const QVariantMap &opts) const
{
qDebug() << "FileSystem - _open:" << path << opts;
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
const QVariant modeVar = opts["mode"];
// Ensure only strings
if (modeVar.type() != QVariant::String) {
qDebug() << "FileSystem::open - " << "Mode must be a string!" << modeVar;
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
return 0;
}
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
bool isBinary = false;
QFile::OpenMode modeCode = QFile::NotOpen;
// Determine the OpenMode
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
foreach(const QChar &c, modeVar.toString()) {
switch(c.toLatin1()) {
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
case 'r': case 'R': {
modeCode |= QFile::ReadOnly;
break;
}
case 'a': case 'A': case '+': {
modeCode |= QFile::Append;
modeCode |= QFile::WriteOnly;
break;
}
case 'w': case 'W': {
modeCode |= QFile::WriteOnly;
break;
}
case 'b': case 'B': {
isBinary = true;
break;
}
default: {
qDebug() << "FileSystem::open - " << "Wrong Mode:" << c;
return 0;
}
}
}
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
// Make sure the file exists OR it can be created at the required path
if ( !QFile::exists(path) && modeCode & QFile::WriteOnly ) {
if ( !makeTree(QFileInfo(path).dir().absolutePath()) ) {
qDebug() << "FileSystem::open - " << "Full path coulnd't be created:" << path;
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
return 0;
}
}
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
// Make sure there is something to read
if ( !QFile::exists(path) && modeCode & QFile::ReadOnly ) {
qDebug() << "FileSystem::open - " << "Trying to read a file that doesn't exist:" << path;
return 0;
}
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
QTextCodec *codec = 0;
if (!isBinary) {
// default to UTF-8 encoded files
const QString charset = opts.value("charset", "UTF-8").toString();
codec = QTextCodec::codecForName(charset.toLatin1());
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
if (!codec) {
qDebug() << "FileSystem::open - " << "Unknown charset:" << charset;
return 0;
}
}
// Try to Open
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
QFile* file = new QFile(path);
if ( !file->open(modeCode) ) {
// Return "NULL" if the file couldn't be opened as requested
delete file;
qDebug() << "FileSystem::open - " << "Couldn't be opened:" << path;
return 0;
}
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
return new File(file, codec);
}
bool FileSystem::_remove(const QString &path) const
{
return QFile::remove(path);
}
bool FileSystem::_copy(const QString &source, const QString &destination) const {
return QFile(source).copy(destination);
}