10 - FileInput.lua

Last-modified: 2009-03-11 (水) 12:36:47

(scripts/10 - FileInput.lua)

01:function textView( szFileName )
02:  local i = 0
03:  local line
04:  for line in io.lines ( szFileName ) do
05:    i = i + 1
06:    print( i .. "[" .. line .. "]" )
07:  end
08:end
09:
10:function hexView( szFileName )
11:  local f = assert( io.open( szFileName, "rb" ) )
12:  local block = 10
13:  while true do
14:    local strResult = ""
15:    local bytes = f:read(block)
16:    if not bytes then break end
17:    for b in string.gfind(bytes, ".") do
18:      strResult = strResult .. ( string.format("%02X", string.byte( b ) ) )
19:    end
20:    strResult = strResult .. ( string.rep( "  ", block - string.len( bytes ) ) ) .. " "
21:    strResult = strResult .. string.gsub( bytes, "%c", "." )
22:    print( strResult )
23:  end
24:  f:close()
25:end
26:
27:SCREEN_BOTTOM   = 0
28:BGBotText = Screen.LoadTextBG()
29:Screen.Initialize( SCREEN_BOTTOM, BGBotText )
30:
31:textView( "02 - TextPadsStylus.lua" )
32:print( "Press any key to continue" )
33:DSLua.WaitForAnyKey()
34:
35:hexView( "explosion.bmp" )
36:print( "Press any key to exit" )
37:DSLua.WaitForAnyKey()

プログラムの解説

01:function textView( szFileName )
02:  local i = 0
03:  local line
04:  for line in io.lines ( szFileName ) do
05:    i = i + 1
06:    print( i .. "[" .. line .. "]" )
07:  end
08:end
09:
10:function hexView( szFileName )
11:  local f = assert( io.open( szFileName, "rb" ) )
12:  local block = 10
13:  while true do
14:    local strResult = ""
15:    local bytes = f:read(block)
16:    if not bytes then break end
17:    for b in string.gfind(bytes, ".") do
18:      strResult = strResult .. ( string.format("%02X", string.byte( b ) ) )
19:    end
20:    strResult = strResult .. ( string.rep( "  ", block - string.len( bytes ) ) ) .. " "
21:    strResult = strResult .. string.gsub( bytes, "%c", "." )
22:    print( strResult )
23:  end
24:  f:close()
25:end
26:
27:SCREEN_BOTTOM   = 0
28:BGBotText = Screen.LoadTextBG()
29:Screen.Initialize( SCREEN_BOTTOM, BGBotText )
30:
31:textView( "02 - TextPadsStylus.lua" )
32:print( "Press any key to continue" )
33:DSLua.WaitForAnyKey()
34:
35:hexView( "explosion.bmp" )
36:print( "Press any key to exit" )
37:DSLua.WaitForAnyKey()

コメント