got truncate working
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/a338d4b4 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/a338d4b4 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/a338d4b4 Branch: refs/heads/playbookFile Commit: a338d4b4fc5081bb60cf12e2270f9e2aadb950d5 Parents: 6c34ea6 Author: Tim Kim <t...@adobe.com> Authored: Wed May 9 17:18:59 2012 -0700 Committer: Tim Kim <t...@adobe.com> Committed: Wed May 9 17:18:59 2012 -0700 ---------------------------------------------------------------------- lib/playbook/plugin/playbook/FileWriter.js | 140 +++++++---------------- 1 files changed, 42 insertions(+), 98 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/a338d4b4/lib/playbook/plugin/playbook/FileWriter.js ---------------------------------------------------------------------- diff --git a/lib/playbook/plugin/playbook/FileWriter.js b/lib/playbook/plugin/playbook/FileWriter.js index f2dfced..159b0df 100644 --- a/lib/playbook/plugin/playbook/FileWriter.js +++ b/lib/playbook/plugin/playbook/FileWriter.js @@ -84,10 +84,7 @@ FileWriter.prototype.write = function(text) { } // Write file - // TODO: Need to think about how to make this asynch - // 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 + // TODO: Need to think about how to make this asynch and add try/catch if (typeof me.onwrite === "function") { me.onwrite(new ProgressEvent("write", {"target":me})); @@ -105,9 +102,7 @@ FileWriter.prototype.write = function(text) { if(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); } } @@ -143,59 +138,6 @@ FileWriter.prototype.write = function(text) { if (typeof me.onwriteend === "function") { me.onwriteend(new ProgressEvent("writeend", {"target":me})); } - - /* - exec( - // Success callback - function(r) { - // If DONE (cancelled), then don't do anything - if (me.readyState === FileWriter.DONE) { - return; - } - - // position always increases by bytes written because file would be extended - me.position += r; - // The length of the file is now where we are done writing. - - me.length = me.position; - - // DONE state - me.readyState = FileWriter.DONE; - - // If onwrite callback - if (typeof me.onwrite === "function") { - me.onwrite(new ProgressEvent("write", {"target":me})); - } - - // If onwriteend callback - if (typeof me.onwriteend === "function") { - me.onwriteend(new ProgressEvent("writeend", {"target":me})); - } - }, - // Error callback - function(e) { - // If DONE (cancelled), then don't do anything - if (me.readyState === FileWriter.DONE) { - return; - } - - // DONE state - me.readyState = FileWriter.DONE; - - // Save error - me.error = new FileError(e); - - // If onerror callback - if (typeof me.onerror === "function") { - me.onerror(new ProgressEvent("error", {"target":me})); - } - - // If onwriteend callback - if (typeof me.onwriteend === "function") { - me.onwriteend(new ProgressEvent("writeend", {"target":me})); - } - }, "File", "write", [this.fileName, text, this.position]); - */ }; /** @@ -255,56 +197,58 @@ FileWriter.prototype.truncate = function(size) { } // Write file - /* - exec( - // Success callback - function(r) { - // If DONE (cancelled), then don't do anything - if (me.readyState === FileWriter.DONE) { - return; - } + // TODO: Need to think about how to make this asynch and add try/catch - // DONE state - me.readyState = FileWriter.DONE; + if (typeof me.onwrite === "function") { + me.onwrite(new ProgressEvent("write", {"target":me})); + } - // Update the length of the file - me.length = r; - me.position = Math.min(me.position, r); + if(blackberry.io.file.exists(this.fileName)){ - // If onwrite callback - if (typeof me.onwrite === "function") { - me.onwrite(new ProgressEvent("write", {"target":me})); - } + var oldText = ''; + var newText = ''; - // If onwriteend callback - if (typeof me.onwriteend === "function") { - me.onwriteend(new ProgressEvent("writeend", {"target":me})); + var getFileContents = function(path,blob){ + + if(blob){ + oldText = blackberry.utils.blobToString(blob); + if(oldText.length>0){ + newText = oldText.slice(0,size); + }else{ + // TODO: throw error + } } - }, - // Error callback - function(e) { - // If DONE (cancelled), then don't do anything - if (me.readyState === FileWriter.DONE) { - return; + + var tempFile = me.fileName+'temp'; + if(blackberry.io.file.exists(tempFile)){ + blackberry.io.file.deleteFile(tempFile); } - // DONE state - me.readyState = FileWriter.DONE; + var newTextBlob = blackberry.utils.stringToBlob(newText); - // Save error - me.error = new FileError(e); + // 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()); - // If onerror callback - if (typeof me.onerror === "function") { - me.onerror(new ProgressEvent("error", {"target":me})); - } + 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{ + + // TODO: file doesn't exist - throw error - // If onwriteend callback - if (typeof me.onwriteend === "function") { + } + + me.readyState = FileWriter.DONE; + + if (typeof me.onwriteend === "function") { me.onwriteend(new ProgressEvent("writeend", {"target":me})); - } - }, "File", "truncate", [this.fileName, size]); - */ + } }; module.exports = FileWriter;