mirror of https://github.com/vitalif/phantomjs
Allow passing option object to fs.{open,read,write}
http://code.google.com/p/phantomjs/issues/detail?id=3671.5
parent
f6c29b16c1
commit
21906c5536
|
@ -274,11 +274,20 @@ QString FileSystem::absolute(const QString &relativePath) const
|
|||
}
|
||||
|
||||
// Files
|
||||
QObject *FileSystem::_open(const QString &path, const QString &mode) const
|
||||
QObject *FileSystem::_open(const QString &path, const QVariantMap &opts) const
|
||||
{
|
||||
File *f = NULL;
|
||||
QFile *_f = new QFile(path);
|
||||
QFile::OpenMode modeCode = QFile::NotOpen;
|
||||
QVariant modeVar = opts["mode"];
|
||||
|
||||
// Ensure only strings
|
||||
if (modeVar.type() != QVariant::String) {
|
||||
qDebug() << "FileSystem::open - " << "Mode must be a string!" << modeVar;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QString mode = modeVar.toString();
|
||||
|
||||
// Ensure only one "mode character" has been selected
|
||||
if ( mode.length() != 1) {
|
||||
|
|
|
@ -87,10 +87,10 @@ public slots:
|
|||
bool _removeTree(const QString &path) const;
|
||||
|
||||
// Files
|
||||
// 'open(path, mode)' implemented in "filesystem-shim.js" using '_open(path, mode)'
|
||||
QObject *_open(const QString &path, const QString &mode) const;
|
||||
// 'read(path)' implemented in "filesystem-shim.js"
|
||||
// 'write(path, mode)' implemented in the "filesystem-shim.js"
|
||||
// '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"
|
||||
// 'write(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)'
|
||||
|
|
|
@ -36,11 +36,29 @@
|
|||
* It will throw exception if it fails.
|
||||
*
|
||||
* @param path Path of the file to open
|
||||
* @param mode Open Mode. A string made of 'r', 'w', 'a/+' characters.
|
||||
* @param modeOrOpts
|
||||
* mode: Open Mode. A string made of 'r', 'w', 'a/+' characters.
|
||||
* opts: Options.
|
||||
* - mode (see Open Mode above)
|
||||
* @return "file" object
|
||||
*/
|
||||
exports.open = function (path, mode) {
|
||||
var file = exports._open(path, mode);
|
||||
exports.open = function (path, modeOrOpts) {
|
||||
var file, opts;
|
||||
|
||||
// Extract charset from opts
|
||||
if (modeOrOpts == null) {
|
||||
// Empty options
|
||||
opts = {};
|
||||
} else if (typeof modeOrOpts !== 'object') {
|
||||
opts = {
|
||||
mode: modeOrOpts
|
||||
};
|
||||
} else {
|
||||
opts = modeOrOpts;
|
||||
}
|
||||
|
||||
// Open file
|
||||
file = exports._open(path, opts);
|
||||
if (file) {
|
||||
return file;
|
||||
}
|
||||
|
@ -51,10 +69,15 @@ exports.open = function (path, mode) {
|
|||
* It will throw an exception if it fails.
|
||||
*
|
||||
* @param path Path of the file to read from
|
||||
* @param opts Options.
|
||||
* @return file content
|
||||
*/
|
||||
exports.read = function (path) {
|
||||
var f = exports.open(path, 'r'),
|
||||
exports.read = function (path, opts) {
|
||||
if (opts == null || typeof opts !== 'object') {
|
||||
opts = {};
|
||||
}
|
||||
opts.mode = 'r';
|
||||
var f = exports.open(path, opts),
|
||||
content = f.read();
|
||||
|
||||
f.close();
|
||||
|
@ -66,10 +89,16 @@ exports.read = function (path) {
|
|||
*
|
||||
* @param path Path of the file to read from
|
||||
* @param content Content to write to the file
|
||||
* @param mode Open Mode. A string made of 'w' or 'a / +' characters.
|
||||
* @param modeOrOpts
|
||||
* mode: Open Mode. A string made of 'r', 'w', 'a/+' characters.
|
||||
* opts: Options.
|
||||
* - mode (see Open Mode above)
|
||||
*/
|
||||
exports.write = function (path, content, mode) {
|
||||
var f = exports.open(path, mode);
|
||||
exports.write = function (path, content, modeOrOpts) {
|
||||
if (modeOrOpts == null) {
|
||||
modeOrOpts = {};
|
||||
}
|
||||
var f = exports.open(path, modeOrOpts);
|
||||
|
||||
f.write(content);
|
||||
f.close();
|
||||
|
|
Loading…
Reference in New Issue