diff options
Diffstat (limited to 'utils/platforms/xulrunner/lib/io-platform.js')
-rwxr-xr-x | utils/platforms/xulrunner/lib/io-platform.js | 117 |
1 files changed, 111 insertions, 6 deletions
diff --git a/utils/platforms/xulrunner/lib/io-platform.js b/utils/platforms/xulrunner/lib/io-platform.js index 5c2ce62..60103e4 100755 --- a/utils/platforms/xulrunner/lib/io-platform.js +++ b/utils/platforms/xulrunner/lib/io-platform.js @@ -4,25 +4,25 @@ const Ci = Components.interfaces; var IO = exports.IO = function(inputStream, outputStream) { this.inputStream = inputStream; this.outputStream = outputStream; -} +}; IO.prototype.read = function(length) { return this.inputStream(length); -} +}; IO.prototype.write = function(object) { this.outputStream(object); -} +}; IO.prototype.flush = function() { -} +}; IO.prototype.close = function() { -} +}; IO.prototype.isatty = function () { return false; -} +}; @@ -94,3 +94,108 @@ exports.TextIOWrapper = function (raw, mode, lineBuffering, buffering, charset, throw new Error("file must be opened for read, write, or append mode."); } }; + + + +var StringIO = exports.StringIO = function (initial) { + var buffer = []; + if (initial) { + buffer = buffer.concat(initial.join("")); + } + + function length() { + return buffer.length; + } + + function read(length) { + var result; + + if (arguments.length == 0) { + result = buffer.join(""); + buffer = []; + return result; + } else { + if (!length || length < 1) + length = 1024; + length = Math.min(buffer.length, length); + result = buffer.slice(0, length).join(""); + buffer = []; + return result; + } + } + + function write(text) { + buffer = buffer.concat(text.split("")); + return self; + } + + function copy(output) { + output.write(read()).flush(); + return self; + } + + function next() { + var pos, result; + if (buffer.length === 0) { throw StopIteration; } + pos = buffer.indexOf("\n"); + if (pos === -1) { pos = buffer.length; } + result = read(pos); + read(1); + return result; + } + + var self = { + get length() { + return length(); + }, + read: read, + write: write, + copy: copy, + close: function () { + return self; + }, + flush: function () { + return self; + }, + iterator: function () { + return self; + }, + forEach: function (block) { + while (true) { + try { + block.call(this, next()); + } catch (exception) { + if (exception instanceof StopIteration) + break; + throw exception; + } + } + }, + readLine: function () { + var pos = buffer.indexOf("\n"); + if (pos === -1) { pos = buffer.length; } + return read(pos + 1); + }, + next: next, + print: function (line) { + return write(line + "\n").flush(); + }, + toString: function() { + return buffer.join(""); + }, + substring: function () { + var string = buffer.join(""); + return string.substring.apply(string, arguments); + }, + slice: function () { + var string = buffer.join(""); + return string.slice.apply(string, arguments); + }, + substr: function () { + var string = buffer.join(""); + return string.substr.apply(string, arguments); + } + }; + return self; +}; + |