--- C:/Users/Hugheth/AppData/Local/Temp/jQuery.t-rev9946.svn008.tmp.js	Mon Jul 20 15:19:55 2009
+++ C:/Subversion/core/jquery/plugins/jQuery.twFile.js	Mon Jul 20 15:19:50 2009
@@ -1,4 +1,4 @@
-/*
+﻿/*
 jQuery.twFile.js
 
 jQuery plugin for loading a file and saving data to a file
@@ -33,9 +33,15 @@
 			this.getDriver();
 		},
 		load: function(filePath) {
-			return this.getDriver().loadFile(filePath);
+			return this.getDriver().loadFile($.twFile.getFilePath(filePath));
 		},
 		save: function(filePath,content) {
+			if(!content){
+				content = filePath;
+				filePath = $.twFile.getFilePath()
+			} else {
+				filePath = $.twFile.getFilePath(filePath)
+			}
 			return this.getDriver().saveFile(filePath,content);
 		},
 		copy: function(dest,source) {
@@ -43,6 +49,40 @@
 				return this.currentDriver.copyFile(dest,source);
 			else
 				return false;
+		},
+		getFilePath: function(filePath){	// helper function to retrieve requested file's path
+			// If blank then assume the file requested is the current file
+			if(!filePath){
+				filePath = document.location.href;
+			}
+			filePath = unescape(filePath);		// Unescape the filePath
+			// Remove any location or query part of the URL
+			var hashPos = filePath.indexOf("#");
+			var argPos = filePath.indexOf("?");
+			var nPos = Math.min((hashPos+1) ? hashPos : filePath.length, (argPos+1) ? argPos : filePath.length);
+			var info = filePath.substr(nPos);
+			filePath = filePath.substr(0,nPos);
+			// Convert file://localhost/ to file:///
+			if(filePath.indexOf("file://localhost/") == 0)
+			filePath = "file://" + filePath.substr(16);
+			// Convert to a native file format
+			if(filePath.charAt(9) == ":") // pc local file
+				filePath = filePath.substr(8).replace(new RegExp("/","g"),"\\");
+			else if(filePath.indexOf("file://///") == 0) // FireFox pc network file
+				filePath = "\\\\" + filePath.substr(10).replace(new RegExp("/","g"),"\\");
+			else if(filePath.indexOf("file:///") == 0) // mac/unix local file
+				filePath = filePath.substr(7);
+			else if(filePath.indexOf("file:/") == 0) // mac/unix local file
+				filePath = filePath.substr(5);
+			else if(filePath.indexOf("//") == 0) // pc network file
+				filePath = "\\\\" + filePath.substr(7).replace(new RegExp("/","g"),"\\");
+			// If relative then append pathname
+			if(filePath.indexOf("/")!=0&&(filePath.indexOf(":")==-1||filePath.indexOf(":")>2)){
+				var brPos = document.location.pathname.lastIndexOf("/");
+				filePath = "file://" + document.location.pathname.substr(0,brPos) + "/" + filePath;
+			}
+			// Return filePath
+			return filePath;
 		}
 	});
 
@@ -70,6 +110,7 @@
 			}
 			return true;
 		},
+
 		loadFile: function(filePath) {
 			// Returns null if it can't do it, false if there's an error, or a string of the content if successful
 			try {
@@ -78,7 +119,7 @@
 				var content = file.ReadAll();
 				file.Close();
 			} catch(ex) {
-				//# alert("Exception while attempting to load\n\n" + ex.toString());
+				alert("Exception while attempting to load\n\n" + ex.toString());
 				return null;
 			}
 			return content;
@@ -162,7 +203,7 @@
 					inputStream.close();
 					return result.join("");
 				} catch(ex) {
-					//# alert("Exception while attempting to load\n\n" + ex);
+					alert("Exception while attempting to load\n\n" + ex);
 					return false;
 				}
 			}
@@ -186,7 +227,7 @@
 					out.close();
 					return true;
 				} catch(ex) {
-					alert("Exception while attempting to save\n\n" + ex);
+					//# alert("Exception while attempting to save\n\n" + ex);
 					return false;
 				}
 			}
@@ -210,7 +251,7 @@
 			var r;
 			try {
 				if(document.applets["TiddlySaver"]) {
-					r = document.applets["TiddlySaver"].loadFile(javaUrlToFilename(filePath),"UTF-8");
+					r = document.applets["TiddlySaver"].loadFile(filePath,"UTF-8");
 					return (r === undefined || r === null) ? null : String(r);
 				}
 			} catch(ex) {
@@ -220,7 +261,7 @@
 		saveFile: function(filePath,content) {
 			try {
 				if(document.applets["TiddlySaver"])
-					return document.applets["TiddlySaver"].saveFile(javaUrlToFilename(filePath),"UTF-8",content);
+					return document.applets["TiddlySaver"].saveFile(filePath,"UTF-8",content);
 			} catch(ex) {
 			}
 			return null;
@@ -238,7 +279,7 @@
 			var r;
 			var content = [];
 			try {
-				r = new java.io.BufferedReader(new java.io.FileReader(javaUrlToFilename(filePath)));
+				r = new java.io.BufferedReader(new java.io.FileReader(filePath));
 				var line;
 				while((line = r.readLine()) != null)
 					content.push(new String(line));
@@ -250,7 +291,7 @@
 		},
 		saveFile: function(filePath,content) {
 			try {
-				var s = new java.io.PrintStream(new java.io.FileOutputStream(javaUrlToFilename(filePath)));
+				var s = new java.io.PrintStream(new java.io.FileOutputStream(filePath));
 				s.print(content);
 				s.close();
 			} catch(ex) {
@@ -260,14 +301,4 @@
 		}
 	}
 
-	// Private utilities
-
-	function javaUrlToFilename(url) {
-		var f = "//localhost";
-		if(url.indexOf(f) == 0)
-			return url.substring(f.length);
-		var i = url.indexOf(":");
-		return i > 0 ? url.substring(i-1) : url;
-	}
-
 })(jQuery);

