/usr/share/julia/base/io.jl is in julia-common 0.4.7-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | # This file is a part of Julia. License is MIT: http://julialang.org/license
## core stream types ##
# the first argument to any IO MUST be a POINTER (to a JL_STREAM) or using show on it will cause memory corruption
# Generic IO functions
## byte-order mark, ntoh & hton ##
const ENDIAN_BOM = reinterpret(UInt32,UInt8[1:4;])[1]
if ENDIAN_BOM == 0x01020304
ntoh(x) = x
hton(x) = x
ltoh(x) = bswap(x)
htol(x) = bswap(x)
elseif ENDIAN_BOM == 0x04030201
ntoh(x) = bswap(x)
hton(x) = bswap(x)
ltoh(x) = x
htol(x) = x
else
error("seriously? what is this machine?")
end
isreadonly(s) = isreadable(s) && !iswritable(s)
## binary I/O ##
write(io::IO, x) = throw(MethodError(write, (io, x)))
function write(io::IO, xs...)
local written::Int = 0
for x in xs
written += write(io, x)
end
written
end
if ENDIAN_BOM == 0x01020304
function write(s::IO, x::Integer)
sz = sizeof(x)
local written::Int = 0
for n = sz:-1:1
written += write(s, (x>>>((n-1)<<3))%UInt8)
end
return written
end
else
function write(s::IO, x::Integer)
sz = sizeof(x)
local written::Int = 0
for n = 1:sz
written += write(s, (x>>>((n-1)<<3))%UInt8)
end
return written
end
end
write(s::IO, x::Bool) = write(s, UInt8(x))
write(s::IO, x::Float16) = write(s, reinterpret(Int16,x))
write(s::IO, x::Float32) = write(s, reinterpret(Int32,x))
write(s::IO, x::Float64) = write(s, reinterpret(Int64,x))
write(to::IO, p::Ptr) = write(to, convert(UInt, p))
function write(s::IO, a::AbstractArray)
nb = 0
for i in eachindex(a)
nb += write(s, a[i])
end
return nb
end
function write(s::IO, ch::Char)
c = reinterpret(UInt32, ch)
if c < 0x80
return write(s, c%UInt8)
elseif c < 0x800
return (write(s, (( c >> 6 ) | 0xC0)%UInt8)) +
(write(s, (( c & 0x3F ) | 0x80)%UInt8))
elseif c < 0x10000
return (write(s, (( c >> 12 ) | 0xE0)%UInt8)) +
(write(s, (((c >> 6) & 0x3F ) | 0x80)%UInt8)) +
(write(s, (( c & 0x3F ) | 0x80)%UInt8))
elseif c < 0x110000
return (write(s, (( c >> 18 ) | 0xF0)%UInt8)) +
(write(s, (((c >> 12) & 0x3F ) | 0x80)%UInt8)) +
(write(s, (((c >> 6) & 0x3F ) | 0x80)%UInt8)) +
(write(s, (( c & 0x3F ) | 0x80)%UInt8))
else
return write(s, '\ufffd')
end
end
function write(s::IO, p::Ptr, n::Integer)
local written::Int = 0
for i=1:n
written += write(s, unsafe_load(p, i))
end
return written
end
function write(io::IO, s::Symbol)
pname = unsafe_convert(Ptr{UInt8}, s)
return write(io, pname, Int(ccall(:strlen, Csize_t, (Ptr{UInt8},), pname)))
end
read(s::IO, ::Type{Int8}) = reinterpret(Int8, read(s,UInt8))
function read{T <: Integer}(s::IO, ::Type{T})
x = zero(T)
for n = 1:sizeof(x)
x |= (convert(T,read(s,UInt8))<<((n-1)<<3))
end
return x
end
read(s::IO, ::Type{Bool}) = (read(s,UInt8)!=0)
read(s::IO, ::Type{Float16}) = box(Float16,unbox(Int16,read(s,Int16)))
read(s::IO, ::Type{Float32}) = box(Float32,unbox(Int32,read(s,Int32)))
read(s::IO, ::Type{Float64}) = box(Float64,unbox(Int64,read(s,Int64)))
read{T}(s::IO, ::Type{Ptr{T}}) = convert(Ptr{T}, read(s,UInt))
read{T}(s::IO, t::Type{T}, d1::Int, dims::Int...) = read(s, t, tuple(d1,dims...))
read{T}(s::IO, t::Type{T}, d1::Integer, dims::Integer...) =
read(s, t, convert(Tuple{Vararg{Int}},tuple(d1,dims...)))
read{T}(s::IO, ::Type{T}, dims::Dims) = read!(s, Array(T, dims))
function read!(s::IO, a::Vector{UInt8})
for i in 1:length(a)
a[i] = read(s, UInt8)
end
return a
end
function read!{T}(s::IO, a::Array{T})
if isbits(T)
nb::Int = length(a) * sizeof(T)
read!(s, reinterpret(UInt8, a, (nb,)))
else
for i in eachindex(a)
a[i] = read(s, T)
end
end
return a
end
function read(s::IO, ::Type{Char})
ch = read(s, UInt8)
if ch < 0x80
return Char(ch)
end
# mimic utf8.next function
trailing = Base.utf8_trailing[ch+1]
c::UInt32 = 0
for j = 1:trailing
c += ch
c <<= 6
ch = read(s, UInt8)
end
c += ch
c -= Base.utf8_offset[trailing+1]
Char(c)
end
function readuntil(s::IO, delim::Char)
if delim < Char(0x80)
data = readuntil(s, delim%UInt8)
enc = byte_string_classify(data)
return (enc==1) ? ASCIIString(data) : UTF8String(data)
end
out = IOBuffer()
while !eof(s)
c = read(s, Char)
write(out, c)
if c == delim
break
end
end
takebuf_string(out)
end
function readuntil{T}(s::IO, delim::T)
out = T[]
while !eof(s)
c = read(s, T)
push!(out, c)
if c == delim
break
end
end
out
end
# based on code by Glen Hertz
function readuntil(s::IO, t::AbstractString)
l = length(t)
if l == 0
return ""
end
if l > 40
warn("readuntil(IO,AbstractString) will perform poorly with a long string")
end
out = IOBuffer()
m = Array(Char, l) # last part of stream to match
t = collect(t)
i = 0
while !eof(s)
i += 1
c = read(s, Char)
write(out, c)
if i <= l
m[i] = c
else
# shift to last part of s
for j = 2:l
m[j-1] = m[j]
end
m[l] = c
end
if i >= l && m == t
break
end
end
return takebuf_string(out)
end
readline() = readline(STDIN)
readline(s::IO) = readuntil(s, '\n')
readchomp(x) = chomp!(readall(x))
# read up to nb bytes into nb, returning # bytes read
function readbytes!(s::IO, b::AbstractArray{UInt8}, nb=length(b))
olb = lb = length(b)
nr = 0
while nr < nb && !eof(s)
a = read(s, UInt8)
nr += 1
if nr > lb
lb = nr * 2
resize!(b, lb)
end
b[nr] = a
end
if lb > olb
resize!(b, nr) # shrink to just contain input data if was resized
end
return nr
end
# read up to nb bytes from s, returning a Vector{UInt8} of bytes read.
function readbytes(s::IO, nb=typemax(Int))
b = Array(UInt8, nb == typemax(Int) ? 1024 : nb)
nr = readbytes!(s, b, nb)
resize!(b, nr)
end
function readall(s::IO)
b = readbytes(s)
return isvalid(ASCIIString, b) ? ASCIIString(b) : UTF8String(b)
end
readall(filename::AbstractString) = open(readall, filename)
## high-level iterator interfaces ##
type EachLine
stream::IO
ondone::Function
EachLine(stream) = EachLine(stream, ()->nothing)
EachLine(stream, ondone) = new(stream, ondone)
end
eachline(stream::IO) = EachLine(stream)
start(itr::EachLine) = nothing
function done(itr::EachLine, nada)
if !eof(itr.stream)
return false
end
itr.ondone()
true
end
next(itr::EachLine, nada) = (readline(itr.stream), nothing)
eltype(::Type{EachLine}) = ByteString
readlines(s=STDIN) = collect(eachline(s))
# IOStream Marking
# Note that these functions expect that io.mark exists for
# the concrete IO type. This may not be true for IO types
# not in base.
function mark(io::IO)
io.mark = position(io)
end
function unmark(io::IO)
!ismarked(io) && return false
io.mark = -1
return true
end
function reset{T<:IO}(io::T)
ismarked(io) || throw(ArgumentError("$(T) not marked"))
m = io.mark
seek(io, m)
io.mark = -1 # must be after seek, or seek may fail
return m
end
ismarked(io::IO) = io.mark >= 0
# Generic IO stubs
lock(::IO) = nothing
unlock(::IO) = nothing
reseteof(x::IO) = nothing
const SZ_UNBUFFERED_IO = 65536
buffer_writes(x::IO, bufsize=SZ_UNBUFFERED_IO) = nothing
function isopen end
function close end
function flush end
function wait_connected end
function wait_readnb end
function wait_readbyte end
function wait_close end
function nb_available end
function readavailable end
function isreadable end
function iswritable end
function copy end
function eof end
# all subtypes should implement this
read(s::IO, ::Type{UInt8}) = error(typeof(s)," does not support byte I/O")
write(s::IO, x::UInt8) = error(typeof(s)," does not support byte I/O")
|