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:
Ryan 2017-11-13 22:16:42 -05:00
parent 3bbfcf2e9b
commit 73a9ee499f
4 changed files with 73 additions and 45 deletions

View File

@ -1,5 +1,5 @@
bin={}
bin.Version={5,0,3}
bin.Version={5,0,1}
bin.stage='stable'
bin.data=''
bin.t='bin'
@ -98,6 +98,7 @@ function bin.fileExist(path)
return p
end
function bin.toHex(str)
local str=bin.normalizeData(str)
return (str:gsub('.', function (c)
return string.format('%02X', string.byte(c))
end))
@ -418,7 +419,9 @@ function bin:getBlock(t,n)
end
function bin:addBlock(d,fit,fmt)
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()
error("Overflow! Space allotted for number is smaller than the number takes up. Increase the fit!")
end)
@ -429,8 +432,6 @@ function bin:addBlock(d,fit,fmt)
data=data..string.rep("\0",fit-#data)
end
self:tackE(data)
elseif bin.registerBlocks[fmt] then
self:tackE(bin.registerBlocks[fmt][2](d,fit,fmt,self,bin.registerBlocks[fmt][2]))
end
end
bin.registerBlocks={}

View File

@ -1,4 +1,4 @@
local __CURRENTVERSION=1
local __CURRENTVERSION=2
bin.registerBlock("t",function(SIZE_OR_NIL,ref)
local header=ref:read(3)
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)
end
if dt=="N" then
tab[ind]=ref:getBlock("n",4)
n=n+4
tab[ind]=ref:getBlock("d")
n=n+8
elseif dt=="I" then
tab[ind]=math.huge
ref:getBlock("n",4)
@ -94,7 +94,9 @@ end,function(d,fit,fmt,self,rec,tabsaw)
end
if type(v)=="number" then
-- 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
-- Lets work on strings
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)
local dump=string.dump(d)
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)

View File

@ -19,6 +19,13 @@ function table.flip(t)
end
return tt
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)
name=name:gsub("\\","")
name=name:gsub("/","")

View File

@ -1,37 +1,44 @@
package.path="?/init.lua;?.lua;"..package.path
require("multi")
require("bin")
--~ function bin.streamCipherEncrypt(data,key)
--~
--~ end
--~ t=bin.new("Hello")
--~ print(t)
--~ t:slide(50)
--~ print(t)
--~ t:slide(-50)
--~ print(t)
--~ t2=bin.stream("test.dat",false)
--~ tt=t2:toDataBuffer()
--~ print(tt[1])
print("TEST - 1")
test=bin.new("I am a string in a bin")
print(test)
print("TEST - 2: Writing a test file to disk")
test2=bin.freshStream("newFileToStreamWriteTo.dat",false)
test2:addBlock(1234,4)
test2:addBlock("Hello",5)
test2:addBlock(true) -- always 1 and a lua type
test2:addBlock({1,2,3,4,5}) -- depends and is a lua type
test2:close()
print("test 2 done")
print("TEST - 3: reading the file we wrote to disk")
test3=bin.load("newFileToStreamWriteTo.dat") -- binary file
nL,nB=test3:getBlock("n",4) -- reads the first 4 bytes as a number
-- litte endian, big endian
print(nL,nB)
str=test3:getBlock("s",5)
print(str)
bool=test3:getBlock("b")
print(bool)
tab=test3:getBlock("t")
print(tab)
function bubbleSort(A)
local itemCount=#A
local hasChanged
repeat
for i=1,#A do
io.write(string.char(A[i]))
end
io.write("\n")
hasChanged = false
itemCount=itemCount - 1
for i = 1, itemCount do
if A[i] > A[i + 1] then
A[i], A[i + 1] = A[i + 1], A[i]
hasChanged = true
end
end
until hasChanged == false
end
reflist={"A","S","S","I","G","N","M","E","N","T"}
list={}
list2={}
for i,v in pairs(reflist) do
list[#list+1]=string.byte(v)
list2[#list2+1]=string.byte(v)
end
function SelectionSort(f)
for k = 1, #f-1 do
for i=1,#f do
io.write(string.char(f[i]))
end
io.write("\n")
local idx = k
for i = k+1, #f do
if f[i] < f[idx] then
idx = i
end
end
f[k], f[idx] = f[idx], f[k]
end
end
print("Bubble Sort")
bubbleSort(list)
print("Selection Sort")
SelectionSort(list2)