Skip to content

Instantly share code, notes, and snippets.

@noqisofon
Created November 24, 2010 06:49
Show Gist options
  • Save noqisofon/713232 to your computer and use it in GitHub Desktop.
Save noqisofon/713232 to your computer and use it in GitHub Desktop.
csv 形式のファイルをメッセージ DLL の中の人の mc 形式のファイルに変換するスクリプト。
#!/bin/ruby
# -*- encoding: shift_jis -*-
# file: csv2mc.rb
require 'optparse'
opt = OptionParser.new
OPTS = {}
opt.version = "0.0.1"
begin
#
# オプションパーサーの設定を行ないます。
#
opt.on( "--prefix=PREFIX", "出力先を指定します。" ) do |prefix|
OPTS[:prefix] = prefix
end
opt.on( "-l CSVFILE", "--location=CSVFILE", "csv ファイルを指定します。" ) do |csvfile|
OPTS[:infile] = csvfile
end
opt.on( "--interval=NUMBER", "メッセージシンボルの値間隔を指定します。" ) do |interval|
OPTS[:interval] = interval
end
opt.parse!( ARGV )
#
# コマンドライン引数を調べます。
#
if OPTS.has_key? :infile then
infile = File.expand_path( OPTS[:infile] )
else
# 入力ファイルがない場合。
raise "no input file"
end
if OPTS.has_key? :prefix then
target = File.expand_path( OPTS[:prefix] )
else
target = "#{Dir.pwd}/#{File.basename( infile.gsub( /-/, '_' ), '.*' )}.mc"
end
if OPTS.has_key? :interval then
if OPTS[:interval] =~ /^[0-9]+$/ then
interval = OPTS[:interval].to_i
else
# interval の値が数字以外の場合。
raise "invalid option interval: #{OPTS[:interval]}"
end
else
interval = 10
end
rescue => e
puts "#{__FILE__}: #{e.to_s}"
exit
end
begin
message_id = 1000
row_books = []
File.open( infile, "r" ) do |output|
output.each_line do |line|
line.chomp!
row = line.split( /,/ )
if row.size == 3 then
row_books << {
:message_id => row[1] == "success"? 0: message_id,
:symbolic_name => row[0],
:message_text => row[2]
}
message_id += interval unless row[1] == "success"
end
end
end
rescue => e
puts "#{__FILE__}: #{e.to_s}"
exit
end
anti_double_symbol = "#{File.basename( infile.gsub( /-/, '_' ), '.*' )}_h"
File.open( target, "w" ) do |input|
input.puts ";#ifndef #{anti_double_symbol}"
input.puts ";#define #{anti_double_symbol}"
input.puts
input.puts "MessageIdTypedef=DWORD"
input.puts "LanguageNames=(Japanese=0x0441:MSG00441)"
input.puts "OutputBase=16"
input.puts
row_books.each do |row|
input.puts "MessageId=#{row[:message_id]}"
input.puts "SymbolicName=#{row[:symbolic_name]}"
input.puts "Language=Japanese"
input.puts "#{row[:message_text]}"
input.puts "."
input.puts
end
input.puts ";#endif /* #{anti_double_symbol} */"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment