1 const PERM_IWOTH = 00002; /* write permission, others */
2 const PERM_IWGRP = 00020; /* write permission, group */
4 const MODE_RDONLY = 0x01;
5 const MODE_WRONLY = 0x02;
6 const MODE_RDWR = 0x04;
7 const MODE_CREATE = 0x08;
8 const MODE_APPEND = 0x10;
9 const MODE_TRUNCATE = 0x20;
10 const MODE_SYNC = 0x40;
11 const MODE_EXCL = 0x80;
12 const MODE_OUTPUT = MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE;
13 const MODE_OUTPUT_APPEND = MODE_WRONLY | MODE_CREATE | MODE_APPEND;
14 const MODE_INPUT = MODE_RDONLY;
16 define_keywords("$charset");
17 function read_text_file(file)
19 keywords(arguments, $charset = "UTF-8");
21 var ifstream = null, icstream = null;
24 ifstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
25 icstream = Cc["@mozilla.org/intl/converter-input-stream;1"].createInstance(Ci.nsIConverterInputStream);
27 ifstream.init(file, -1, 0, 0);
28 const replacementChar = Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER;
29 icstream.init(ifstream, arguments.$charset, 4096, replacementChar); // 4096 bytes buffering
33 while (icstream.readString(4096, str) != 0)
44 define_keywords("$mode", "$perms", "$charset");
45 function write_text_file(file, buf)
47 keywords(arguments, $charset = "UTF-8", $perms = 0644, $mode = MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE);
49 var ofstream, ocstream;
51 ofstream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
52 ocstream = Cc["@mozilla.org/intl/converter-output-stream;1"].createInstance(Ci.nsIConverterOutputStream);
54 ofstream.init(file, arguments.$mode, arguments.$perms, 0);
55 ocstream.init(ofstream, arguments.$charset, 0, 0x0000);
56 ocstream.writeString(buf);
65 define_keywords("$mode", "$perms");
66 function write_binary_file(file, buf)
68 keywords(arguments, $perms = 0644, $mode = MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE);
69 var stream = null, bstream = null;
72 stream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
73 stream.init(file, arguments.$mode, arguments.$perms, 0);
75 bstream = binary_output_stream(stream);
76 bstream.writeBytes(buf, buf.length);
85 function get_file(path) {
86 var f = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
91 var thread_manager = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
93 function input_stream_async_wait(stream, callback, requested_count) {
94 stream = stream.QueryInterface(Ci.nsIAsyncInputStream);
95 var flags = (requested_count === false) ? Ci.nsIAsyncInputStream.WAIT_CLOSURE_ONLY : 0;
96 if (requested_count == null || requested_count == false)
98 stream.asyncWait({onInputStreamReady: callback}, flags, requested_count, thread_manager.mainThread);
101 function output_stream_async_wait(stream, callback, requested_count) {
102 stream = stream.QueryInterface(Ci.nsIAsyncOutputStream);
103 var flags = (requested_count === false) ? Ci.nsIAsyncOutputStream.WAIT_CLOSURE_ONLY : 0;
104 if (requested_count == null || requested_count == false)
106 stream.asyncWait({onOutputStreamReady: callback}, flags, requested_count, thread_manager.mainThread);
109 function binary_output_stream(stream) {
110 var s = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(Ci.nsIBinaryOutputStream);
111 s.setOutputStream(stream);
115 function binary_input_stream(stream) {
116 var s = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
117 s.setInputStream(stream);
121 // callback is called with a single argument, either true if the write succeeded, or false otherwise
122 function async_binary_write(stream, data, callback) {
123 function attempt_write() {
126 if (data.length == 0) {
131 var len = stream.write(data, data.length);
134 data = data.substring(len);
137 catch (e if (e instanceof Components.Exception) && e.result == Cr.NS_BASE_STREAM_WOULD_BLOCK) {}
142 output_stream_async_wait(stream, attempt_write, data.length);
148 * The `str' parameter should be a normal JavaScript string whose char codes specify Unicode code points.
149 * The return value is a byte string (all char codes are from 0 to 255) equal to the `str' encoded in the specified charset.
150 * If charset is not specified, it defaults to UTF-8.
152 function encode_string(str, charset) {
153 var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
154 converter.charset = charset || "UTF-8";
155 var output = converter.ConvertFromUnicode(str);
156 output += converter.Finish();
163 * The `bstr' parameter should be a byte string (all char codes are from 0 to 255).
164 * The return value is a normal JavaScript unicode sring equal to `bstr' decoded using the specified charset.
165 * If charset is not specified, it defaults to UTF-8.
167 function decode_string(bstr, charset) {
168 var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
169 converter.charset = charset || "UTF-8";
170 return converter.ConvertToUnicode(bstr);
173 function async_binary_string_writer(bstr) {
174 return function (stream) {
175 async_binary_write(stream, bstr);
179 function async_binary_reader(callback) {
180 return function (stream) {
181 var bstream = binary_input_stream(stream);
184 let avail = stream.available();
186 callback(bstream.readBytes(avail));
188 input_stream_async_wait(stream, handler);
193 input_stream_async_wait(stream, handler);