--- C:/Users/Hugheth/AppData/Local/Temp/jQuery.twFile.js-rev9946.svn000.tmp.js	Wed Jul 15 15:02:23 2009
+++ C:/Subversion/core/jquery/plugins/jQuery.twFile.js	Wed Jul 15 14:43:01 2009
@@ -1,4 +1,4 @@
-/*
+﻿/*
 jQuery.twFile.js
 
 jQuery plugin for loading a file and saving data to a file
@@ -10,7 +10,6 @@
   http://www.opensource.org/licenses/mit-license.php
   http://www.gnu.org/licenses/gpl.html
 */
-
 (function($) {
 	if(!$.twFile) {
 		$.twFile = {};
@@ -20,22 +19,11 @@
 		currentDriver: null,
 		driverList: ["activeX", "mozilla", "tiddlySaver", "javaLiveConnect"],
 
-		getDriver: function() {
-			if(this.currentDriver === null) {
-				for(var t=0; t<this.driverList.length; t++) {
-					if(this.currentDriver === null && drivers[this.driverList[t]].isAvailable && drivers[this.driverList[t]].isAvailable())
-						this.currentDriver = drivers[this.driverList[t]];
-				}
-			}
-			return this.currentDriver;
-		},
-		init: function() {
-			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,9 +31,56 @@
 				return this.currentDriver.copyFile(dest,source);
 			else
 				return false;
+		},
+		getDriver: function() {
+			if(this.currentDriver === null) {
+				for(var t=0; t<this.driverList.length; t++) {
+					if(this.currentDriver === null && drivers[this.driverList[t]].isAvailable && drivers[this.driverList[t]].isAvailable())
+						this.currentDriver = drivers[this.driverList[t]];
+				}
+			}
+			return this.currentDriver;
+		},
+		getFilePath: function(fPath){	// helper function to retrieve requested file's path
+			// If blank then assume the file requested is the current file
+			if(!fPath){
+				fPath = document.location.href;
+			}
+			fPath = unescape(fPath);		// Unescape the fPath
+			//var origPath = convertUriToUTF8(fPath,config.options.txtFileSystemCharSet);
+			var origPath = fPath;
+			// Remove any location or query part of the URL
+			var hashPos = fPath.indexOf("#");
+			var argPos = fPath.indexOf("?");
+			var nPos = Math.min((hashPos+1)?hashPos:fPath.length,(argPos+1)?argPos:fPath.length);
+			var info = origPath.substr(nPos);
+			origPath = origPath.substr(0,nPos);
+			// Convert file://localhost/ to file:///
+			if(origPath.indexOf("file://localhost/") == 0)
+			origPath = "file://" + origPath.substr(16);
+			// Convert to a native file format
+			var localPath = origPath;
+			if(origPath.charAt(9) == ":") // pc local file
+				localPath = origPath.substr(8).replace(new RegExp("/","g"),"\\");
+			else if(origPath.indexOf("file://///") == 0) // FireFox pc network file
+				localPath = "\\\\" + origPath.substr(10).replace(new RegExp("/","g"),"\\");
+			else if(origPath.indexOf("file:///") == 0) // mac/unix local file
+				localPath = origPath.substr(7);
+			else if(origPath.indexOf("file:/") == 0) // mac/unix local file
+				localPath = origPath.substr(5);
+			else if(origPath.indexOf("//") == 0) // pc network file
+				localPath = "\\\\" + unescape(origPath.substr(7)).replace(new RegExp("/","g"),"\\");
+			// If relative then append pathname
+			if(localPath.indexOf("/")!=0&&(localPath.indexOf(":")==-1||localPath.indexOf(":")>2)){
+				var brPos = document.location.pathname.lastIndexOf("/");
+				localPath = "file://" + document.location.pathname.substr(0,brPos) + "/" + localPath;
+			}
+			// Append original information to the path
+			// Return localPath
+			return localPath;
 		}
 	});
-
+	
 	// Deferred initialisation for any drivers that need it
 	$(function() {
 		for(var t in drivers) {
@@ -78,7 +113,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;
@@ -186,7 +221,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 +245,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 +255,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 +273,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 +285,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 +295,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);

