Warning: this is an htmlized version!
The original is across this link,
and the conversion rules are here.
-- ctypes.lua: implement (middle-)C types on top of peek.c.
-- This file:
--   http://angg.twu.net/peek/ctypes.lua.html
--   http://angg.twu.net/peek/ctypes.lua
--           (find-angg "peek/ctypes.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- Version: 2011aug25
-- License: GPL3
--
-- See: (find-TH "peek")
--    http://angg.twu.net/peek.html
-- file:///home/edrx/TH/L/peek.html
-- (find-dn5file "")
-- This is just a skeleton at the moment.

-- (find-angg "DAVINCI/peek.lua")
-- (find-angg "DAVINCI/peek-luadecls-1.txt")
-- (find-angg "DAVINCI/peek-luadecls-2.txt")
-- (find-angg "LUA/middle-c.lua")

package.path = ee_expand("~/dednat5/?.lua")..";"..package.path
require "eoo"         -- (find-dn5 "eoo.lua")

* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)

Class = {
    type   = "Class",
    __call = function (class, o)
        setmetatable(o, class)
        if class.__init then class:__init(o) end
        return o
      end,
  }
setmetatable(Class, Class)

ctypes = {}

CPrimType = Class {
  type    = "CPrimType",
  __init  = function (c, o) ctypes[o.ctype] = o end,
  __index = {
  },
}
CStarType = Class {
  type    = "CStarType",
  __init  = function (c, o) ctypes[o.ctype] = o end,
  __index = {
  },
}
CArrType = Class {
  type    = "CArrType",
  __init  = function (c, o) ctypes[o.ctype] = o end,
   __index = {
  },
}

CStructType = Class {
  type    = "CStructType",
  __init  = function (c, o) ctypes[o.ctype] = o end,
  __index = {
  },
}
CUnionType = Class {
  type    = "CUnionType",
  __init  = function (c, o) ctypes[o.ctype] = o end,
  __index = {
  },
}

CTypeDefType = Class {
  type    = "CTypeDefType",
  __init  = function (c, o) ctypes[o.ctype] = o end,
  __index = {
  },
}

------------------------------------------------------------

CPrimType {ctype="char",                  nbytes=1,   align=1}
CStarType {ctype="char*",    base="char", nbytes=4,   align=4}
CArrType  {ctype="char[]",   base="char", nbytes=nil, align=nil}
CArrType  {ctype="char[10]", base="char", nbytes=10,  align=1}
CPrimType {ctype="int",                   nbytes=4,   align=4}
CStarType {ctype="int*",     base="int",  nbytes=4,   align=4}
CArrType  {ctype="int[]",    base="int",  nbytes=nil, align=nil}
CArrType  {ctype="int[10]",  base="int",  nbytes=40,  align=4}

CStructType  {ctype="struct{int a,char b}", nbytes=5, align=4,
              offsets={["a"]=CField {ctype="int",  offset=0},
                       ["b"]=CField {ctype="char", offset=4},
                      },
             }
CUnionType   {ctype="union{int a,char b}",  nbytes=4, align=4,
              offsets={["a"]=CField {ctype="int",  offset=0},
                       ["b"]=CField {ctype="char", offset=0},
                      },
             }

CTypedefType {ctype="sab", base="struct{int a,char b}", nbytes=5, align=4}
CTypedefType {ctype="uab", base="union{int a,char b}",  nbytes=4, align=4}

PP(ctypes)



--- Create a few primitive types.
CPrimTypeFrom("char",   1, 1)
CPrimTypeFrom("short",  2, 2)
CPrimTypeFrom("ushort", 2, 2)
CPrimTypeFrom("int",    4, 4)
CPrimTypeFrom("uint",   4, 4)
CPrimTypeFrom("void",   nil, nil)

ctype_add_star = function (ctname)
    local newctname = ctname.."*"
    local ctype = ctypes[ctname]
    types[newctname] = CStarType {
      name     = newctname,
      basetype = ctype,
      size     = 4,
      align    = 4,
    }
    return types[newctname]
  end


-- (find-blogme4 "eval.lua" "parse_pattern")

-- AssertCType("char[23][4][]*[5]")

CType = Class {
  type    = "CType",
  __index = {
  },
}
ctypes = {}


CPrimTypeFrom("char",   1, 1)
CPrimTypeFrom("short",  2, 2)
CPrimTypeFrom("ushort", 2, 2)
CPrimTypeFrom("int",    4, 4)
CPrimTypeFrom("uint",   4, 4)
CPrimTypeFrom("void",   nil, nil)




-- dump-to: tests
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
require "ctypes"


CObj {ctype="char",  addr=1000, bytes="w"}
CObj {ctype="char*", addr=1000}
CObj {ctype="char*", addr=1000, bytes="!@#$"}



methods:
  plus
  peek
  poke
  star
  

--               
--  __int__ __int__ c______
-- |_:_:_:_|_:_:_:_|_|_:_:_|
-- \-----intintc-----/
-- 0 1 2 3 4 5 6 7 8 9 101112
-- Example: if "intintc" is a ctype with nbytes=9 and align=4,
-- then it has excess=1 and miss=3, and "intintc[100]" is a ctype
-- with nbytes = (9+3)*100-3 = 1200-3 = 1197.
-- 

excess = nbytes % align
miss   = (excess == 0) and 0 else (nbytes - excess)



fake("char*", 1000)


(char*)1000 + 1 = (char*)1004
sizeof((char*)1000) = sizeof((char*)) = 4
*(char*)1000 = peek(1000, 1) = "a"

(char)25
(int)1234




* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
require "ctypes"
string.tonumber = tonumber
printf_ = tostring(printf):match("0x([0-9a-f]+)"):tonumber(16)
= peek_(printf_, 10)

-- (find-es "lua5" "lua-datatypes-gdb")

-- require "peek"
-- (find-angg "peek/peek-0.0.1-0.rockspec" "test")
buf = malloc_(20)
poke_(buf, "abcd")
PP(peek_(buf+2, 2))   --> "cd"





= print
= tostring(print)
= tostring(print):match("0x([0-9A-Fa-f]+)")
= tostring(print):match("0x([0-9A-Fa-f]+)")

hexton = function (hexstr)
    return tonumber(hexstr:match("0x([0-9A-Fa-f]+)"), 16)
  end
ntohex = function (n) return string.format("0x%x", n) end
= hexton "0x2F"
= ntohex(47)

 * (find-angg "peek/peek-0.0.1-0.rockspec" "test")

--]==]

-- (find-angg "peek/ctypes.lua")
-- (find-angg "DAVINCI/peek.lua")
-- (find-angg "DAVINCI/peek-luadecls-1.txt")
-- (find-angg "DAVINCI/peek-luadecls-2.txt")

-- Local Variables:
-- coding:             raw-text-unix
-- ee-anchor-format:   "«%s»"
-- End: