Skip to content
Permalink
Browse files
Added wrappers for bit, peripheral, and redstone. Added HTTP classes.…
… Data IO streams now use the see.util.Bit class instead of natively importing bit.
  • Loading branch information
James King committed Aug 12, 2013
1 parent f1224cf commit fece31c
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 75 deletions.
@@ -3,15 +3,15 @@
--@extends see.io.InputStream

--[[
Wraps around an array to read from.
@param Array:wrap The array to wrap around.
Wraps around an array to read from.
@param Array:wrap The array to wrap around.
]]
function ArrayInputStream:init(wrap)
self.wrap = wrap
self.position = 1
self.wrap = wrap
self.position = 1
end

function ArrayInputStream:read()
self.position = self.position + 1
return self.wrap[self.position - 1]
self.position = self.position + 1
return self.wrap[self.position - 1]
end
@@ -3,15 +3,15 @@
--@extends see.io.OutputStream

--[[
Wraps around an array to write to.
@param Array:wrap The array to wrap around.
Wraps around an array to write to.
@param Array:wrap The array to wrap around.
]]
function ArrayOutputStream:init(wrap)
self.wrap = wrap
self.position = 1
self.wrap = wrap
self.position = 1
end

function ArrayOutputStream:write(b)
self.wrap[self.position] = b
self.position = self.position + 1
self.wrap[self.position] = b
self.position = self.position + 1
end
@@ -1,57 +1,56 @@
--@native bit

--@import see.io.InputStream
--@import see.util.Bit

--@extends see.io.InputStream

--[[
An InputStream for doing more advanced operations on other InputStreams.
An InputStream for doing more advanced operations on other InputStreams.
]]

--[[
Wraps arounds a see.io.InputStream.
@param see.io.InputStream:wrap The InputStream to wrap around.
Wraps arounds a see.io.InputStream.
@param see.io.InputStream:wrap The InputStream to wrap around.
]]
function DataInputStream:init(wrap)
self.wrap = wrap
self.wrap = wrap
end

function DataInputStream:read()
return self.wrap:read()
return self.wrap:read()
end

--[[
Reads an integer with the given amount of bytes.
@param number:bytes The number of bytes to encode the number with.
@return number The integer that was read.
Reads an integer with the given amount of bytes.
@param number:bytes The number of bytes to encode the number with.
@return number The integer that was read.
]]
function DataInputStream:readInt(bytes)
local ret = 0
local b
for i = bytes, 1, -1 do
b = self:read()
ret = bit.bor(ret, bit.blshift(b, (i - 1) * 8))
end
return ret
local ret = 0
local b
for i = bytes, 1, -1 do
b = self:read()
ret = bit.bor(ret, bit.blshift(b, (i - 1) * 8))
end
return ret
end

--[[
Reads a string of a specific length.
@param number:len The length of the string to read.
@return see.base.String The string that was read.
Reads a string of a specific length.
@param number:len The length of the string to read.
@return see.base.String The string that was read.
]]
function DataInputStream:readString(len)
local ret = String.new()
for i = 1, len do
ret[i] = self:read()
end
return ret
local ret = String.new()
for i = 1, len do
ret[i] = self:read()
end
return ret
end

function DataInputStream:flush()
self.wrap:flush()
self.wrap:flush()
end

function DataInputStream:close()
self.wrap:close()
self.wrap:close()
end
@@ -1,51 +1,50 @@
--@native bit

--@import see.io.OutputStream
--@import see.util.Bit

--@extends see.io.OutputStream

--[[
An OutputStream for doing more advanced operations on other OutputStreams.
An OutputStream for doing more advanced operations on other OutputStreams.
]]

--[[
Wraps arounds a see.io.OutputStream.
@param see.io.OutputStream:wrap The OutputStream to wrap around.
Wraps arounds a see.io.OutputStream.
@param see.io.OutputStream:wrap The OutputStream to wrap around.
]]
function DataOutputStream:init(wrap)
self.wrap = wrap
self.wrap = wrap
end

function DataOutputStream:write(b)
self.wrap:write(b)
self.wrap:write(b)
end

--[[
Writes an integer with the given amount of bytes.
@param number:value The value to write.
@param number:bytes The number of bytes to encode the number with.
Writes an integer with the given amount of bytes.
@param number:value The value to write.
@param number:bytes The number of bytes to encode the number with.
]]
function DataOutputStream:writeInt(value, bytes)
for i = bytes, 1, -1 do
self:write(bit.band(0xff, bit.brshift(value, (i - 1) * 8)))
end
for i = bytes, 1, -1 do
self:write(bit.band(0xff, bit.brshift(value, (i - 1) * 8)))
end
end

--[[
Writes a string.
@param see.base.String:str The string to write.
Writes a string.
@param see.base.String:str The string to write.
]]
function DataOutputStream:writeString(str)
str = cast(str, String)
for i = 1, str:length() do
self:write(str[i])
end
str = cast(str, String)
for i = 1, str:length() do
self:write(str[i])
end
end

function DataOutputStream:flush()
self.wrap:flush()
self.wrap:flush()
end

function DataOutputStream:close()
self.wrap:close()
self.wrap:close()
end
@@ -0,0 +1,18 @@
--@native peripheral
--@native unpack

function Peripheral.isPresent(side)
return peripheral.isPresent(cast(side, "string"))
end

function Peripheral.getType(side)
return String.new(peripheral.getType(cast(side, "string")))
end

function Peripheral.getMethods(side)
return Array.new(unpack(peripheral.getMethods(cast(side, "string"))))
end

function Peripheral.call(side, method, ...)
return peripheral.call(cast(side, "string"), cast(method, "string"), ...)
end
@@ -0,0 +1,37 @@
--@native redstone

function Redstone.getInput(side)
return redstone.getInput(cast(side, "string"))
end

function Redstone.setOutput(side, o)
redstone.setOutput(cast(side, "string"), o)
end

function Redstone.getOutput(side)
return redstone.getOutput(cast(side, "string"))
end

function Redstone.getAnalogInput(side)
return redstone.getAnalogInput(cast(side, "string"))
end

function Redstone.setAnalogOutput(side, strength)
redstone.setAnalogOutput(cast(side, "string"), strength)
end

function Redstone.getAnalogOutput(side)
return redstone.getAnalogOutput(cast(side, "string"))
end

function Redstone.getBundledInput(side)
return redstone.getAnalogInput(cast(side, "string"))
end

function Redstone.setBundledOutput(side, colors)
redstone.setAnalogOutput(cast(side, "string"), colors)
end

function Redstone.getBundledOutput(side)
return redstone.getAnalogOutput(cast(side, "string"))
end
@@ -0,0 +1,26 @@
--@native http

--@import see.net.HttpResponse

--[[
Makes a blocking HTTP request.
@param see.net.Url:url The URL to request from.
@param see.base.String:postData Post data to send if this is a POST request.
@return see.base.String The body of the response.
]]
function Http.sync(url, postData)
if postData then postData = cast(postData, "string") end
local f = postData and http.post or http.get
local handle = f(url.string:lstr(), postData)
return HttpResponse.new(String.new(handle.readAll()), handle.getResponseCode())
end

--[[
Makes an asynchronous HTTP request.
@param see.net.Url:url The URL to request from.
@param see.base.String:postData Post data to send if this is a POST request.
]]
function Http.async(url, postData)
if postData then postData = cast(postData, "string") end
http.request(url, postData)
end
@@ -0,0 +1,8 @@
--[[
Wraps around a native HTTP handle.
@param handle:handle The native handle.
]]
function HttpResponse:init(body, responseCode)
self.body = cast(body, String)
self.responseCode = responseCode
end
@@ -0,0 +1,7 @@
function Url:init(string)
self.string = cast(string, String)
end

function Url:toString()
return self.string
end
@@ -0,0 +1,29 @@
--@native bit

function Bit.blshift(n, bits)
return bit.blshift(n, bits)
end

function Bit.brshift(n, bits)
return bit.brshift(n, bits)
end

function Bit.blogicrshift(n, bits)
return bit.blogic_rshift(n, bits)
end

function Bit.bxor(a, b)
return bit.bxor(a, b)
end

function Bit.bor(a, b)
return bit.bor(a, b)
end

function Bit.band(a, b)
return bit.band(a, b)
end

function Bit.bnot(a, b)
return bit.bnot(a, b)
end
@@ -1,18 +1,5 @@
--@import see.io.Path
--@import see.io.FileOutputStream
--@import see.io.FileInputStream
--@import see.io.DataOutputStream
--@import see.io.DataInputStream
--@import see.io.ArrayOutputStream
--@import see.io.ArrayInputStream
--@import see.io.Redstone

function Test.main()
local path = Path.new("/filetest")
local array = Array.new()

local dos = DataOutputStream.new(ArrayOutputStream.new(array))
dos:writeString("Hello!\n")
local dis = DataInputStream.new(ArrayInputStream.new(array))

System.write(dis:readString(array:length()))

end

0 comments on commit fece31c

Please sign in to comment.