Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/utils/platforms/xulrunner/lib/io-platform.js
blob: 60103e4e99acb00841876194b44ff95d0575d500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const Cc = Components.classes;
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;
};



exports.TextInputStream = function (raw, lineBuffering, buffering, charset, options) {
    var stream;

//    if (charset === undefined) {
//    if (buffering === undefined)

    stream = raw.inputStream;
    stream.QueryInterface(Ci.nsILineInputStream);

    var self = this;

    self.readLine = function () {
//        var line = stream.readLine();
//        if (line === null)
//            return '';
//        return String(line) + "\n";
    };

    self.iter = function () {
        return self;
    };

    self.next = function () {
//        var line = stream.readLine();
//        if (line === null)
//            throw new Error("StopIteration");
//        return line;
    };

    self.input = function () {
        throw "NYI";
    };

    self.readLines = function () {
        var line = {},
            lines = [],
            hasmore;
        do {
          hasmore = stream.readLine(line);
          lines.push(line.value);
        } while(hasmore);
        return lines;
    };

    self.read = function () {
        return self.readLines().join("\n");
    };

    self.readInto = function (buffer) {
        throw "NYI";
    };

    self.close = function () {
        stream.close();
    };
};

exports.TextIOWrapper = function (raw, mode, lineBuffering, buffering, charset, options) {
    if (mode.update) {
        return new exports.TextIOStream(raw, lineBuffering, buffering, charset, options);
    } else if (mode.write || mode.append) {
        return new exports.TextOutputStream(raw, lineBuffering, buffering, charset, options);
    } else if (mode.read) {
        return new exports.TextInputStream(raw, lineBuffering, buffering, charset, options);
    } else {
        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;
};