| 1 |
/* |
| 2 |
* Copyright (C)2005-2022 Haxe Foundation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
type file |
| 24 |
|
| 25 |
type input { |
| 26 |
mutable in_read : void -> char; |
| 27 |
mutable in_input : string -> int -> int -> int; |
| 28 |
mutable in_close : void -> void; |
| 29 |
} |
| 30 |
|
| 31 |
type output { |
| 32 |
mutable out_write : char -> void; |
| 33 |
mutable out_output : string -> int -> int -> int; |
| 34 |
mutable out_close : void -> void; |
| 35 |
mutable out_flush : void -> void; |
| 36 |
} |
| 37 |
|
| 38 |
exception Overflow : string; |
| 39 |
exception Eof; |
| 40 |
exception Closed; |
| 41 |
exception Blocked; |
| 42 |
|
| 43 |
/* ---------------------------- |
| 44 |
RAW FILE API |
| 45 |
------------------------------- */ |
| 46 |
|
| 47 |
neko(" |
| 48 |
@load = function(name,nargs) { |
| 49 |
return $loader.loadprim('std@'+name,nargs); |
| 50 |
} |
| 51 |
"); |
| 52 |
|
| 53 |
var file_open : string -> string -> file = neko("@load('file_open',2)"); |
| 54 |
var file_contents : string -> string = neko("@load('file_contents',1)"); |
| 55 |
var file_close : file -> void = neko("@load('file_close',1)"); |
| 56 |
var file_read : file -> string -> int -> int -> int = neko("@load('file_read',4)"); |
| 57 |
var file_read_char : file -> char = neko("@load('file_read_char',1)"); |
| 58 |
var file_write : file -> string -> int -> int -> int = neko("@load('file_write',4)"); |
| 59 |
var file_write_char : file -> char -> void = neko("@load('file_write_char',2)"); |
| 60 |
var file_flush : file -> void = neko("@load('file_flush',1)"); |
| 61 |
|
| 62 |
function file_input(f) { |
| 63 |
{ |
| 64 |
in_read = function() { try file_read_char(f) catch { _ -> throw Eof } }; |
| 65 |
in_input = function(s,p,l) { try file_read f s p l catch { _ -> throw Eof } }; |
| 66 |
in_close = function() { file_close(f) }; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
function read_file(path,bin) { |
| 71 |
var f = file_open(path,if bin then "rb" else "r"); |
| 72 |
file_input f |
| 73 |
} |
| 74 |
|
| 75 |
function read_string(str) { |
| 76 |
var p = &0; |
| 77 |
var len = String.length str; |
| 78 |
{ |
| 79 |
in_read = function() { |
| 80 |
if *p == len then throw Eof; |
| 81 |
var c = String.get str (*p); |
| 82 |
p := *p + 1; |
| 83 |
c |
| 84 |
}; |
| 85 |
in_input = function(s,pp,l) { |
| 86 |
if *p == len then throw Eof; |
| 87 |
var l = min l (len - *p); |
| 88 |
String.blit s pp str (*p) l; |
| 89 |
p := *p + l; |
| 90 |
l |
| 91 |
}; |
| 92 |
in_close = function() { |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
function file_output(f) { |
| 98 |
{ |
| 99 |
out_write = file_write_char f; |
| 100 |
out_output = file_write f; |
| 101 |
out_close = function() { file_close(f) }; |
| 102 |
out_flush = function() { file_flush(f) }; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
function write_file(path,bin) { |
| 107 |
var f = file_open(path,if bin then "wb" else "w"); |
| 108 |
file_output f |
| 109 |
} |
| 110 |
|
| 111 |
function write_string() { |
| 112 |
var b = Buffer.create(); |
| 113 |
var o = { |
| 114 |
out_write = Buffer.add_char b; |
| 115 |
out_output = function(s,pp,l) { |
| 116 |
Buffer.add_sub b s pp l; |
| 117 |
l |
| 118 |
}; |
| 119 |
out_close = function() { |
| 120 |
}; |
| 121 |
out_flush = function() { |
| 122 |
}; |
| 123 |
}; |
| 124 |
(o , function() { Buffer.string b }) |
| 125 |
} |
| 126 |
|
| 127 |
var file_stdin : file = neko("@load('file_stdin',0)()"); |
| 128 |
var file_stdout : file = neko("@load('file_stdout',0)()"); |
| 129 |
var file_stderr : file = neko("@load('file_stderr',0)()"); |
| 130 |
|
| 131 |
var stdin : input = file_input file_stdin; |
| 132 |
var stdout : output = file_output file_stdout; |
| 133 |
var stderr : output = file_output file_stderr; |
| 134 |
|
| 135 |
|
| 136 |
/* ---------------------------- |
| 137 |
INPUT API |
| 138 |
------------------------------- */ |
| 139 |
|
| 140 |
function create_in(read,input,close) { |
| 141 |
{ |
| 142 |
in_read = read; |
| 143 |
in_input = input; |
| 144 |
in_close = close; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
function read_char(i) { |
| 149 |
i.in_read() |
| 150 |
} |
| 151 |
|
| 152 |
function read_byte(i) { |
| 153 |
ord i.in_read() |
| 154 |
} |
| 155 |
|
| 156 |
function input(i,s,p,n) { |
| 157 |
var len = String.length s; |
| 158 |
if p < 0 || n < 0 || p > len || p + n > len then invalid_arg(); |
| 159 |
i.in_input s p n |
| 160 |
} |
| 161 |
|
| 162 |
function read(i,n) { |
| 163 |
if n < 0 then invalid_arg(); |
| 164 |
if n == 0 then |
| 165 |
"" |
| 166 |
else { |
| 167 |
var s = String.create n; |
| 168 |
var p = &0; |
| 169 |
var len = &n; |
| 170 |
while *len > 0 { |
| 171 |
var n = i.in_input s (*p) (*len); |
| 172 |
if n == 0 then throw Blocked; |
| 173 |
p := *p + n; |
| 174 |
len := *len - n; |
| 175 |
}; |
| 176 |
s |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
function read_buf(i,n) { |
| 181 |
if n < 0 then invalid_arg(); |
| 182 |
if n == 0 then |
| 183 |
"" |
| 184 |
else { |
| 185 |
var s = String.create n; |
| 186 |
var p = &0; |
| 187 |
var len = &n; |
| 188 |
try { |
| 189 |
while *len > 0 { |
| 190 |
var n = i.in_input s (*p) (*len); |
| 191 |
if n == 0 then throw (if *p == 0 then Blocked else Eof); |
| 192 |
p := *p + n; |
| 193 |
len := *len - n; |
| 194 |
}; |
| 195 |
s |
| 196 |
} catch { |
| 197 |
Eof -> |
| 198 |
if *p == 0 then throw Eof; |
| 199 |
String.sub s 0 (*p) |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
function read_all(i : input) : string { |
| 205 |
var b = Buffer.create(); |
| 206 |
var maxlen = 4096; |
| 207 |
try { |
| 208 |
while true { |
| 209 |
Buffer.add b (read_buf i maxlen); |
| 210 |
} |
| 211 |
magic(); |
| 212 |
} catch { |
| 213 |
Eof -> Buffer.string b |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
function read_line(i : input) : string { |
| 218 |
var buf = Buffer.create(); |
| 219 |
try { |
| 220 |
while true { |
| 221 |
var ch = i.in_read(); |
| 222 |
if ch == '\n' then throw Exit; |
| 223 |
Buffer.add_char buf ch; |
| 224 |
} |
| 225 |
assert() |
| 226 |
} catch { |
| 227 |
Exit -> Buffer.string buf |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
function read_i32(i) : int { |
| 232 |
var ch1 = read_byte i; |
| 233 |
var ch2 = read_byte i; |
| 234 |
var ch3 = read_byte i; |
| 235 |
var ch4 = read_byte i; |
| 236 |
ch1 or (ch2 << 8) or (ch3 << 16) or (ch4 << 24) |
| 237 |
} |
| 238 |
|
| 239 |
function read_ui16(i) { |
| 240 |
var ch1 = read_byte i; |
| 241 |
var ch2 = read_byte i; |
| 242 |
ch1 or (ch2 << 8) |
| 243 |
} |
| 244 |
|
| 245 |
function read_ui24(i) { |
| 246 |
var ch1 = read_byte i; |
| 247 |
var ch2 = read_byte i; |
| 248 |
var ch3 = read_byte i; |
| 249 |
ch1 or (ch2 << 8) or (ch3 << 16) |
| 250 |
} |
| 251 |
|
| 252 |
function read_i16(i) { |
| 253 |
var ch1 = read_byte i; |
| 254 |
var ch2 = read_byte i; |
| 255 |
var n = ch1 or (ch2 << 8); |
| 256 |
if ch2 and 128 != 0 then |
| 257 |
n - 65536 |
| 258 |
else |
| 259 |
n |
| 260 |
} |
| 261 |
|
| 262 |
function close_in(i) { |
| 263 |
i.in_close(); |
| 264 |
i.in_read := function() { throw Closed }; |
| 265 |
i.in_input := function(s,p,l) { throw Closed }; |
| 266 |
i.in_close := function() { throw Closed }; |
| 267 |
} |
| 268 |
|
| 269 |
/* ---------------------------- |
| 270 |
OUTPUT API |
| 271 |
------------------------------- */ |
| 272 |
|
| 273 |
function create_out(write,output,flush,close) { |
| 274 |
{ |
| 275 |
out_write = write; |
| 276 |
out_output = output; |
| 277 |
out_flush = flush; |
| 278 |
out_close = close; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
function write_char(o : output, x : char) : void { |
| 283 |
o.out_write x |
| 284 |
} |
| 285 |
|
| 286 |
function write_byte(o : output, x : int) : void { |
| 287 |
o.out_write (chr (x and 255)) |
| 288 |
} |
| 289 |
|
| 290 |
function write_i8(o : output, x : int) : void { |
| 291 |
if x < 127 || x > 128 then invalid_arg(); |
| 292 |
write_byte o (x and 0xFF) |
| 293 |
} |
| 294 |
|
| 295 |
function output(o,s,p,n) { |
| 296 |
var len = String.length s; |
| 297 |
if p < 0 || n < 0 || p > len || p + n > len then invalid_arg(); |
| 298 |
o.out_output s p n |
| 299 |
} |
| 300 |
|
| 301 |
function write(o : output,x : string) : void { |
| 302 |
var p = &0; |
| 303 |
var len = &(String.length x); |
| 304 |
while *len > 0 { |
| 305 |
var n = o.out_output x (*p) (*len); |
| 306 |
if n == 0 then throw Blocked; |
| 307 |
p := *p + n; |
| 308 |
len := *len - n; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
function write_i32(o : output, x : int) : void { |
| 313 |
write_byte o x; |
| 314 |
write_byte o (x >> 8); |
| 315 |
write_byte o (x >> 16); |
| 316 |
write_byte o (x >>> 24) |
| 317 |
} |
| 318 |
|
| 319 |
function write_ui16(o : output, x : int) : void { |
| 320 |
if x < 0 || x > 0xFFFF then invalid_arg(); |
| 321 |
write_byte o x; |
| 322 |
write_byte o (x >> 8) |
| 323 |
} |
| 324 |
|
| 325 |
function write_i16(o : output, x : int) : void { |
| 326 |
if x < -0x7FFF || x > 0x7FFF then invalid_arg(); |
| 327 |
if x < 0 then |
| 328 |
write_ui16 o (65536 + x) |
| 329 |
else |
| 330 |
write_ui16 o x |
| 331 |
} |
| 332 |
|
| 333 |
function write_ui24(o : output, x : int) : void { |
| 334 |
if x < 0 || x > 0xFFFFFF then invalid_arg(); |
| 335 |
write_byte o x; |
| 336 |
write_byte o (x >> 8); |
| 337 |
write_byte o (x >> 16); |
| 338 |
} |
| 339 |
|
| 340 |
function flush(o) { |
| 341 |
o.out_flush(); |
| 342 |
} |
| 343 |
|
| 344 |
function close_out(o) { |
| 345 |
o.out_close(); |
| 346 |
o.out_flush := function() { throw Closed }; |
| 347 |
o.out_write := function(_) { throw Closed }; |
| 348 |
o.out_output := function(_,_,_) { throw Closed }; |
| 349 |
o.out_close := function() { throw Closed }; |
| 350 |
} |
| 351 |
|
| 352 |
function printf(o : output, fmt : 'a format, p : 'a ) : void { |
| 353 |
write o (sprintf fmt p) |
| 354 |
} |
| 355 |
|