Return-Path: X-Original-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-callback-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A80729FB6 for ; Thu, 7 Jun 2012 20:40:45 +0000 (UTC) Received: (qmail 71363 invoked by uid 500); 7 Jun 2012 20:40:45 -0000 Delivered-To: apmail-incubator-callback-commits-archive@incubator.apache.org Received: (qmail 71310 invoked by uid 500); 7 Jun 2012 20:40:45 -0000 Mailing-List: contact callback-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: callback-dev@incubator.apache.org Delivered-To: mailing list callback-commits@incubator.apache.org Received: (qmail 70627 invoked by uid 99); 7 Jun 2012 20:40:44 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Jun 2012 20:40:44 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 69DF210B1F; Thu, 7 Jun 2012 20:40:44 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: timkim@apache.org To: callback-commits@incubator.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [34/48] js commit: got truncate working Message-Id: <20120607204044.69DF210B1F@tyr.zones.apache.org> Date: Thu, 7 Jun 2012 20:40:44 +0000 (UTC) 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/881194cb Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/881194cb Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/881194cb Branch: refs/heads/master Commit: 881194cbeb7c014b6a0157aad9dccb60a10d9c71 Parents: 3e1da2a Author: Tim Kim Authored: Wed May 9 17:18:59 2012 -0700 Committer: Tim Kim Committed: Thu Jun 7 13:40:22 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/881194cb/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;