You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ti...@apache.org on 2012/05/18 00:37:08 UTC
[11/19] js commit: seek can now append stuff to files
seek can now append stuff to files
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/6c34ea6b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/6c34ea6b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/6c34ea6b
Branch: refs/heads/playbookFile
Commit: 6c34ea6b80b270b5d26e04b46858c2a7798754f6
Parents: 487f5ee
Author: Tim Kim <ti...@adobe.com>
Authored: Wed May 9 16:38:00 2012 -0700
Committer: Tim Kim <ti...@adobe.com>
Committed: Wed May 9 16:38:00 2012 -0700
----------------------------------------------------------------------
lib/playbook/plugin/playbook/FileWriter.js | 45 ++++++++++++++++-------
1 files changed, 31 insertions(+), 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/6c34ea6b/lib/playbook/plugin/playbook/FileWriter.js
----------------------------------------------------------------------
diff --git a/lib/playbook/plugin/playbook/FileWriter.js b/lib/playbook/plugin/playbook/FileWriter.js
index 91ef97f..f2dfced 100644
--- a/lib/playbook/plugin/playbook/FileWriter.js
+++ b/lib/playbook/plugin/playbook/FileWriter.js
@@ -88,45 +88,62 @@ FileWriter.prototype.write = function(text) {
// can't append to file - must open a temp file, write to temp, then delete current file
// and save again - lame
// also have to investigate how bb blobs work in order to set proper seek positions :S
- console.log('writing to file');
-
+
if (typeof me.onwrite === "function") {
- me.onwrite(new ProgressEvent("write", {"target":me}));
+ me.onwrite(new ProgressEvent("write", {"target":me}));
}
-
+
var textBlob = blackberry.utils.stringToBlob(text);
+
if(blackberry.io.file.exists(this.fileName)){
- // for now just overwrite
- var oldTextBlob = '';
-
+
+ var oldText = '';
+ var newText = text;
+
var getFileContents = function(path,blob){
if(blob){
- oldTextBlob = blackberry.utils.blobToString(blob);
+ oldText = blackberry.utils.blobToString(blob);
+ if(oldText.length>0){
+ console.log('old text: ' + oldText);
+ newText = oldText.substr(0,me.position) + text + oldText.substr(me.position);
+ console.log('newText: '+ newText);
+ }
}
- // seek position stuff here
- var newTextBlob = text;
var tempFile = me.fileName+'temp';
if(blackberry.io.file.exists(tempFile)){
blackberry.io.file.deleteFile(tempFile);
}
-
- // crete a temp file, delete file we are 'overwriting', then rename temp file
- blackberry.io.file.saveFile(tempFile, blackberry.utils.stringToBlob(newTextBlob));
+
+ var newTextBlob = blackberry.utils.stringToBlob(newText);
+
+ // crete a temp file, delete file we are 'overwriting', then rename temp file
+ blackberry.io.file.saveFile(tempFile, newTextBlob);
blackberry.io.file.deleteFile(me.fileName);
blackberry.io.file.rename(tempFile, me.fileName.split('/').pop());
+
+ me.position = newText.length;
+ me.length = me.position;
}
// setting asynch to off - worry about making this all callbacks later
blackberry.io.file.readFile(this.fileName, getFileContents, false);
}else{
+
+ // file is new so just save it
blackberry.io.file.saveFile(this.fileName, textBlob);
+ me.position = text.length;
+ me.length = me.position;
}
me.readyState = FileWriter.DONE;
- console.log('done writing file');
+
+ if (typeof me.onwriteend === "function") {
+ me.onwriteend(new ProgressEvent("writeend", {"target":me}));
+ }
+
/*
exec(
// Success callback