home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Networking / nmap-5.00-setup.exe / nselib / tab.lua < prev    next >
Text File  |  2009-07-06  |  3KB  |  126 lines

  1. --- Arrange output into tables.
  2. --
  3. -- This module provides NSE scripts with a way to output structured tables
  4. -- similar to what <code>NmapOutputTable.cc</code> provides.
  5. --
  6. -- Example usage:
  7. -- <code>
  8. -- local t = tab.new(2)
  9. -- tab.add(t, 1, 'A1')
  10. -- tab.add(t, 2, 'A2')
  11. -- tab.nextrow(t)
  12. -- tab.add(t, 1, 'BBBBBBBBB1')
  13. -- tab.add(t, 2, 'BBB2')
  14. -- tab.dump(t)
  15. -- </code>
  16. -- @copyright Same as Nmap--See http://nmap.org/book/man-legal.html
  17.  
  18. module(... or "tab",package.seeall)
  19.  
  20. require('strbuf')
  21.  
  22. --- Create and return a new table with a given number of columns and
  23. -- the row counter set to 1.
  24. -- @param cols The number of columns the table will hold.
  25. -- @return A new table.
  26. function new(cols)
  27.     assert(cols > 0)
  28.     local table ={}
  29.  
  30.     table['cols'] = cols
  31.     table['rows'] = 1
  32.     setmetatable(table, {__tostring=dump})
  33.     return table
  34. end
  35.  
  36. --- Add a new string item to a table at a given column position.
  37. --
  38. -- The item will be added to the current row. If <code>nextrow</code> hasn't
  39. -- been called yet that will be row 1.
  40. -- @param t The table.
  41. -- @param v The string to add.
  42. -- @param c The column position at which to add the item.
  43. function add(t, c, v)
  44.     assert(t)
  45.     assert(v)
  46.     assert(t['rows'])
  47.     assert(t['cols'])
  48.     assert(type(v) == "string")
  49.  
  50.     if c < 1 or c > t['cols'] then
  51.         return false
  52.     end
  53.  
  54.     -- add a new row if one doesn't exist
  55.     if t[t['rows']] == nil then
  56.         t[t['rows']] = {}
  57.     end
  58.  
  59.     t[t['rows']][c] = v
  60.     return true
  61. end
  62.  
  63. --- Add a complete row to the table and move on to the next row.
  64. --
  65. -- Calls <code>add</code> for each argument starting with the second argument
  66. -- and after that calls <code>nextrow</code>.
  67. -- @param t The table.
  68. -- @param ... The elements to add to the row.
  69. function addrow(t, ...)
  70.     for i=1, select("#", ...) do
  71.         add( t, i, tostring( ( select(i, ...)) ) )
  72.     end
  73.     nextrow( t )
  74. end
  75.  
  76. --- Move on to the next row in the table. If this is not called
  77. -- then previous column values will be over-written by subsequent
  78. -- values.
  79. -- @param t The table.
  80. function nextrow(t)
  81.     assert(t)
  82.     assert(t['rows'])
  83.     t['rows'] = t['rows'] + 1
  84. end
  85.  
  86. --- Return a formatted string representation of the table.
  87. --
  88. -- The number of spaces in a column is based on the largest element in the
  89. -- column with an additional two spaces for padding.
  90. -- @param t The table.
  91. function dump(t)
  92.     assert(t)
  93.     assert(t['rows'])
  94.     assert(t['cols'])
  95.  
  96.     local col_len = {}    
  97.     local table = strbuf.new()
  98.     local len
  99.  
  100.     -- find largest string in column
  101.     for i=1, t['cols'] do
  102.         local max = 0
  103.         for x=1, t['rows'] do
  104.             if t[x] == nil then t[x] = {} end
  105.             if t[x][i] ~= nil and string.len(t[x][i]) > max then
  106.                 max = string.len(t[x][i])
  107.             end
  108.         end
  109.         col_len[i] = max+2
  110.     end
  111.  
  112.     -- build table with padding so all column elements line up
  113.     for i=1,t['rows'] do
  114.         for x=1, t['cols'] do
  115.             if t[i][x] ~= nil then
  116.                 length = string.len(t[i][x])
  117.                 table = table .. t[i][x]
  118.                 table = table .. string.rep(' ', col_len[x]-length)
  119.             end
  120.         end
  121.         table = table .. "\n"
  122.     end
  123.  
  124.     return strbuf.dump(table)
  125. end
  126.