Ask Your Question
0

Lua - Get string for a ProtoField that uses a lookup table

asked 2019-10-04 20:36:42 +0000

doobop gravatar image

I'm trying to add some summary information and modify a treeitem string in a Lua dissector. I'd like to add the string of a ProtoField that is set via a valuestring table. Is there a way to get the ProtoField's current string (the currently active valuestring)? If I do a tostring(field) it dumps out internal information.

Here's some stub code:

local selectionLookup = {
   [1] = "selection 1",
   [2] = "selection 2",
   [3] = "selection 3"
}
local pfSelection = ProtoField.new("Current selection",
                          "selection", ftypes.UINT8, selectionLookup)

function selectionDissector(stuff, stuff2, stuff3)
   ...
   selectionString = tostring(pfSelection) -- how do I get "selection1", etc. from pfSelection?
   ...
   return 42
end
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-10-07 15:41:19 +0000

doobop gravatar image

updated 2019-10-07 15:41:40 +0000

Upon further review (and learning more about lua), 'selectionLookup' is a dictionary / map. So, just a simple

selectionLookup[selectionValue]

Will give me the string. Still curious if there's any accessor methods to a ProtoField to retrieve the value, but small concern since there's a simple one-liner to get the value.

edit flag offensive delete link more

Comments

I don't think it's quite that simple. In your example, what happens when selectionValue is a value other than 1, 2, or 3? Here's a function I use to deal with this:

function get_valstr(t, val, err_str)

    local str = t[val]
    if str == nil then
        str = err_str
    end

    return str or "Unknown"

end -- get_valstr()
cmaynard gravatar imagecmaynard ( 2019-10-07 16:26:34 +0000 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-10-04 20:36:42 +0000

Seen: 740 times

Last updated: Oct 07 '19