| 2 | | |
| 3 | | function File (path) { |
| 4 | | if (this instanceof arguments.callee) { |
| 5 | | if (typeof path == "string") { |
| 6 | | this.file = Components.classes["@mozilla.org/file/local;1"] |
| 7 | | .createInstance(Components.interfaces.nsILocalFile); |
| 8 | | this.file.initWithPath(path); |
| 9 | | } else { |
| 10 | | this.file = path; |
| 11 | | } |
| 12 | | this.charset = "UTF-8"; |
| 13 | | } else { |
| 14 | | return new File(path); |
| 15 | | } |
| 16 | | } |
| 17 | | File.prototype = { |
| 18 | | get path () { |
| 19 | | return this.file.path; |
| 20 | | }, |
| 21 | | |
| 22 | | get lastModifiedTime () { |
| 23 | | return this.file.lastModifiedTime; |
| 24 | | }, |
| 25 | | |
| 26 | | read : function (cb) { |
| 27 | | if (!cb) cb = function (i) { |
| 28 | | var ret = []; |
| 29 | | var str = {}; |
| 30 | | while (i.readString(4096, str) != 0) { |
| 31 | | ret.push(str.value); |
| 32 | | } |
| 33 | | return ret.join(""); |
| 34 | | }; |
| 35 | | var fistream = Components.classes["@mozilla.org/network/file-input-stream;1"] |
| 36 | | .createInstance(Components.interfaces.nsIFileInputStream); |
| 37 | | var cistream = Components.classes["@mozilla.org/intl/converter-input-stream;1"] |
| 38 | | .createInstance(Components.interfaces.nsIConverterInputStream); |
| 39 | | fistream.init(this.file, 0x01, 0444, 0); |
| 40 | | cistream.init(fistream, this.charset, 1024, Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); |
| 41 | | var ret = cb.call(this, cistream, fistream); |
| 42 | | cistream.close(); |
| 43 | | fistream.close(); |
| 44 | | return ret; |
| 45 | | }, |
| 46 | | write : function (cb) { |
| 47 | | if (typeof cb == "string") { |
| 48 | | var _str = cb; |
| 49 | | cb = function (i) { |
| 50 | | i.writeString(_str); |
| 51 | | return this; |
| 52 | | }; |
| 53 | | } |
| 54 | | var fostream = Components.classes["@mozilla.org/network/file-output-stream;1"] |
| 55 | | .createInstance(Components.interfaces.nsIFileOutputStream); |
| 56 | | var costream = Components.classes["@mozilla.org/intl/converter-output-stream;1"] |
| 57 | | .createInstance(Components.interfaces.nsIConverterOutputStream); |
| 58 | | fostream.init(this.file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate |
| 59 | | costream.init(fostream, this.charset, 4096, 0x0000); |
| 60 | | var ret = cb.call(this, costream, fostream); |
| 61 | | costream.close(); |
| 62 | | fostream.close(); |
| 63 | | return ret; |
| 64 | | } |
| 65 | | }; |
| 66 | | File.TempFile = function (prefix, suffix) { |
| 67 | | var name = [prefix, Math.floor(Math.random() * 0xffffff).toString(16), suffix].join("."); |
| 68 | | var file = Components.classes["@mozilla.org/file/directory_service;1"] |
| 69 | | .getService(Components.interfaces.nsIProperties) |
| 70 | | .get("TmpD", Components.interfaces.nsIFile); |
| 71 | | file.append(name); |
| 72 | | file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0664); |
| 73 | | |
| 74 | | Components.classes["@mozilla.org/uriloader/external-helper-app-service;1"] |
| 75 | | .getService(Components.interfaces.nsPIExternalAppLauncher) |
| 76 | | .deleteTemporaryFileOnExit(file); |
| 77 | | |
| 78 | | return File(file); |
| 79 | | } |