lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Since at least one user of this list asked, and I suppose several other
silent ones would like, I continue to send here some code snippets I create.
Mostly for educational purpose, a bit because they can be reused.
I will publish it on the SWiki. I am waiting for lua-users.org...

This time, my code snippet will, for once, have a well written function...
That's because Roberto wrote it :-)

I had to dump the content of a string because its size was smaller than the
file I read it from. I made a little routine to do it. I discovered that
readfrom() wasn't made to read binary files...

Then, I found a similar function in Roberto's book. Except of course it was
more elegant (using regular expressions), and faster too...

To avoid pure paraphrasing, I improved it a little (more parametrizing), and
I publish my own lame routine as educational "don't do Lua programs the VB
way" :-)

I choose to parametrize the function using global variables, but of course
transforming it to accept parameters as arguments is trivial.

-- Dump a file in hexadecimal form.
-- by Philippe Lhoste <PhiLho@GMX.net> http://jove.prohosting.com/~philho/
-- v. 1.0 -- 2001/07/27

filenameIn  = (arg and arg[1]) or "-"  -- If absent, use standard input
filenameOut = (arg and arg[2]) or "-"  -- If absent, use standard output

-- For debugging:
--filenameIn = "Fav.ubx"

-- My dumb dump
function PLDumpString(fileContent)
  local len = strlen(fileContent)
  local line = ""
  for i = 1, len do
    line = line .. format('%02X', strbyte(strsub(fileContent, i, i))) .. ' '
    if mod(i, 16) == 0 then
      write(line, '\n')
      line = ""
    end
  end
  if line ~= '' then
    write(line, '\n')
  end
end

-- Roberto's dump
-- Slighly faster despite dumping more data...
bytesPerLine = 16
bDumpOffset = 1
bDumpAscii = 1

function DumpString(fileContent)
  local offset = 0
  local line
  while 1 do
    line = strsub(fileContent, offset + 1, offset + bytesPerLine)
    if line == '' then break end
    if bDumpOffset then
      write(format("%08X", offset), "  ") -- Write offset of the line
    end
    -- Smart use of gsub: it doesn't do any replacement,
    -- but instead is used as an iterator over the string bytes.
    gsub(line, "(.)",
      function(byte) write(format("%02X ", strbyte(byte))) end
    )
    if bDumpAscii then
      write(strrep("   ", bytesPerLine - strlen(line) + 1)) -- align strings
      write(gsub(line, "%c", '&#8226;')) -- replace control chars by dot
    end
    write('\n')
    offset = offset + bytesPerLine
  end
end

function ProcessFile()
  local fileInHandle
  if filenameIn ~= "-" then
    -- I must use openfile instead of readfrom because the later doesn't
support binary mode
    fileInHandle = openfile(filenameIn, "rb")
    if not fileInHandle then
      return nil, filenameIn
    end
  else
    fileInHandle = _INPUT
  end
  if filenameOut ~= "-" then
    if not writeto(filenameOut) then
      return nil, filenameOut
    end
  end

  print("Files: ", filenameIn, filenameOut)
  local fileContent = read(fileInHandle, "*a")  -- Read the whole file
  closefile(fileInHandle)
  print("fileContent is " .. strlen(fileContent) .. " bytes long")
  t1 = clock()
  PLDumpString(fileContent)
  t2 = clock()
  print("Time: ", t2 - t1)
  writeto()
  if filenameIn ~= "-" then
    print(remove(filenameOut))
  end
  if filenameOut ~= "-" then
    if not writeto(filenameOut) then
      return nil, filenameOut
    end
  end
  t1 = clock()
  DumpString(fileContent)
  t2 = clock()
  print("Time: ", t2 - t1)
  writeto()    -- Restore default writing
  return 0
end

result, fn = ProcessFile()
if not result then
  print("Error in parameter: " .. (fn or 'nil'))
end

Of course, comments and (constructive) critisism are welcome!

Regards.

-- 
--._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`--

Sent through GMX FreeMail - http://www.gmx.net