Support for fractions in addedblocks
binobj:addBlock(123.234,nil,"d") allows you to store fractional data to a string
This commit is contained in:
parent
3bbfcf2e9b
commit
73a9ee499f
@ -1,5 +1,5 @@
|
|||||||
bin={}
|
bin={}
|
||||||
bin.Version={5,0,3}
|
bin.Version={5,0,1}
|
||||||
bin.stage='stable'
|
bin.stage='stable'
|
||||||
bin.data=''
|
bin.data=''
|
||||||
bin.t='bin'
|
bin.t='bin'
|
||||||
@ -98,6 +98,7 @@ function bin.fileExist(path)
|
|||||||
return p
|
return p
|
||||||
end
|
end
|
||||||
function bin.toHex(str)
|
function bin.toHex(str)
|
||||||
|
local str=bin.normalizeData(str)
|
||||||
return (str:gsub('.', function (c)
|
return (str:gsub('.', function (c)
|
||||||
return string.format('%02X', string.byte(c))
|
return string.format('%02X', string.byte(c))
|
||||||
end))
|
end))
|
||||||
@ -418,7 +419,9 @@ function bin:getBlock(t,n)
|
|||||||
end
|
end
|
||||||
function bin:addBlock(d,fit,fmt)
|
function bin:addBlock(d,fit,fmt)
|
||||||
if not fmt then fmt=type(d):sub(1,1) end
|
if not fmt then fmt=type(d):sub(1,1) end
|
||||||
if type(d)=="number" then
|
if bin.registerBlocks[fmt] then
|
||||||
|
self:tackE(bin.registerBlocks[fmt][2](d,fit,fmt,self,bin.registerBlocks[fmt][2]))
|
||||||
|
elseif type(d)=="number" then
|
||||||
local data=bin.defualtBit.numToBytes(d,fit or 4,fmt,function()
|
local data=bin.defualtBit.numToBytes(d,fit or 4,fmt,function()
|
||||||
error("Overflow! Space allotted for number is smaller than the number takes up. Increase the fit!")
|
error("Overflow! Space allotted for number is smaller than the number takes up. Increase the fit!")
|
||||||
end)
|
end)
|
||||||
@ -429,8 +432,6 @@ function bin:addBlock(d,fit,fmt)
|
|||||||
data=data..string.rep("\0",fit-#data)
|
data=data..string.rep("\0",fit-#data)
|
||||||
end
|
end
|
||||||
self:tackE(data)
|
self:tackE(data)
|
||||||
elseif bin.registerBlocks[fmt] then
|
|
||||||
self:tackE(bin.registerBlocks[fmt][2](d,fit,fmt,self,bin.registerBlocks[fmt][2]))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
bin.registerBlocks={}
|
bin.registerBlocks={}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
local __CURRENTVERSION=1
|
local __CURRENTVERSION=2
|
||||||
bin.registerBlock("t",function(SIZE_OR_NIL,ref)
|
bin.registerBlock("t",function(SIZE_OR_NIL,ref)
|
||||||
local header=ref:read(3)
|
local header=ref:read(3)
|
||||||
if not header:match("(LT.)") then error("Not a valid table struct!") end
|
if not header:match("(LT.)") then error("Not a valid table struct!") end
|
||||||
@ -21,8 +21,8 @@ bin.registerBlock("t",function(SIZE_OR_NIL,ref)
|
|||||||
ind=ref:read(indL)
|
ind=ref:read(indL)
|
||||||
end
|
end
|
||||||
if dt=="N" then
|
if dt=="N" then
|
||||||
tab[ind]=ref:getBlock("n",4)
|
tab[ind]=ref:getBlock("d")
|
||||||
n=n+4
|
n=n+8
|
||||||
elseif dt=="I" then
|
elseif dt=="I" then
|
||||||
tab[ind]=math.huge
|
tab[ind]=math.huge
|
||||||
ref:getBlock("n",4)
|
ref:getBlock("n",4)
|
||||||
@ -94,7 +94,9 @@ end,function(d,fit,fmt,self,rec,tabsaw)
|
|||||||
end
|
end
|
||||||
if type(v)=="number" then
|
if type(v)=="number" then
|
||||||
-- How do we handle number data
|
-- How do we handle number data
|
||||||
table.insert(bData,bin.defualtBit.numToBytes(v,4))
|
local temp=bin.new()
|
||||||
|
temp:addBlock(v,nil,"d")
|
||||||
|
table.insert(bData,temp.data)
|
||||||
elseif type(v)=="string" then
|
elseif type(v)=="string" then
|
||||||
-- Lets work on strings
|
-- Lets work on strings
|
||||||
table.insert(bData,bin.defualtBit.numToBytes(#v,4)) -- add length of string
|
table.insert(bData,bin.defualtBit.numToBytes(#v,4)) -- add length of string
|
||||||
@ -130,4 +132,15 @@ bin.registerBlock("f",function(SIZE_OR_NIL,ref)
|
|||||||
end,function(d)
|
end,function(d)
|
||||||
local dump=string.dump(d)
|
local dump=string.dump(d)
|
||||||
return bin.defualtBit.numToBytes(#dump,4)..dump
|
return bin.defualtBit.numToBytes(#dump,4)..dump
|
||||||
|
end)
|
||||||
|
bin.registerBlock("d",function(SIZE_OR_NIL,ref)
|
||||||
|
local w,p=ref:getBlock("n",4),ref:getBlock("n",4)
|
||||||
|
p=tonumber("0."..tostring(p))
|
||||||
|
return w+p
|
||||||
|
end,function(d,fit,fmt,self,rec,tabsaw)
|
||||||
|
local w,p = toFraction(d)
|
||||||
|
local temp=bin.new()
|
||||||
|
temp:addBlock(w,4)
|
||||||
|
temp:addBlock(p,4)
|
||||||
|
return temp.data
|
||||||
end)
|
end)
|
||||||
@ -19,6 +19,13 @@ function table.flip(t)
|
|||||||
end
|
end
|
||||||
return tt
|
return tt
|
||||||
end
|
end
|
||||||
|
function toFraction(n)
|
||||||
|
local w,p=math.modf(n)
|
||||||
|
if p~=0 then
|
||||||
|
p=tonumber(tostring(p):sub(3))
|
||||||
|
end
|
||||||
|
return w,p
|
||||||
|
end
|
||||||
function io.cleanName(name)
|
function io.cleanName(name)
|
||||||
name=name:gsub("\\","")
|
name=name:gsub("\\","")
|
||||||
name=name:gsub("/","")
|
name=name:gsub("/","")
|
||||||
|
|||||||
81
crypto.lua
81
crypto.lua
@ -1,37 +1,44 @@
|
|||||||
package.path="?/init.lua;?.lua;"..package.path
|
function bubbleSort(A)
|
||||||
require("multi")
|
local itemCount=#A
|
||||||
require("bin")
|
local hasChanged
|
||||||
--~ function bin.streamCipherEncrypt(data,key)
|
repeat
|
||||||
--~
|
for i=1,#A do
|
||||||
--~ end
|
io.write(string.char(A[i]))
|
||||||
--~ t=bin.new("Hello")
|
end
|
||||||
--~ print(t)
|
io.write("\n")
|
||||||
--~ t:slide(50)
|
hasChanged = false
|
||||||
--~ print(t)
|
itemCount=itemCount - 1
|
||||||
--~ t:slide(-50)
|
for i = 1, itemCount do
|
||||||
--~ print(t)
|
if A[i] > A[i + 1] then
|
||||||
--~ t2=bin.stream("test.dat",false)
|
A[i], A[i + 1] = A[i + 1], A[i]
|
||||||
--~ tt=t2:toDataBuffer()
|
hasChanged = true
|
||||||
--~ print(tt[1])
|
end
|
||||||
print("TEST - 1")
|
end
|
||||||
test=bin.new("I am a string in a bin")
|
until hasChanged == false
|
||||||
print(test)
|
end
|
||||||
print("TEST - 2: Writing a test file to disk")
|
reflist={"A","S","S","I","G","N","M","E","N","T"}
|
||||||
test2=bin.freshStream("newFileToStreamWriteTo.dat",false)
|
list={}
|
||||||
test2:addBlock(1234,4)
|
list2={}
|
||||||
test2:addBlock("Hello",5)
|
for i,v in pairs(reflist) do
|
||||||
test2:addBlock(true) -- always 1 and a lua type
|
list[#list+1]=string.byte(v)
|
||||||
test2:addBlock({1,2,3,4,5}) -- depends and is a lua type
|
list2[#list2+1]=string.byte(v)
|
||||||
test2:close()
|
end
|
||||||
print("test 2 done")
|
function SelectionSort(f)
|
||||||
print("TEST - 3: reading the file we wrote to disk")
|
for k = 1, #f-1 do
|
||||||
test3=bin.load("newFileToStreamWriteTo.dat") -- binary file
|
for i=1,#f do
|
||||||
nL,nB=test3:getBlock("n",4) -- reads the first 4 bytes as a number
|
io.write(string.char(f[i]))
|
||||||
-- litte endian, big endian
|
end
|
||||||
print(nL,nB)
|
io.write("\n")
|
||||||
str=test3:getBlock("s",5)
|
local idx = k
|
||||||
print(str)
|
for i = k+1, #f do
|
||||||
bool=test3:getBlock("b")
|
if f[i] < f[idx] then
|
||||||
print(bool)
|
idx = i
|
||||||
tab=test3:getBlock("t")
|
end
|
||||||
print(tab)
|
end
|
||||||
|
f[k], f[idx] = f[idx], f[k]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print("Bubble Sort")
|
||||||
|
bubbleSort(list)
|
||||||
|
print("Selection Sort")
|
||||||
|
SelectionSort(list2)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user