2 //////////// Stolen from venkman
4 const PERM_IWOTH = 00002; /* write permission, others */
5 const PERM_IWGRP = 00020; /* write permission, group */
7 const MODE_RDONLY = 0x01;
8 const MODE_WRONLY = 0x02;
9 const MODE_RDWR = 0x04;
10 const MODE_CREATE = 0x08;
11 const MODE_APPEND = 0x10;
12 const MODE_TRUNCATE = 0x20;
13 const MODE_SYNC = 0x40;
14 const MODE_EXCL = 0x80;
17 // XXX: is is necessary to fully qualify fopen as conkeror.fopen?
19 conkeror.fopen = function (path, mode, perms, tmp)
21 return new LocalFile(path, mode, perms, tmp);
24 function LocalFile(file, mode, perms, tmp)
26 const classes = Components.classes;
27 const interfaces = Components.interfaces;
29 const LOCALFILE_CTRID = "@mozilla.org/file/local;1";
30 const FILEIN_CTRID = "@mozilla.org/network/file-input-stream;1";
31 const FILEOUT_CTRID = "@mozilla.org/network/file-output-stream;1";
32 const SCRIPTSTREAM_CTRID = "@mozilla.org/scriptableinputstream;1";
34 const nsIFile = interfaces.nsIFile;
35 const nsILocalFile = interfaces.nsILocalFile;
36 const nsIFileOutputStream = interfaces.nsIFileOutputStream;
37 const nsIFileInputStream = interfaces.nsIFileInputStream;
38 const nsIScriptableInputStream = interfaces.nsIScriptableInputStream;
40 if (typeof perms == "undefined")
41 perms = 0666 & ~(PERM_IWOTH | PERM_IWGRP);
43 if (typeof mode == "string")
48 mode = MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE;
51 mode = MODE_WRONLY | MODE_CREATE | MODE_APPEND;
57 throw "Invalid mode ``" + mode + "''";
61 if (typeof file == "string")
63 this.localFile = classes[LOCALFILE_CTRID].createInstance(nsILocalFile);
64 this.localFile.initWithPath(file);
66 else if (file instanceof nsILocalFile)
68 this.localFile = file;
72 throw "bad type for argument |file|.";
75 this.path = this.localFile.path;
77 if (mode & (MODE_WRONLY | MODE_RDWR))
80 classes[FILEOUT_CTRID].createInstance(nsIFileOutputStream);
81 this.outputStream.init(this.localFile, mode, perms, 0);
84 if (mode & (MODE_RDONLY | MODE_RDWR))
86 var is = classes[FILEIN_CTRID].createInstance(nsIFileInputStream);
87 is.init(this.localFile, mode, perms, tmp);
89 classes[SCRIPTSTREAM_CTRID].createInstance(nsIScriptableInputStream);
90 this.inputStream.init(is);
95 LocalFile.prototype.write =
96 function fo_write(buf)
98 if (!("outputStream" in this))
99 throw "file not open for writing.";
101 return this.outputStream.write(buf, buf.length);
104 LocalFile.prototype.read =
105 function fo_read(max)
107 if (!("inputStream" in this))
108 throw "file not open for reading.";
110 var av = this.inputStream.available();
111 if (typeof max == "undefined")
117 var rv = this.inputStream.read(max);
121 LocalFile.prototype.close =
124 if ("outputStream" in this)
125 this.outputStream.close();
126 if ("inputStream" in this)
127 this.inputStream.close();
130 LocalFile.prototype.flush =
133 return this.outputStream.flush();