#! /usr/bin/env ruby
# DoCoMo の公開している i-mode 端末の表示能力、対応 HTML バージョンのテー
# ブルからほしい情報を扱いやすい形に抜き出す。
#
# 「扱いやすい形」とは各スクリプト言語ででそのまま配列やハッシュとして扱
# える形を意味する。対応言語は今のところ Ruby, Perl, PHP
#
# 元データ http://www.nttdocomo.co.jp/mc-user/i/spec/ryouiki.html
#
# Published with Creative Commons 帰属 - 非営利 - 同一条件許諾 License.
#
# Histyory:
# 2004-11-08 思ったように動いた
require 'nkf'
$KCODE = 'EUC'
DEBUG = false
FORMAT = 'plain' # Perl, PHP, Ruby, plain
@terminal = Array.new() # モデル名のリスト(以下モデル名を key とする)
@chtml = Hash.new() # 対応する DoCoMo C-HTML のバージョン
@col = Hash.new() # 1行の文字数
@row = Hash.new() # 表示行数
@width = Hash.new() # 横幅(px)
@height = Hash.new() # 高さ(px)
@color = Hash.new() # 色数
#
# 1行ずつ読み込んで正規表現で引っ掛けて意味のあるデータとして格納
#
def read_html
html_ver = 0 # 端末情報が書かれている領域であるかどうかのフラグの役割も
item = 1
while ( line = gets )
line = NKF.nkf( '-e', line )
if ( line =~ /(iモード対応HTML([1-9])\.[0-9])/i )
# HTML のバージョンのセット
not_begin_line = Regexp.new( '.*'+$1+'.*<\/a>', 'i' )
html_ver = sprintf( "%1i", $2 ).to_i
item = 1
# toc の場合があるのでその場合はバージョンをゼロに
if ( line =~ not_begin_line )
html_ver = 0
next
end
elsif ( html_ver > 0 && ( item == 1 || item >= 6 ) &&
line =~ /
.*>([A-Z]{1,2}[0-9]{3,4}[a-zA-Z]{0,2}).*<\/td>/i )
# 型番
model = $1.to_s
if ( DEBUG )
STDERR.puts "- model " + model
end
item = item + 1
@terminal.push( model )
@chtml[model] = html_ver
elsif ( item == 2 && line =~ /(?:(?: | |デフォルト).*>|^)([0-9]{1,2})×([0-9]{1,2})/i )
# 表示領域(文字)
col = $1.to_i
@col[model] = col
row = $2.to_i
@row[model] = row
if ( DEBUG )
STDERR.puts "col " + col.to_s + " row " + row.to_s
end
item = item + 1
elsif ( item == 3 && line =~ / | .*/i )
# 文字数は無視
item = item + 1
elsif ( item == 4 && line =~ /( | |デフォルト).*>([0-9]{2,})×([0-9]{2,}).*<\/td>/i )
# 画面領域(ピクセル)
width = $2.to_i
@width[model] = width
height = $3.to_i
@height[model] = height
if ( DEBUG )
STDERR.puts "width " + width.to_s + " height " + height.to_s
end
item = item + 1
elsif ( item == 5 && line =~ / | /i )
# 待ち受けの画面領域は無視
item = item + 1
elsif ( item == 6 && line =~ /(?:白黒([2244])階調|(?:[^0-9]|^)([0-9]+)色).*<\/td>/i )
# 色数
if ( $1 )
color = NKF.nkf( '-Z', $1 ).to_i
@color[model] = color
elsif ( $2 )
color = $2.to_i
@color[model] = color
end
if ( DEBUG )
STDERR.puts "color " + color.to_s
end
item = 1
end
end # of main while loop
end # of read_html()
#
# ハッシュ式を各言語向けに生成する
#
def output_hash_expression
if ( FORMAT == 'PHP' )
puts " max_model_length )
max_model_length = model.size
end
}
puts @array_paren_close[FORMAT]
puts @hash_prefix[FORMAT] + "chtml = " + @hash_paren_open[FORMAT]
@terminal.each { |model|
printf( " %-*s => %s,\n", max_model_length+2, "'" + model + "'", @chtml[model].to_s )
}
puts @hash_paren_close[FORMAT]
puts @hash_prefix[FORMAT] + "col = " + @hash_paren_open[FORMAT]
@terminal.each { |model|
printf( " %-*s => %s,\n", max_model_length+2, "'" + model + "'", @col[model].to_s )
}
puts @hash_paren_close[FORMAT]
puts @hash_prefix[FORMAT] + "row = " + @hash_paren_open[FORMAT]
@terminal.each { |model|
printf( " %-*s => %s,\n", max_model_length+2, "'" + model + "'", @row[model].to_s )
}
puts @hash_paren_close[FORMAT]
puts @hash_prefix[FORMAT] + "width = " + @hash_paren_open[FORMAT]
@terminal.each { |model|
printf( " %-*s => %s,\n", max_model_length+2, "'" + model + "'", @width[model].to_s )
}
puts @hash_paren_close[FORMAT]
puts @hash_prefix[FORMAT] + "height = " + @hash_paren_open[FORMAT]
@terminal.each { |model|
printf( " %-*s => %s,\n", max_model_length+2, "'" + model + "'", @height[model].to_s )
}
puts @hash_paren_close[FORMAT]
puts @hash_prefix[FORMAT] + "color = " + @hash_paren_open[FORMAT]
@terminal.each { |model|
printf( " %-*s => %s,\n", max_model_length+2, "'" + model + "'", @color[model].to_s )
}
puts @hash_paren_close[FORMAT]
puts get_plainoutput_expression()
if ( FORMAT == 'PHP' )
puts "?>"
end
end # of output_hash_expression()
#
# 各言語向けの確認用出力処理の生成
#
def get_plainoutput_expression
case FORMAT
when 'Ruby'
return < '@',
'PHP' => '$',
'Perl' => '@',
}
@array_paren_open = {
'Ruby' => '[',
'PHP' => 'array(',
'Perl' => '(',
}
@array_paren_close = {
'Ruby' => ']',
'PHP' => ');',
'Perl' => ');',
}
@hash_prefix = {
'Ruby' => '@',
'PHP' => '$',
'Perl' => '%',
}
@hash_paren_open = {
'Ruby' => '{',
'PHP' => 'array(',
'Perl' => '(',
}
@hash_paren_close = {
'Ruby' => '}',
'PHP' => ');',
'Perl' => ');',
}
end # of set_syntax()
set_syntax()
read_html()
if ( FORMAT == 'plain' )
output_db_plain()
else
output_hash_expression()
end
|