incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tim...@apache.org
Subject [43/48] js commit: seek can now append stuff to files
Date Thu, 07 Jun 2012 20:40:44 GMT
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/3e1da2ab
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/3e1da2ab
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/3e1da2ab

Branch: refs/heads/master
Commit: 3e1da2abc68644b47bf1483dfe13ea10f0721d79
Parents: ecf5cec
Author: Tim Kim <timk@adobe.com>
Authored: Wed May 9 16:38:00 2012 -0700
Committer: Tim Kim <tim.kim@nitobi.com>
Committed: Thu Jun 7 13:40:22 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/3e1da2ab/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


Mime
View raw message