コンピュータを楽しもう!!

今、自分が面白くていろいろやってみたことを書き綴りたいと思います。連絡先はtarosa.yでgmail.comです。

ドロルアリダランチャ

DOROKURIのドロルアリダからダウンロードしたアプリを実行するランチャ「ドロルアリダランチャ」をアップデートしました。
修正内容は、実行するluaファイルを検索するときに、目的のフォルダにluaファイルが見つからない場合、サブフォルダまで探すようにしたところです。
アンドロイドマーケットにリンク

ドロルアリダランチャのソース

ドロルアリダランチャもLuaridaのプログラムなので、ソースを載せておきます。ドロルアランチャには汎用的に使える関数として、「画面の縦横の自動設定」と「ファイル名を取得する」関数が実装されています。ファイル名を取得する関数は、ファイル名だけではなくて、ファイル属性、ユーザ名、グループ名、サイズ、作成日、作成時間がテーブルになって返ってくるので、参考にして下さい。

------------------------------------------
-- doroLuaridaインストールアプリランチャ
-- Ver 1.1
------------------------------------------
--関数宣言--------------------------------
main={}                 --mainメソッド
getFiles={}             --ファイル名を取得します
split={}                --文字の分解
setDirection={}         -- 縦横の自動設定
getLuafile={}           --luaファイルの検索
--グローバル変数宣言----------------------
--------以下が実プログラム----------------
------------------------------------------
-- 縦横の自動設定
------------------------------------------
function setDirection()
local i
local ax,ay
 sensor.setdevAccel( 1 )        --加速度センサ起動
 for i=1,32 do
   ax, ay = sensor.getAccel()
 end
 sensor.setdevAccel( 0 )       --加速度センサオフ
 if( ax<5 and ay>5 )then
   system.setScreen(1)         --縦向きに変更
   --内部グラフィック画面設定の変更
   local w,h = canvas.getviewSize()
   canvas.setMainBmp( w, h )
   canvas.drawCls()
 end
end
------------------------------------------
--文字の分解
------------------------------------------
function split(str, d)
local s = str
local t = {}
local p = "%s*(.-)%s*"..d.."%s*"
local f = function(v) table.insert(t, v) end
 if s ~= nil then
   string.gsub(s, p, f)
   f(string.gsub(s, p, ""))
 end
 return t
end
------------------------------------------
-- ファイル・フォルダ名を取得し配列を返します
--files[ ].attr, user, group, size, date, time, name が返る
------------------------------------------
function getFiles( folder )
local files={}
local tmpFile = system.getCardMnt().."/luarida/dorolauncher.txt"  --ファイル名を書き出すテンポラリファイル
local str
local fp, msg
 os.execute( 'ls -l "'..folder..'" > '..tmpFile )
 --ファイルを開きます
 fp, msg = io.open( tmpFile, "r")
 if( not(fp) )then
   dialog( tmpFile.."がオープンできません", msg, 1 )
   return files
 end
 --内容を読みます
 local i = 1
 while(true)do
   str = fp:read("*l")                  --1行読み込み
   if( str==nil )then break end         --読込むデータが無ければ終了
   str = string.gsub( str,"\r","" )     --改行コードを外す
   files[i]={}
   local fg = 0
   local sc = 1
   local j = 1
   local n = 0
   if( string.sub( str, 1, 1 )=="d" )then  fg = 1 end
   --スペースの数を数えます
   for k=1, string.len(str) do
     if(  string.sub( str, k, k )==" " )then
       if( n~=k-1 )then
         if( j==1 )then
           files[i].attr = string.gsub( string.sub( str, sc, k-1 ), "^%s*(.-)%s*$", "%1")
           sc = k
           j = j + 1
         elseif( j==2 )then
           files[i].user = string.gsub( string.sub( str, sc, k-1 ), "^%s*(.-)%s*$", "%1")
           sc = k
           j = j + 1
         elseif( j==3 )then
           files[i].group = string.gsub( string.sub( str, sc, k-1 ), "^%s*(.-)%s*$", "%1")
           sc = k
           j = j + 1
         elseif( j==4 and fg==0 )then
           files[i].size = string.gsub( string.sub( str, sc, k-1 ), "^%s*(.-)%s*$", "%1")
           sc = k
           j = j + 1
         elseif( j==4 and fg==1 )then
           files[i].size = "0"
           files[i].date = string.gsub( string.sub( str, sc, k-1 ), "^%s*(.-)%s*$", "%1")
           sc = k
           j = j + 1
         elseif( j==5 and fg==0 )then
           files[i].date = string.gsub( string.sub( str, sc, k-1 ), "^%s*(.-)%s*$", "%1")
           sc = k
           j = j + 1
         elseif( j==5 and fg==1 )then
           files[i].time = string.gsub( string.sub( str, sc, k-1 ), "^%s*(.-)%s*$", "%1")
           files[i].name = string.sub( str, k+1 )
           break
         elseif( j==6 and fg==0 )then
           files[i].time = string.gsub( string.sub( str, sc, k-1 ), "^%s*(.-)%s*$", "%1")
           files[i].name = string.sub( str, k+1 )
           break
         end
       end
       n = k
     end
   end
   i = i + 1
 end
 io.close( fp )
 return files
end
------------------------------------------
--luaファイルの検索
------------------------------------------
function getLuafile( folder )
local files={}
local cname = ""
 files = getFiles( folder.."/" )
 if( #files==0 )then
   dialog("メッセージ", "ファイルが見つかりませんでした", 1 )
   El_Psy_Congroo()
 end

 local name
 local brflg = false
 for i=1, #files do
   name = files[i].name
   for i=1, name:len() do
     cname = name:sub(-i )
     if( cname:sub(-i):lower()==".lua" )then
       brflg = true
       break
     end
   end
   if( brflg==true )then break end
 end

 if( brflg==true )then
   return (folder.."/"..name)
 end

 --見つからなかったとき
 name = ""
 for i=1, #files do
   if( files[i].attr:sub(1,1)=="d" )then
     --再帰的に呼び出します
     name = getLuafile( folder.."/" ..files[i].name )
     if( name~="" )then break end
   end
 end
 return name
end
------------------------------------------
--メインプログラム
------------------------------------------
function main()
local files={}
local i
local sel
local folder={}

 -- 縦横画面の自動設定
 setDirection()

 --フォルダ内データの取得
 files = getFiles( system.getCardMnt().."/dorokuri/doroluarida/" )
 if( #files==0 )then
   El_Psy_Congroo()
 end
 item.clear()
 sel = 1
 for i=1, #files do
   if( string.sub( files[i].attr, 1, 1)=="d" )then
     folder[sel] = files[i].name
     item.add( folder[sel], 0 )
     sel = sel + 1
   end
 end
 sel = item.list( "実行アプリを選択してください(Ver1.1)" )
 if( sel ==0 )then
   dialog("メッセージ", "doroLuaridaランチャを終了します", 1 )
   El_Psy_Congroo()
 end

 --Luaファイルを(再帰的に)検索します
 local cname = getLuafile(  system.getCardMnt().."/dorokuri/doroluarida/"..folder[sel] )

 if( cname~="" )then
   system.setrun( cname )
   system.setScreen(0)   --横向きに変更
   --内部グラフィック画面設定の変更
   local w,h = canvas.getviewSize()
   canvas.setMainBmp( w, h )
   canvas.drawCls()
 else
   dialog("メッセージ", "Luaファイルが見つかりませんでした", 1 )
   El_Psy_Congroo()
 end
end
main()