--:-- LUA API

SDカード

sd_exists / sd_read / sd_write / sd_remove / sd_mkdir / sd_list / config_read / config_get / config_write

SDカードが使用不可の間、および sd_outside_app_dir 権限が無いのに app_dir の外を指した場合、これらはすべてエラーにならず失敗値(false/nil)を返すだけです。権限の詳細は 権限モデル を参照してください。

アプリの app.cfg は書き換えられません。 書き込み系(sd_write / sd_remove / sd_mkdir / canvas_save / config_write)は、自分のアプリディレクトリ直下の app.cfg と /lua/apps/<名前>/app.cfg を指すと false を返します(sd_outside_app_dir 権限があっても同じ)。sd_remove は、それらを含むディレクトリ(自分のアプリディレクトリとその親、/lua/apps とその下の各アプリのディレクトリ)も消せません。読むのは自由です。

pico.sd_exists

pico.sd_exists(path: string) -> exists: boolean

pico.sd_read

pico.sd_read(path: string) -> content: string | nil

ファイル全体を文字列で読み込みます。上限は16KiBで、超過するファイルは切り詰めずに nil を返します(壊れたデータを気付かず使わせないため)。読み取り自体が失敗した場合も nil。

pico.sd_write

pico.sd_write(path: string, content: string, append?: boolean) -> ok: boolean

append の既定値は false(新規作成+上書き)。true を渡すとファイル末尾へ追記します。

pico.sd_remove

pico.sd_remove(path: string) -> ok: boolean

ファイルなら削除、ディレクトリなら中身ごと削除します。

pico.sd_mkdir

pico.sd_mkdir(path: string) -> ok: boolean

pico.sd_list

pico.sd_list(path: string) -> entries: table | nil

path がディレクトリなら、{ {name = string, is_dir = boolean, size = integer}, ... } の配列を返します(size はバイト数、ディレクトリは 0)。path が存在しない・ディレクトリでない場合は nil。

local entries = pico.sd_list("/lua/apps/myapp")
if entries then
    for i, e in ipairs(entries) do
        print(i, e.name, e.is_dir)
    end
end

pico.sd_stat

pico.sd_stat(path: string) -> info: table | nil

ファイル/ディレクトリの情報 {size = integer, is_dir = boolean} を返します。存在しない・SDが使えない・権限外のときは nil。更新日時は持っていません。

pico.sd_read_part

pico.sd_read_part(path: string, offset: integer, length: integer) -> data: string | nil

offset バイト目から最大 length バイトを読みます。ファイルの終わりを越える分は短く(完全に越えていれば空文字列)返ります。length の上限は sd_read と同じ16KiB。offset / length が負だとエラー、存在しない・ディレクトリは nil。sd_read では読めない大きなファイルを少しずつ読むためのものです。

pico.config_read

pico.config_read(path: string) -> entries: table | nil

key=value 形式の設定ファイルを読み、{ key = value, ... } のテーブルで返します。値は常に文字列です(前後の空白は除かれます)。# で始まる行はコメント、同じキーが複数あれば後の行が勝ちます。ファイルが無い・ディレクトリ・16KiBを超える場合は nil。

pico.config_get

pico.config_get(path: string, key: string) -> value: string | nil

1つのキーの値だけを返します。ファイルかキーが無ければ nil。

pico.config_write

pico.config_write(path: string, key: string, value: string | number | boolean) -> ok: boolean

key の行だけを書き換えます(無ければ末尾へ追記、ファイルが無ければ作成)。コメントや他の行はそのまま残ります。一時ファイル(path .. ".tmp")へ書いてから差し替えます。

  • 数値は 55 / 0.25 のように、真偽値は true / false として書きます(読むときは tonumber(v) や v == "true")。
  • キーは64バイト未満で、=・改行・前後の空白を含まず # で始まらないこと(違反はエラー)。値の上限は160バイト未満。
  • 改行を含む値は書けず false を返します。