-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbase2-io-p.js
9 lines (8 loc) · 8.49 KB
/
base2-io-p.js
1
2
3
4
5
6
7
8
9
/*
base2 - copyright 2007-2009, Dean Edwards
http://code.google.com/p/base2/
http://www.opensource.org/licenses/mit-license.php
Contributors:
Doeke Zanstra
*/
new function(_d){var io=new base2.Package(this,{name:"io",version:base2.version,imports:"Enumerable,Function2",exports:"NOT_SUPPORTED,READ,WRITE,FileSystem,Directory,LocalFileSystem,LocalDirectory,LocalFile"});eval(this.imports);var NOT_SUPPORTED=function(){throw new Error("Not supported.");};var READ=1,WRITE=2;var _7=/\/[^\/]+\/\.\./,_8=/[^\/]+$/,_9=/\//g,_a=/\\/g,_b=/^\//,_c=/\/$/;var _5=function(){throw new Error("Invalid file open mode.");};var _6={fromNativePath:function(a){return"/"+String(a).replace(_a,"/")},toNativePath:function(a){return String(a).replace(_b,"").replace(_9,"\\")}};function _4(a){return LocalFileSystem.toNativePath(FileSystem.resolve(LocalFileSystem.getPath(),a))};var _3;function _2(a,b,c,d){if(!_3)_3=new ActiveXObject("Scripting.FileSystemObject");b=_4(b);if(arguments.length>2){c=_4(c)}switch(arguments.length){case 2:return _3[a](b);case 3:return _3[a](b,c);case 4:return _3[a](b,c,d)}return undefined};function _0(a){var b=XPCOM.createObject("file/local;1","nsILocalFile");b.initWithPath(_4(a));return b};function _1(a){return new java.io.File(_4(a))};var XPCOM=Module.extend({privelegedMethod:I,privelegedObject:I,"@(Components)":{createObject:function(a,b){if(a.indexOf("@")!=0){a="@mozilla.org/"+a}try{return new(new Components.Constructor(a,b))}catch(error){throw new Error(format("Failed to create object '%1' (%2).",b,error.message));}},privelegedMethod:function(a){return function(){netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");return a.apply(this,arguments)}},privelegedObject:function(c){Base.forEach(c,function(a,b){if(typeof a=="function"){c[b]=XPCOM.privelegedMethod(a)}})}}});var FileSystem=Base.extend({constructor:function(a){if(a)this.chdir(a)},path:"/",chdir:function(a){assert(this.isDirectory(a),a+" is not a directory.");a=this.makepath(a);if(!_c.test(a))a+="/";this.path=a},makepath:function(a,b){if(arguments.length==1){b=a;a=this.path}return FileSystem.resolve(a,b)},copy:NOT_SUPPORTED,exists:NOT_SUPPORTED,isDirectory:NOT_SUPPORTED,isFile:NOT_SUPPORTED,mkdir:NOT_SUPPORTED,move:NOT_SUPPORTED,read:NOT_SUPPORTED,remove:NOT_SUPPORTED,write:NOT_SUPPORTED},{resolve:function(a,b){a=String(a||"");b=String(b||"");if(b.indexOf("/")==0){var c=""}else{c=a.replace(_8,"")}c+=b;while(_7.test(c)){c=c.replace(_7,"")}return c}});var Directory=Collection.extend({sort:function(){return this.base(function(a,b,c,d){if(a.isDirectory!=b.isDirectory){return a.isDirectory?-1:1}else{return c<d?-1:1}})}},{Item:{constructor:function(a,b,c){this.name=a+"";this.isDirectory=!!b;this.size=b?0:c||0},name:"",isDirectory:false,size:0,toString:function(){return this.name}}});var LocalFileSystem=FileSystem.extend({constructor:function(a){this.path=LocalFileSystem.getPath();this.base(a)},backup:function(a,b){if(this.isFile(a)){if(!b)b=".backup";this.write(a+b,this.read(a))}},read:function(a){if(this.isDirectory(a)){return new LocalDirectory(this.makepath(a))}else{var b=new LocalFile(this.makepath(a));b.open(READ);var c=b.read();b.close();return c}},write:function(a,b){var c=new LocalFile(this.makepath(a));c.open(WRITE);c.write(b);c.close()},"@(ActiveXObject)":{copy:function(a,b){_2(this.isDirectory(a)?"CopyFolder":"CopyFile",this.makepath(a),this.makepath(b),true)},exists:function(a){return this.isFile(a)||this.isDirectory(a)},isFile:function(a){return _2("FileExists",this.makepath(a))},isDirectory:function(a){return _2("FolderExists",this.makepath(a))},mkdir:function(a){_2("CreateFolder",this.makepath(a))},move:function(a,b){_2(this.isDirectory(a)?"MoveFolder":"MoveFile",this.makepath(a),this.makepath(b))},remove:function(a){if(this.isFile(a)){_2("DeleteFile",this.makepath(a))}else if(this.isDirectory(a)){_2("DeleteFolder",this.makepath(a))}}},"@(Components)":{copy:function(a,b){var c=_0(this.makepath(a));var d=_0(this.makepath(b));c.copyTo(d.parent,d.leafName)},exists:function(a){return _0(this.makepath(a)).exists()},isFile:function(a){var b=_0(this.makepath(a));return b.exists()&&b.isFile()},isDirectory:function(a){var b=_0(this.makepath(a));return b.exists()&&b.isDirectory()},mkdir:function(a){_0(this.makepath(a)).create(1)},move:function(a,b){var c=_0(this.makepath(a));var d=_0(this.makepath(b));c.moveTo(d.parent,d.leafName)},remove:function(a){_0(this.makepath(a)).remove(false)}},"@(java && !global.Components)":{exists:function(a){return _1(this.makepath(a)).exists()},isFile:function(a){return _1(this.makepath(a)).isFile()},isDirectory:function(a){return _1(this.makepath(a)).isDirectory()},mkdir:function(a){_1(this.makepath(a)).mkdir()},move:function(a,b){var c=_1(this.makepath(a));var d=_1(this.makepath(b));c.renameTo(d)},remove:function(a){_1(this.makepath(a))["delete"]()}}},{init:function(){forEach.csv("copy,move",function(d){extend(this,d,function(a,b,c){assert(this.exists(a),"File does not exist: "+a);if(this.exists(b)){if(c){this.remove(b)}else{throw new Error("File already exists: "+b);}}this.base(a,b)})},this.prototype)},"@(Components)":{init:function(){this.base();XPCOM.privelegedObject(this.prototype)}},fromNativePath:I,toNativePath:I,getPath:K("/"),"@(global.java.io.File.separator=='\\\\')":_6,"@(jscript)":_6,"@win(32|64)":_6,"@(java)":{getPath:function(){return this.fromNativePath(new java.io.File("").getAbsolutePath())}},"@(ActiveXObject)":{getPath:function(){var a=new ActiveXObject("Scripting.FileSystemObject");return this.fromNativePath(a.GetFolder(".").path)}},"@(location)":{getPath:function(){return decodeURIComponent(location.pathname.replace(_8,""))}},"@(true)":{getPath:function(){var a=this.base();this.getPath=K(a);return a}}});var LocalDirectory=Directory.extend({"@(ActiveXObject)":{constructor:function(d){this.base();if(typeof d=="string"){var e=_2("GetFolder",d);forEach([e.SubFolders,e.Files],function(a){var b=new Enumerator(a);while(!b.atEnd()){var c=b.item();this.put(c.Name,c);b.moveNext()}},this)}}},"@(Components)":{constructor:function(a){this.base();if(typeof a=="string"){var b=_0(a);var c=b.directoryEntries;var d=c.QueryInterface(Components.interfaces.nsIDirectoryEnumerator);while(d.hasMoreElements()){b=d.nextFile;this.put(b.leafName,b)}}}},"@(java && !global.Components)":{constructor:function(a){this.base();if(typeof a=="string"){var b=_1(a);var c=b.list();for(var d=0;d<c.length;d++){b=new java.io.File(c[d]);this.put(b.getName(),b)}}}}},{"@(ActiveXObject)":{create:function(a,b){return new this.Item(a,b.Type|16,b.Size)}},"@(Components)":{create:function(a,b){return new this.Item(a,b.isDirectory(),b.fileSize)}},"@(java && !global.Components)":{create:function(a,b){return new this.Item(a,b.isDirectory(),b.length())}}});var LocalFile=Base.extend({constructor:function(a){this.toString=K(FileSystem.resolve(LocalFileSystem.getPath(),a))},close:_5,open:NOT_SUPPORTED,read:_5,write:_5,"@(ActiveXObject)":{open:function(b){var c=LocalFileSystem.toNativePath(this);var d=new ActiveXObject("Scripting.FileSystemObject");switch(b){case READ:assert(d.FileExists(c),"File does not exist: "+this);var e=d.OpenTextFile(c,1);this.read=function(){return e.ReadAll()};break;case WRITE:e=d.OpenTextFile(c,2,-1,0);this.write=function(a){e.Write(a||"")};break}this.close=function(){e.Close();delete this.read;delete this.write;delete this.close}}},"@(Components)":{open:function(b){var c=_0(this);switch(b){case READ:assert(c.exists(),"File does not exist: "+this);var d=XPCOM.createObject("network/file-input-stream;1","nsIFileInputStream");d.init(c,0x01,00004,null);var e=XPCOM.createObject("scriptableinputstream;1","nsIScriptableInputStream");e.init(d);this.read=function(){return e.read(e.available())};break;case WRITE:if(!c.exists())c.create(0,0664);e=XPCOM.createObject("network/file-output-stream;1","nsIFileOutputStream");e.init(c,0x20|0x02,00004,null);this.write=function(a){if(a==null)a="";e.write(a,a.length)};break}this.close=function(){if(b==WRITE)e.flush();e.close();delete this.read;delete this.write;delete this.close}}},"@(java && !global.Components)":{open:function(d){var e=LocalFileSystem.toNativePath(this);var f=java.io;switch(d){case READ:var h=_1(this);assert(h.exists(),"File does not exist: "+this);var g=new f.BufferedReader(new f.FileReader(e));this.read=function(){var a=[],b,c=0;while((b=g.readLine())!=null){a[c++]=b}return a.join("\r\n")};break;case WRITE:assert(!global.navigator,"Cannot write to local files with this browser.");g=new f.PrintStream(new f.FileOutputStream(e));this.write=function(a){g.print(a||"")};break}this.close=function(){g.close();delete this.read;delete this.write;delete this.close}}}});eval(this.exports)};