Nmap Development mailing list archives

Re: [NSE] proposal of size_tohuman() in stdnse library


From: David Fifield <david () bamsoftware com>
Date: Tue, 8 Jun 2010 11:16:53 -0600

On Tue, Jun 08, 2010 at 02:09:03AM +0100, Djalal Harouni wrote:
I've coded a function which converts the size in bytes to
human readable format, I'm using this function in my NFS code to show
files size, but perhaps this function would be more useful if it is in
the stdnse library, it's a generic function.

I've make it simple, the %.1f format will do some round up/down.


--- Converts the size in bytes to a human readable format
--
-- An optional second argument is the size of the block
--
-- @usage
-- size_tohuman(1024) --> 1024.0B
-- size_tohuman(926548776) --> 883.6M
-- size_tohuman(246548, 1024) --> 240.8K
-- size_tohuman(246548, 1000) --> 246.5K
--
-- @param size in bytes
-- @param blocksize represents the number of bytes per block
--        Possible values are: 1024 or 1000
--        Default value is: 1024
-- @return String that represent the size in the human readable
--         format
function size_tohuman(size, blocksize)
      local idx, bs = 1, 1024
      local unit = { "B", "K", "M", "G" }
      if blocksize and blocksize == 1000 then
              bs = blocksize
      end
        for i=1, #unit do
              if (size > bs) then
                              size = size / bs
                              idx = idx + 1
              end
        end
        return string.format("%.1f%s", size, unit[idx])
end

This looks fine to me. In tcpip.cc there is a function called
ll2shortascii that is used for the same purpose in Nmap. (ll2shortascii
is a bad name; it's going to be changed to format_bytecount when Luis
merges his nmap-dedup branches.) How about the name format_bytecount?
That goes with the names format_difftime and format_output that are
already in stdnse.

David Fifield
_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://seclists.org/nmap-dev/


Current thread: