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 | var StringIO, _complain_ifclosed, _test, module_root;
|
45 |
|
46 | _complain_ifclosed = function(closed) {
|
47 | if (closed) {
|
48 | throw new Error('I/O operation on closed file');
|
49 | }
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | StringIO = (function() {
|
60 | function StringIO(buf) {
|
61 | if (buf == null) {
|
62 | buf = '';
|
63 | }
|
64 | this.buf = '' + buf;
|
65 | this.length = this.buf.length;
|
66 | this.buflist = [];
|
67 | this.pos = 0;
|
68 | this.closed = false;
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | StringIO.prototype.close = function() {
|
75 | if (!this.closed) {
|
76 | this.closed = true;
|
77 | delete this.buf;
|
78 | delete this.pos;
|
79 | }
|
80 | };
|
81 |
|
82 | StringIO.prototype._flush_buflist = function() {
|
83 | this.buf += this.buflist.join('');
|
84 | return this.buflist = [];
|
85 | };
|
86 |
|
87 |
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | StringIO.prototype.seek = function(pos, mode) {
|
96 | if (mode == null) {
|
97 | mode = 0;
|
98 | }
|
99 | _complain_ifclosed(this.closed);
|
100 | if (this.buflist.length) {
|
101 | this._flush_buflist();
|
102 | }
|
103 | if (mode === 1) {
|
104 | pos += this.pos;
|
105 | } else if (mode === 2) {
|
106 | pos += this.length;
|
107 | }
|
108 | this.pos = Math.max(0, pos);
|
109 | };
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | StringIO.prototype.tell = function() {
|
115 | _complain_ifclosed(this.closed);
|
116 | return this.pos;
|
117 | };
|
118 |
|
119 |
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | StringIO.prototype.read = function(n) {
|
128 | var newpos, r;
|
129 | if (n == null) {
|
130 | n = -1;
|
131 | }
|
132 | _complain_ifclosed(this.closed);
|
133 | if (this.buflist.length) {
|
134 | this._flush_buflist();
|
135 | }
|
136 | if (n < 0) {
|
137 | newpos = this.length;
|
138 | } else {
|
139 | newpos = Math.min(this.pos + n, this.length);
|
140 | }
|
141 | r = this.buf.slice(this.pos, newpos);
|
142 | this.pos = newpos;
|
143 | return r;
|
144 | };
|
145 |
|
146 |
|
147 | |
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | StringIO.prototype.readline = function(length) {
|
157 | var i, newpos, r;
|
158 | if (length == null) {
|
159 | length = null;
|
160 | }
|
161 | _complain_ifclosed(this.closed);
|
162 | if (this.buflist.length) {
|
163 | this._flush_buflist();
|
164 | }
|
165 | i = this.buf.indexOf('\n', this.pos);
|
166 | if (i < 0) {
|
167 | newpos = this.length;
|
168 | } else {
|
169 | newpos = i + 1;
|
170 | }
|
171 | if ((length != null) && this.pos + length < newpos) {
|
172 | newpos = this.pos + length;
|
173 | }
|
174 | r = this.buf.slice(this.pos, newpos);
|
175 | this.pos = newpos;
|
176 | return r;
|
177 | };
|
178 |
|
179 |
|
180 | |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | StringIO.prototype.readlines = function(sizehint) {
|
188 | var line, lines, total;
|
189 | if (sizehint == null) {
|
190 | sizehint = 0;
|
191 | }
|
192 | total = 0;
|
193 | lines = [];
|
194 | line = this.readline();
|
195 | while (line) {
|
196 | lines.push(line);
|
197 | total += line.length;
|
198 | if ((0 < sizehint && sizehint <= total)) {
|
199 | break;
|
200 | }
|
201 | line = this.readline();
|
202 | }
|
203 | return lines;
|
204 | };
|
205 |
|
206 |
|
207 | |
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | StringIO.prototype.truncate = function(size) {
|
217 | if (size == null) {
|
218 | size = null;
|
219 | }
|
220 | _complain_ifclosed(this.closed);
|
221 | if (size == null) {
|
222 | size = this.pos;
|
223 | } else if (size < 0) {
|
224 | throw new Error('Negative size not allowed');
|
225 | } else if (size < this.pos) {
|
226 | this.pos = size;
|
227 | }
|
228 | this.buf = this.getvalue().slice(0, size);
|
229 | this.length = size;
|
230 | };
|
231 |
|
232 |
|
233 | |
234 |
|
235 |
|
236 |
|
237 | StringIO.prototype.write = function(s) {
|
238 | var newpos, null_bytes, slen, spos;
|
239 | _complain_ifclosed(this.closed);
|
240 | if (!s) {
|
241 | return;
|
242 | }
|
243 | if (typeof s !== 'string') {
|
244 | s = s.toString();
|
245 | }
|
246 | spos = this.pos;
|
247 | slen = this.length;
|
248 | if (spos === slen) {
|
249 | this.buflist.push(s);
|
250 | this.length = this.pos = spos + s.length;
|
251 | return;
|
252 | }
|
253 | if (spos > slen) {
|
254 | null_bytes = (Array(spos - slen + 1)).join('\x00');
|
255 | this.buflist.push(null_bytes);
|
256 | slen = spos;
|
257 | }
|
258 | newpos = spos + s.length;
|
259 | if (spos < slen) {
|
260 | if (this.buflist.length) {
|
261 | this._flush_buflist();
|
262 | }
|
263 | this.buflist.push(this.buf.slice(0, spos), s, this.buf.slice(newpos));
|
264 | this.buf = '';
|
265 | if (newpos > slen) {
|
266 | slen = newpos;
|
267 | }
|
268 | } else {
|
269 | this.buflist.push(s);
|
270 | slen = newpos;
|
271 | }
|
272 | this.length = slen;
|
273 | this.pos = newpos;
|
274 | };
|
275 |
|
276 |
|
277 | |
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | StringIO.prototype.writelines = function(array) {
|
285 | var j, len, line;
|
286 | for (j = 0, len = array.length; j < len; j++) {
|
287 | line = array[j];
|
288 | this.write(line);
|
289 | }
|
290 | };
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | StringIO.prototype.flush = function() {
|
296 | _complain_ifclosed(this.closed);
|
297 | };
|
298 |
|
299 |
|
300 | |
301 |
|
302 |
|
303 |
|
304 | StringIO.prototype.getvalue = function() {
|
305 | if (this.buflist.length) {
|
306 | this._flush_buflist();
|
307 | }
|
308 | return this.buf;
|
309 | };
|
310 |
|
311 | return StringIO;
|
312 |
|
313 | })();
|
314 |
|
315 | module_root = typeof exports !== "undefined" && exports !== null ? exports : typeof window !== "undefined" && window !== null ? window : this;
|
316 |
|
317 | module_root.StringIO = StringIO;
|
318 |
|
319 | _test = function() {
|
320 | var f, j, len, length, line, line2, lines, list, print, ref;
|
321 | print = function() {
|
322 | return console.log.apply(console, arguments);
|
323 | };
|
324 | lines = ['This is a test,\n', 'Blah blah blah,\n', 'Wow does this work?\n', 'Okay, here are some lines\n', 'of text.\n'];
|
325 | f = new StringIO;
|
326 | ref = lines.slice(0, -2);
|
327 | for (j = 0, len = ref.length; j < len; j++) {
|
328 | line = ref[j];
|
329 | f.write(line);
|
330 | }
|
331 | f.writelines(lines.slice(-2));
|
332 | if (f.getvalue() !== lines.join('')) {
|
333 | throw new Error('write failed');
|
334 | }
|
335 | length = f.tell();
|
336 | print('File length =', length);
|
337 | f.seek(lines[0].length);
|
338 | f.write(lines[1]);
|
339 | f.seek(0);
|
340 | print("First line = " + (f.readline()));
|
341 | print("Position = " + (f.tell()));
|
342 | line = f.readline();
|
343 | print("Second line = " + line);
|
344 | f.seek(-line.length, 1);
|
345 | line2 = f.read(line.length);
|
346 | if (line !== line2) {
|
347 | throw new Error('bad result after seek back');
|
348 | }
|
349 | f.seek(-line2.length, 1);
|
350 | list = f.readlines();
|
351 | line = list[list.length - 1];
|
352 | f.seek(f.tell() - line.length);
|
353 | line2 = f.read();
|
354 | if (line !== line2) {
|
355 | throw new Error('bad result after seek back from EOF');
|
356 | }
|
357 | print("Read " + list.length + " more lines");
|
358 | print("File length = " + (f.tell()));
|
359 | if (f.tell() !== length) {
|
360 | throw new Error('bad length');
|
361 | }
|
362 | f.truncate((length / 2) | 0);
|
363 | f.seek(0, 2);
|
364 | print("Truncated length = " + (f.tell()));
|
365 | if (f.tell() !== ((length / 2) | 0)) {
|
366 | throw new Error('truncate did not adjust length');
|
367 | }
|
368 | return f.close();
|
369 | }; |
\ | No newline at end of file |