webmcp

diff framework/env/encode/format_options.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/framework/env/encode/format_options.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,40 @@
     1.4 +--[[--
     1.5 +string =                -- part of string to be used as __format information
     1.6 +encode.format_options(
     1.7 +  params                -- arguments for format function
     1.8 +)
     1.9 +
    1.10 +This function is used by encode.format_info(...).
    1.11 +
    1.12 +--]]--
    1.13 +
    1.14 +function encode.format_options(params)
    1.15 +  local params = params or {}
    1.16 +  local result_parts = {}
    1.17 +  for key, value in pairs(params) do
    1.18 +    if type(key) == "string" then
    1.19 +      if string.find(key, "^[A-Za-z][A-Za-z0-9_]*$") then
    1.20 +        table.insert(result_parts, "-")
    1.21 +        table.insert(result_parts, key)
    1.22 +        table.insert(result_parts, "-")
    1.23 +        local t = type(value)
    1.24 +        if t == "string" then
    1.25 +          value = string.gsub(value, "\\", "\\\\")
    1.26 +          value = string.gsub(value, "'", "\\'")
    1.27 +          table.insert(result_parts, "'")
    1.28 +          table.insert(result_parts, value)
    1.29 +          table.insert(result_parts, "'")
    1.30 +        elseif t == "number" then
    1.31 +          table.insert(result_parts, tostring(value))
    1.32 +        elseif t == "boolean" then
    1.33 +          table.insert(result_parts, value and "true" or "false")
    1.34 +        else
    1.35 +          error("Format parameter table contained value of unsupported type " .. t .. ".")
    1.36 +        end
    1.37 +      else
    1.38 +        error('Format parameter table contained invalid key "' .. key .. '".')
    1.39 +      end
    1.40 +    end
    1.41 +  end
    1.42 +  return table.concat(result_parts)
    1.43 +end

Impressum / About Us