Advertisement
Guest User

Data Module

a guest
Jul 2nd, 2012
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.95 KB | None | 0 0
  1. ------------------
  2. --DATA FUNCTIONS--
  3. ------------------
  4.  
  5. data = {}
  6.  
  7. function data.dir(fn)
  8.     local path = debug.getinfo(debug.getinfo(2).func).short_src
  9.     if not fn then
  10.         path = path:reverse()
  11.         path = path:sub(path:find("/",1),-1):reverse()
  12.     end
  13.     return path
  14. end
  15.  
  16. function data.serializeint(int,little)
  17.     int = math.floor(math.abs(int))
  18.     local size = math.ceil(math.logx(int+1,256))
  19.     if size == 0 then size = 1 end
  20.     local t = {}
  21.     for i=1,size-1,1 do
  22.         local value = math.floor(int/math.pow(256,size-i))
  23.         t[i] = string.char(value)
  24.         int = int-value*math.pow(256,size-i)
  25.     end
  26.     table.insert(t,string.char(math.floor(int%256)))
  27.     return (little and table.concat(t):reverse() or table.concat(t))
  28. end
  29.  
  30. function data.deserializeint(int, little)
  31.     local size = string.len(int)
  32.     local value = 0
  33.     for i=1, size do
  34.         local ex = math.pow(256,little and (i-1) or size-i )
  35.         value = value + string.byte(string.sub(int,i,i))*ex
  36.     end
  37.     return value
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement