utils.js: add function string_hashmap.for_each_value
[conkeror.git] / modules / localfile.js
blob9971d0106bf831e1479392ed60f907a19ca4b6ad
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")
44     {
45         switch (mode)
46         {
47             case ">":
48                 mode = MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE;
49                 break;
50             case ">>":
51                 mode = MODE_WRONLY | MODE_CREATE | MODE_APPEND;
52                 break;
53             case "<":
54                 mode = MODE_RDONLY;
55                 break;
56             default:
57                 throw "Invalid mode ``" + mode + "''";
58         }
59     }
61     if (typeof file == "string")
62     {
63         this.localFile = classes[LOCALFILE_CTRID].createInstance(nsILocalFile);
64         this.localFile.initWithPath(file);
65     }
66     else if (file instanceof nsILocalFile)
67     {
68         this.localFile = file;
69     }
70     else
71     {
72         throw "bad type for argument |file|.";
73     }
75     this.path = this.localFile.path;
77     if (mode & (MODE_WRONLY | MODE_RDWR))
78     {
79         this.outputStream =
80             classes[FILEOUT_CTRID].createInstance(nsIFileOutputStream);
81         this.outputStream.init(this.localFile, mode, perms, 0);
82     }
84     if (mode & (MODE_RDONLY | MODE_RDWR))
85     {
86         var is = classes[FILEIN_CTRID].createInstance(nsIFileInputStream);
87         is.init(this.localFile, mode, perms, tmp);
88         this.inputStream =
89             classes[SCRIPTSTREAM_CTRID].createInstance(nsIScriptableInputStream);
90         this.inputStream.init(is);
91     }
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")
112         max = av;
114     if (!av)
115         return null;
117     var rv = this.inputStream.read(max);
118     return rv;
121 LocalFile.prototype.close =
122 function fo_close()
124     if ("outputStream" in this)
125         this.outputStream.close();
126     if ("inputStream" in this)
127         this.inputStream.close();
130 LocalFile.prototype.flush =
131 function fo_close()
133     return this.outputStream.flush();